Skip to main content

Notifications

Microsoft Dynamics CRM forum
Answered

How to get the Guid of an CRM entity?

Posted on by 135

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

Categories:
  • Suggested answer
    Charan Raju C R Profile Picture
    Charan Raju C R 3,155 Moderator on at
    RE: How to get the Guid of an CRM entity?

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

  • Peter Cong Profile Picture
    Peter Cong 135 on at
    RE: How to get the Guid of an CRM 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

  • Suggested answer
    Peter Cong Profile Picture
    Peter Cong 135 on at
    RE: How to get the Guid of an CRM entity?

    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.

  • Suggested answer
    Charan Raju C R Profile Picture
    Charan Raju C R 3,155 Moderator on at
    RE: How to get the Guid of an CRM 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;

  • Peter Cong Profile Picture
    Peter Cong 135 on at
    RE: How to get the Guid of an CRM 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?

  • Charan Raju C R Profile Picture
    Charan Raju C R 3,155 Moderator on at
    RE: How to get the Guid of an CRM entity?

    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
    Peter Cong 135 on at
    RE: How to get the Guid of an CRM entity?

    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

  • Peter Cong Profile Picture
    Peter Cong 135 on at
    RE: How to get the Guid of an CRM entity?

    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

  • RizzKh2 Profile Picture
    RizzKh2 15 on at
    RE: How to get the Guid of an CRM entity?

    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.

    
                          
  • Charan Raju C R Profile Picture
    Charan Raju C R 3,155 Moderator on at
    RE: How to get the Guid of an CRM entity?

    Hi Peter,

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

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,524 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,469 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans