Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

How to get Guid ID from localContext.OrganizationService.Update(entity)?

Posted on by 135

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
    Peter Cong 135 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
    Charan Raju C R 3,155 Super User 2024 Season 1 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
    Peter Cong 135 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
    Charan Raju C R 3,155 Super User 2024 Season 1 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
    Peter Cong 135 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
    Charan Raju C R 3,155 Super User 2024 Season 1 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans