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,

  • Suggested answer
    Peter Cong Profile Picture
    137 on at
    RE: How to get Guid ID from localContext.OrganizationService.Update(entity)?

    Thank you so much, Charan,

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

  • Suggested answer
    Charan Raju C R Profile Picture
    7 Moderator on at
    RE: How to get Guid ID from localContext.OrganizationService.Update(entity)?

    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.

  • Peter Cong Profile Picture
    137 on at
    RE: How to get Guid ID from localContext.OrganizationService.Update(entity)?

    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
    RE: How to get Guid ID from localContext.OrganizationService.Update(entity)?

    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
    RE: How to get Guid ID from localContext.OrganizationService.Update(entity)?

    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
    RE: How to get Guid ID from localContext.OrganizationService.Update(entity)?

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

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 97

#3
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 82 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans