I am creating a plugin in dynamics 365, the aim is when the incident entity statuscode is set too 1 or 3 then a work flow is triggered. However, the statuscode must be set to 1 or 3 for a period of 24 hours before the workflow is triggered.
Also after the workflow is executed the statuscode should be set to pending.
i am unsure if i am going in the right direction to code this. I am unsure how to do this. If someone can show me how to solve this scenario. Thanks!
This is what i have so far: For testing purposes the time period to trigger the workflow is 10 seconds
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
//create an entity
Entity entity = (Entity)context.InputParameters["Target"];
//after creating the entity, we need to retrieve the required entity: Incident
//retrieve incident entity
Incident detail = entity.ToEntity<Incident>();
//contain int value that represents the optionset
TimeSpan sec = new TimeSpan(00,00,10);
// var incident = service.Retrieve("incident", detail.IncidentId.Value, new Microsoft.Xrm.Sdk.Query.ColumnSet(true)).ToEntity<Incident>();
//retrieve the value of status code of a particular entity
//if optionsetvalue is the same for a 24 hr period
if (detail.StatusCode== new OptionSetValue(1) || detail.StatusCode == new OptionSetValue(3))
{
if (sec != null)
{
ExecuteWorkflowRequest request = new ExecuteWorkflowRequest()
{
WorkflowId = new Guid("DB9ABA7E-D4F9-4EBF-8062-C85EF7B850FB"),
EntityId = detail.Id,
};
ExecuteWorkflowResponse response = (ExecuteWorkflowResponse)service.Execute(request);
//change it to pending..statuscode
detail.StatusCode = new OptionSetValue(425390002);
}
service.Update(detail);
}
}
Thank you for your time
*This post is locked for comments