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 Guid ID from localContext.OrganizationService.Update(entity)?

(0) ShareShare
ReportReport
Posted on by 137

Hi,

I have a plugin with following codes:

Guid entityId = Guid.Empty;

….. entity has been created here

entityId = localContext.OrganizationService.Create(entity);   //this one is compiled OK, it returns the Guid of the entity,        

However, I got compile error if I use the following code:

entityId = localContext.OrganizationService.Update(entity); //the compile time error for this line code shows "Cannot implicitly convert type 'void' to System.Guid, "

my question:

Is there anyway to fix this compile time error to get the entity Guid ID above?

Thanks again

Peter,

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

    Hi Peter,

    Update function doesn’t return any value. In order to update a record, you must know the 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
    137 on at

    Hi Charan,

    Thank you for your quick response, but the thing is that how can I get the "<GUID>"?

    Basically, I have the function like this:

    Entity HandleUpdate(Entity entity) //here the entity object is passed in

           {

         …. then doing some setting like this

                entity.Attributes["dev_dateofbirthtext"] = dob.ToString("MM/dd/yyyy");

               …

              entity.Id = new Guid(“< GUID >”);  //here I need to add your code here, I don't know how to get GUID of the entity passed in.

               localContext.OrganizationService.Update(entity);

        return entity;  //here entity has its GUID which is as same as entity.Id, right?

       }

    I just released that since the entity is passed in, so in this case, the entity.Id is already known, right? so basically, the entity.Id is the GUID of the passing in entity,

    so basically, my codes where calling this update function needs to use entity.Id.

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

    Can I check whether it is a plug-in or any other .Net application? 

    Without knowing GUID of the record, we cannot update it. It is like, without knowing address you cannot reach the destination.

    When calling HandleUpdate(entity) function, entity object should have already set for it's GUID as in below code.

    public void UpdateContactDOB()
    {
        // If you do not know the contact record for which you need to update DOB, retrieve it
        EntityCollection contactRecords = localContext.OrganizationService.RetrieveMultiple(query);
        Entity entity = new Entity("contact");
        foreach(Entity contactRecord in contactRecords.Entities)
        {
            entity.Id = contactRecord.Id;
            HandleUpdate(contactRecord);
        }
    }

  • Peter Cong Profile Picture
    137 on at

    Hi Charan, thanks for your help again,

    but in my case, the "entity" object is already given and it passes in to the function to be used,

    which is same as "contactRecord" in your codes, I am wondering if I need to use RetriveMultiple(query) as your codes?

     please see my codes below:

    Entity HandleUpdate(Entity entity) // here the entity object is passed in the function

    {

    .....

    entity.Attributes["dev_dateofbirthtext"] = dob.ToString("MM/dd/yyyy"); //doing these kind of setting

    localContext.OrganizationService.Update(entity);

    return entity;

    }

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

    Since calling function would have already passed complete entity object, no need to make retrieve multiple call in HandleUpdate() function. So, entity.Id would give you GUID of the record.

  • Suggested answer
    Peter Cong Profile Picture
    137 on at

    Thank you so much, Charan,

    That is what I expected, much appreciated for your quick response, have a great day.

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 73 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 43 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