Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Incident entity

(0) ShareShare
ReportReport
Posted on by

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

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 Moderator on at
    RE: Incident entity

    Try this-

    ==============

    public void Execute(IServiceProvider serviceProvider)

           {

               if (serviceProvider == null)

                   throw new ArgumentNullException("serviceProvider");

               var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

               var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

               var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

               var service = serviceFactory.CreateOrganizationService(context.UserId);

               var adminService = serviceFactory.CreateOrganizationService(null);

               var trace = new Tracer(service, GetType().ToString(), tracingService);

               var message = context.MessageName.ToLowerInvariant();

               if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

               {

                   TimeSpan sec = new TimeSpan(00, 00, 10);

                   Entity targetEntity = (Entity)context.InputParameters["Target"];

                   if (((OptionSetValue)targetEntity["statecode"]).Value == 1 || ((OptionSetValue)targetEntity["statecode"]).Value == 3)

                   {

                       if (sec != null)

                       {

                           ExecuteWorkflowRequest request = new ExecuteWorkflowRequest()

                           {

                               WorkflowId = new Guid("DB9ABA7E-D4F9-4EBF-8062-C85EF7B850FB"),

                               EntityId = targetEntity.Id,

                           };

                           ExecuteWorkflowResponse response = (ExecuteWorkflowResponse)service.Execute(request);

                           var incidentUpd = new Entity("incident");

                           incidentUpd["statuscode"] = 425390002;

                           service.Update(incidentUpd);

                       }

                   }

               }

           }

    ===========================

  • Suggested answer
    Arun Vinoth Profile Picture
    Arun Vinoth 11,615 on at
    RE: Incident entity

    Like I explained in below SO threads, pls fix/design your requirement.

    [View:https://stackoverflow.com/a/51992758/7920473]

    [View:https://stackoverflow.com/a/51993266/7920473]

  • RE: Incident entity

    thanks, unfortunatly i have to use a plugin.

    in my code:

    if (detail.StatusCode== new OptionSetValue(1) || detail.StatusCode == new OptionSetValue(3))

    is this the correct way to check that if the status code equals 1 then trigger of a workflow

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 Moderator on at
    RE: Incident entity

    I still believe that you don't need a plugin as you can call a workflow from a workflow as well. Regarding the statecode Status code, you can check this artical for the code-technet.microsoft.com/.../dn531157.aspx

    If you have added any custom status code then you need to check it from your customizations. Setting >> Customization >> Customize the system >> Entities >> case >> fields >> status reason (statsu code) >> you will find all the applicable status for your system

    Hope this helps.

  • RE: Incident entity

    My boss said they need to trigger another workflow that sends email, when the status code is 1xxxxxx or 3xxxxxxx. They specified that the status code is customised to this particular dynamics-not found in MS doc.

    In my code-is this the correct way to check for status code, i abbreviated it to 1 and 3 in my code.

    Thanks

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 Moderator on at
    RE: Incident entity

    Sorry but based on the requirement you don't need to write plugins. Plugins are used to intercept the crm action and add some logic in between. Who so ever asked you to use plugin, try to convince him/ her with valid reasons why we should not use plugin and why we should use workflow Or ask them why they prefer Plugin, is their any specific need for it. It is possible that you do need plugin and you are not aware of the requirement. At the end, everybody wants to build the good product so questioning and implementing the good design help you grow as well.

    Hope you understand.

  • RE: Incident entity

    unfortunately i have been told i have to use a plugin.  

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 Moderator on at
    RE: Incident entity

    Hi,

    You don't need to write plugin for this. You can achieve this within the workflow itself. Basically workflow has timeout/ wit condition which you can set and then run your logic. You need to trigger this on change of status, check the status is 1 or 3, if yes than wait for 24 hours and then run your actual logic.

    Check the below articles-

    blog.clickdimensions.com/crm-workflows-wait-versus-timeout

    community.dynamics.com/.../dynamics-crm-workflows-wait-vs-timeout

    community.dynamics.com/.../how-to-set-up-a-crm-workflow-timeout-condition

    Hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,436 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans