Reputation: 311
The code below creates a set of records from an entity I use to hold "template" records. I loop through the templates and create records which works including the lookup fields where I use an EntityReferenceentity. But when I use an EntityReference to create a relationship back to the parent entity record I get this error.
crm 2011 Unable to cast object of type 'System.Guid' to type 'Microsoft.Xrm.Sdk.EntityReference'
foreach (var template in templateSteps.Entities)
{
Entity step = new Entity("img_workflowmanager");
step["subject"] = template["img_name"];
if (step.Contains("img_poststepid"))
{
step["img_poststepid"] = (EntityReference)template["img_poststepid"];
}
if (step.Contains("img_prestepid"))
{
step["img_prestepid"] = (EntityReference)template["img_prestepid"];
}
step["img_workflowstepsid"] = (EntityReference)postMessageImage["img_procurementpackageid"];
this._orgService.Create(step);
}
Upvotes: 2
Views: 9761
Reputation: 15128
The message is clear, postMessageImage["img_procurementpackageid"]
contains a Guid and not an EntityReference
.
Assuming the entity name is img_workflowsteps
you can write
Guid packageId = (Guid)postMessageImage["img_procurementpackageid"];
step["img_workflowstepsid"] = new EntityReference("img_workflowsteps", packageId);
By the way, the first two if conditions will be never executed, because when you create an entity with that syntax, no attributes are defined.
Upvotes: 8