Reputation: 5351
I'm trying to make a custom workflow on Dynamics CRM. I need to delete some entities when another entity is deleted.
I created my class library
and I retrieved the Guid
of the deleted entity with this code:
protected override void Execute(CodeActivityContext executionContext)
{
ITracingService tracingService = executionContext.GetExtension<ITracingService>();
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory =
executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service =
serviceFactory.CreateOrganizationService(context.UserId);
mService = service;
mExecutionContext = executionContext;
Guid myTipologyTypeDeleted = context.PrimaryEntityId;
bool isReading = context.PrimaryEntityName.Equals(new_tipologialettura_richiesta.EntityLogicalName);
bool isMaintenance = context.PrimaryEntityName.Equals(new_tipologiamanutenzionerichiesta.EntityLogicalName);
bool myResult = AddOnIntervention(isReading, isMaintenance, myTipologyTypeDeleted);
// Retrieve the summands and perform addition
result.Set(executionContext, myResult);
}
And here all works, I get the Guid
and I get the type
(reading or maintenance).
My problem is when I try to retrieve the entity with this code (the same code is working perfectly in another workflow started on record creation, but on record delete gives me error).
Entity myReadingEntity = mService.Retrieve(new_tipologialettura_richiesta.EntityLogicalName, myTipologyTypeDeleted, new ColumnSet(true));
Here I get an exception saying that no record of type MyType
with id myId
has been found.
I checked the record and it still exist in the DB so it has not been deleted. What I'm doing wrong?
Thanks
Upvotes: 3
Views: 1590
Reputation: 2123
I think the best thing to write your custom logic here will be a Plugin
, You Should write a plugin that runs on the
Message: Delete
Stage: POST
After registering the plugin on Post Delete Operation
, you should add an pre-image
that will be available on Post Delete with all the attributes
.Instead of issuing a retrieve, the best practice is to push the required data in an image instead.
Taken from MSDN:
Registering for pre or post images to access entity attribute values results in improved plug-in performance as compared to obtaining entity attributes in plug-in code through RetrieveRequest or RetrieveMultipleRequest requests.
In your plugin change the lines of code:
Entity myReadingEntity = mService.Retrieve(new_tipologialettura_richiesta.EntityLogicalName, myTipologyTypeDeleted, new ColumnSet(true));
to
if (context.PreEntityImages.Contains("YourImageName"))
{
Entity myReadingEntity = context.PreEntityImages["YourImageName"]
}
Upvotes: 1