I've created an assembly to use within a workflow in Dynamics 365 v9.
I have a Parent entity with a 1:N parental relationship to my Child entity. I want to create the Parent and Child in the same transaction so that if anything fails the whole thing is rolled back.
The documentation says this is done with the AddRelatedObject method.
var parent = new Parent()
{
Name = "PARENT"
};
var child = new Child()
{
Name = "CHILD"
};
crmContext.AddObject(parent);
crmContext.AddRelatedObject(parent, new Relationship("my_relationship"), child);
crmContext.SaveChanges();
The classes for Parent and Child were created with the Early Bound Generator plugin for XrmToolbox and have not been altered.
When I call SaveChanges
I get the exception:
Message: An error occured while processing this request.
Inner Message: Cannot find record to be updated
I don't know why this is occurring. The same code works in CRM 2011.
If I remove the AddRelatedObject
line the parent is created just fine.
Any ideas what I'm doing wrong?
The issue for me turned out to be a separate workflow connected to the child entity that tries to update the parent entity. It was upset because it assumed there was a parent when there was not.
I put a Check Condition
at the start of the workflow to terminate it if there's no parent record which has now solved the error.
I didn't realise that errors caused in other plugins/workflows would bubble up to mine.
For anyone else getting this error I'd check two things: