Hi, In my CRM Plug in, after the entity is created, I need to get its Guid.
This is what I did:
Guid entityId = Guid.Empty;
.....
entity = CreateNewEntity("Account_logicalname", "accountnumber"); //here the entity is created.
entityId = (Guid)entity.Id; //this code does not work, it the entityId returns "00000000-0000-0000-0000-000000000000 "
is my codes wrong ?
Are there any other method to get the Guid of an given entity in this situation?
Thank you so much for your help,
Peter
Update function doesn’t return any value. In order to update a record, you must know GUID of the record in advance before calling update function and set it to entity object as below.
entity.Id = new Guid(“<GUID>”);
localContext.OrganizationService.Update(entity);
Hi Charan,
Sorry, one more question:
I have these codes below:
Guid entityId = Guid.Empty;
…..
entityId = localContext.OrganizationService.Create(entity); //this one is compiled OK, it returns the entityId,
entityId = localContext.OrganizationService.Update(entity); //but I have compile time error for this one, it shows "Cannot implicitly convert type 'void' to System.Guid, "
if localContext.OrganizationService.Update(entity) does not return entityId, then how can I get the entityId in this case, I do need the entityId.
Is there anyway to fix this compile time error?
Thanks again,
Peter
Hi Charan, Thank you so much again,
Yes, in my case, I need to return entity and also need entityId as well,
Therefore, your code of "entity.Id = entityId;" resolves my problem. I marked it resolved, much appreciated!
I don't know the entity.Id needs to assign GUID before return the entity.
If you need only GUID of the record, do not return entity, return entityId. service.Create function returns GUID, but won’t set it to entity object. If you need both entity object and GUID, set GUID to entity’s Id and return entity, as below.
entity.Id = entityId;
return entity;
Hi Charan,
Thanks again for your help, yes, you are right, it may be there is an issue of the entity was generated,
The create entity was bit long, I post part of here with my comments:
Guid entityId = Guid.Empty;
....
Entity entity = new Entity("crm_accountlogigicname");
...
entity.Attributes["lbgtqcompensationreceived"] = new OptionSetValue(747220000);
...
entityId = localContext.OrganizationService.Create(entity);
localContext.Trace("before return entity :" + "entity.Id=" + entity.Id.ToString() + "entityId:" + entityId);
//the above code I got: entity.Id= 00000000-0000-0000-0000-000000000000 entityId: 24ace33e-5dd3-ac11-8217-000d3ae8cbc7
//return entityId; // old code
return entity;
please let me know if you have any idea for further debugging, thanks again
I am thinking my this line of code: entityId = localContext.OrganizationService.Create(entity); it is returning the entityId, however, the entity was not actually created, thus the entity.Id = 00000000-0000-0000-0000-000000000000 , how is that possible? any way to fix it?
If your function CreateNewEntity() is returning Entity object, then entity.Id should give a GUID of a record. Since you're saying it is give empty GUID, we need to take a look at the code block inside the CreateNewEntity() method to check if anything wrong in returning value.
Hi RizzKh2, thanks a lot for your help,
"Another way to find out is to use the logical name of the entity:", could you provide an example code of how to do it? thanks
Hi Charan,
As I mentioned the entity is created through CreateNewEntity() function, I can see the attributes through my debug codes,
my question is the code below it: entityId = (Guid)entity.Id; I am not sure this is right way to get the Guid of entity.
Any ideas? thanks again
Hi friends, CRM developer here. I faced the same issue for my site (techloyce.com) It is correct that the logical name of the entity with the suffix 'id' is commonly used. Another way to find out is to use the logical name of the entity:
EntityDefinitions(LogicalName='account')/PrimaryIdAttribute Returns { "@odata.context":"https://yourorg.api.crm.dynamics.com/api/data/v9.1/$metadata#EntityDefinitions('account')/PrimaryIdAttribute", "value": "accountid" } accounts?$select=createdon&$top=1 { "@odata.context": "https://yourorg.api.crm.dynamics.com/api/data/v9.1/$metadata#accounts(createdon)", "value": [ { "@odata.etag": "W/\"26361144\"", "createdon": "2020-04-29T22:02:36Z", "accountid": "2ed66b21-658a-ea11-a811-000d3a122b89" } ] }
The primary key value will be the other property returned.
Hi Peter,
please post the code in your CreateNewEntity() function here.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 290,524 Super User 2024 Season 2
Martin Dráb 228,469 Most Valuable Professional
nmaenpaa 101,148