Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Entity Id must be the same as the value set in property bag - While trying to Update

Posted on by 645

Hey, I am creating a new record and updating it according to another record, but I get the msg above, how to solve it ?
My Code: 

 public void Clone(Entity firstNeeds, IOrganizationService Service)
        {
            Entity newneeds  = new Entity(new_needs.EntityLogicalName);
            Guid newRecId = Service.Create(newneeds);         
            Entity newRec = Service.Retrieve(new_needs.EntityLogicalName, newRecId , new ColumnSet());
foreach (KeyValuePair<String, Object> attribute in firstNeeds.Attributes) { string attributeName = attribute.Key; object attributeValue = attribute.Value; newRec[attributeName] = attributeValue; } try { Service.Update(newRec); } catch (FaultException<OrganizationServiceFault> ex) { throw new InvalidPluginExecutionException("An error occurred in the Clone function.", ex); } }

 

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Entity Id must be the same as the value set in property bag - While trying to Update

    Hi Shaminderpal

    i have the same problem in my plugin

    and use that coed

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
    Entity entity = (Entity)context.InputParameters["Target"];

    if (entity.LogicalName == "msdyn_agreementbookingdate")
    {
    if (entity.Contains("msdyn_agreement"))
    {
    if (entity["msdyn_agreement"] != null)
    {
    Guid agreementId = ((EntityReference)entity.Attributes["msdyn_agreement"]).Id;

    // get booking dates with the same agreement and does not have the same id as the entity
    var fetchXmlDates = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
    fetchXmlDates += "<entity name='msdyn_agreementbookingdate'>";
    fetchXmlDates += "<attribute name='msdyn_bookingsetup' />";
    fetchXmlDates += "<attribute name='msdyn_bookingdate' />";
    fetchXmlDates += "<attribute name='msdyn_agreement' />";
    fetchXmlDates += "<attribute name='msdyn_agreementbookingdateid' />";
    fetchXmlDates += "<order attribute='msdyn_bookingdate' descending='true' />";
    fetchXmlDates += "<filter type='and'>";
    fetchXmlDates += "<condition attribute='msdyn_agreement' operator='eq' uitype='msdyn_agreement' value='" + agreementId + "' />";
    fetchXmlDates += "<condition attribute='msdyn_agreementbookingdateid' operator='ne' value='" + entity.Id + "' />";
    fetchXmlDates += "</filter>";
    fetchXmlDates += "<link-entity name='msdyn_agreementbookingsetup' from='msdyn_agreementbookingsetupid' to='msdyn_bookingsetup' alias='bookRec'>";
    fetchXmlDates += "<attribute name='new_bookingrecurring' />";
    fetchXmlDates += "<filter type='and'>";
    fetchXmlDates += "<condition attribute='new_bookingrecurring' operator='not-null' />";
    fetchXmlDates += "</filter>";
    fetchXmlDates += "</link-entity>";
    fetchXmlDates += "</entity>";
    fetchXmlDates += "</fetch>";

    var resultDates = service.RetrieveMultiple(new FetchExpression(fetchXmlDates));

    try
    {

    if (resultDates.Entities.Count > 1)
    {
    #region Update Customer Asset
    foreach (Entity c in resultDates.Entities)
    {
    var fetchXmlGETCAForeachBS = $@"
    <fetch>
    <entity name='msdyn_agreementbookingsetup'>
    <attribute name='new_customerasset' />
    <attribute name='msdyn_agreementbookingsetupid' />
    <filter type='and'>
    <condition attribute='msdyn_agreementbookingsetupid' operator='eq' value='{c.GetAttributeValue<EntityReference>("msdyn_bookingsetup").Id}'/>
    </filter>
    </entity>
    </fetch>";
    var resultD = service.RetrieveMultiple(new FetchExpression(fetchXmlGETCAForeachBS));


    foreach (Entity b in resultD.Entities)
    {
    if (c.GetAttributeValue<EntityReference>("msdyn_bookingsetup").Id == (Guid)b.Attributes["msdyn_agreementbookingsetupid"])
    {

    entity.Id = (Guid)c.Attributes["msdyn_agreementbookingdateid"];
    entity.Attributes["new_customerasset"] = Convert.ToString(b.GetAttributeValue<EntityReference>("new_customerasset").Id);
    service.Update(entity);

    }
    }

    }

    #endregion


    }

    }
    catch (Exception e)
    {

    throw new InvalidPluginExecutionException("Have An Error : " +e.Message.ToString());
    }


    }
    }
    }

  • sahara Profile Picture
    sahara 645 on at
    RE: Entity Id must be the same as the value set in property bag - While trying to Update

    Hey Rawish

    I do create a new obj - newneeds and then retrive it and update details on that record.. what is the prob? how should I change it ?

  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: Entity Id must be the same as the value set in property bag - While trying to Update

    Hi Sahara,

    I dont recommend using the same object to update which you have retrieved.

    when you retrieve the entity - it might give you Entity.id or your primarycolumnId.

    hence create a new object for update and try again.

  • Verified answer
    Shaminderpal Singh Profile Picture
    Shaminderpal Singh 1,565 on at
    RE: Entity Id must be the same as the value set in property bag - While trying to Update

    Hi sahara,

    In your foreach loop you are overriding the value of id(unique guid) for  newly created record  with the one from which you are copying.

    Place a condition to skip copying id from one obj to another.

    -Shaminder

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans