简体   繁体   中英

Dynamics 365 (CRM) Version 9.1 online C# code Error: Entity Id must be the same as the value set in property bag

When cloning/copying child records, I use a foreach loop and then create a record with all its attributes. I wrote similar code in another project and worked fine for me.

There are multiple articles/questions based on the same Error. Now my issue is how should I create child records with all its attributes.

foreach (var packingList in oPEntityCollection.Entities)
{                                        
    packingList.Attributes.Remove("statuscode");
    packingList.Attributes.Remove("statecode"); 
    packingList.Id=Guid.Empty; 
    orgService.Create(packingList);
}

Another strange issue

An entry with the same key already exists

Code:

Entity parentEntity = orgService.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(true));    
parentEntity.Id = Guid.empty;
orgService.Create(parentEntity);

Even if I create a new object and copy parentEntity just like below I get this error.

Entity costcalEntity = new Entity();

costcalEntity = parentEntity;
costcalEntity.Id = Guid.Empty;
orgService.Create(costcalEntity);

So I end up creating a record with primary name and once the record is created, I update the same record with old record attributes.

Entity costcalEntity = new Entity();
costcalEntity.LogicalName = parentEntity.LogicalName;
costcalEntity["name"] = parentQuotationEntity.GetAttributeValue<string>("name");
costcalEntity.Id = Guid.Empty;
Guid newGuid = orgService.Create(costcalEntity);
if (newGuid != Guid.Empty)
{
    costcalEntity = parentEntity;
    costcalEntity.Id = newGuid;
    orgService.Update(costcalEntity);
}

and this works fine.

In both cases you have the same issue, with it's root cause being the Id stored in the attribute collection of the entity. If you look at the early bound generation, you can access the Id by the entity.Id property, as well as the attribute collection as shown in the definition for the id in the primary id:

public System.Nullable<System.Guid> AccountId
{
    [System.Diagnostics.DebuggerNonUserCode()]
    get
    {
        return this.GetAttributeValue<System.Nullable<System.Guid>>("accountid");
    }
    [System.Diagnostics.DebuggerNonUserCode()]
    set
    {
        this.OnPropertyChanging("AccountId");
        this.SetAttributeValue("accountid", value);
        if (value.HasValue)
        {
            base.Id = value.Value;
        }
        else
        {
            base.Id = System.Guid.Empty;
        }
        this.OnPropertyChanged("AccountId");
    }
}

So when you are retrieving an existing entity, both the Property Id, which you have handled, as well as the attribute collection, which you haven't handled, have been populated by the CRM SDK. So in order to be able to duplicate it, you'll need to clear the id in both places.

Here is how I solved it

 foreach (Entity packingList in oPEntityCollection.Entities)
                {
                    Entity newpackingList = new Entity()
                    {
                        LogicalName = packingList.LogicalName,
                    };
                    newpackingList.Attributes.AddRange(packingList.Attributes);                        
                    newpackingList.Attributes.Remove("primaryGuid");                       
                    Guid newOpGuid = orgService.Create(newpackingList);    
                    tracingService.Trace($"OP record created sucessfully with guid {newOpGuid}");

                    }

So the Trick, issue was rather I was trying to assign packingList directly to newpackingList . This caused to assign packingList metadata attributes as well such. This was not acceptable with crm

But rather I should add it's attribute. This worked and created all child records.

Same worked for parent record as well

Entity parentEntity = orgService.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(true)); 

   Entity newParentEntity = new Entity()
                        {
                            LogicalName = parentEntity.LogicalName,
                        };
newParentEntity.Attributes.AddRange(parentEntity.Attributes); 
 newParentEntity.Attributes.Remove("primaryGuid");
orgService.Create(newParentEntity);

If your question is "How do I duplicate an Entity retrieved from CRM?", your answer can be simplified.

var entity = orgService.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(true)); 
entity.Id = Guid.Empty;
entity.Attributes.Remove("primaryGuid");
orgService.Create(entity);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM