Retrieve the record Id of the new record after calling Entity.Save()
Hi Community,
I have a Configuration service which saves new record on Account. I am saving new record using Entity.Save(). How can retrieve the record Id of the new record after calling Entity.Save()?
Thanks
Like
Hi Fulgen,
Here is the example of my process that I called using the /0/ServiceModel/ProcessEngineService.svc/UsrAddNewExternalContact/Execute endpoint and that returned an Id of the created contact in the auto-generated page that was launched in the background:
The code of the script task is:
var schema = UserConnection.EntitySchemaManager.GetInstanceByName("Contact"); var entity = schema.CreateEntity(UserConnection); var ContactName="This is contact created via a process"; var ContactPhone="13222123"; entity.SetDefColumnValues(); entity.SetColumnValue("Name", ContactName); entity.SetColumnValue("Phone", ContactPhone); entity.Save(); EntitySchemaQuery query = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "Contact"); query.PrimaryQueryColumn.IsAlwaysSelect = true; var entities = query.GetEntityCollection(UserConnection); List<object> contacts = new List<object>(); foreach (var item in entities){ if (item.GetTypedColumnValue<Guid>("Id") == entity.GetTypedColumnValue<Guid>("Id")) { var contact = new { Id = entity.GetTypedColumnValue<Guid>("Id"), }; contacts.Add(contact); } } Set("ContactList", JsonConvert.SerializeObject(contacts)); return true;
So the idea here is to create a contact calling a process from outside (you can modify the code and pass ContactName and ContactPhone as actual parameters when calling /0/ServiceModel/ProcessEngineService.svc/UsrAddNewExternalContact/Execute), execute the entity.Save(); and then find the created contact and return its Id on the autogenerated page. As a result I've received this Id:
and the contact was created in the system:
So you need to use something similar in your webservice.
Best regards,
Oscar