web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

How to get the Guid of an CRM entity?

(0) ShareShare
ReportReport
Posted on by 137

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

I have the same question (0)
  • Charan Raju C R Profile Picture
    7 Moderator on at

    Hi Peter,

    please post the code in your CreateNewEntity() function here.

  • RizzKh2 Profile Picture
    15 on at

    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.

    
                          
  • Peter Cong Profile Picture
    137 on at

    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

  • Peter Cong Profile Picture
    137 on at

    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

  • Charan Raju C R Profile Picture
    7 Moderator on at

    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.

  • Peter Cong Profile Picture
    137 on at

    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?

  • Suggested answer
    Charan Raju C R Profile Picture
    7 Moderator on at

    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;

  • Suggested answer
    Peter Cong Profile Picture
    137 on at

    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.

  • Peter Cong Profile Picture
    137 on at

    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

  • Suggested answer
    Charan Raju C R Profile Picture
    7 Moderator on at

    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);

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans