Auomatic creation of timespan while creating of case

See the below link fist for the questions

https://www.blogger.com/blog/post/edit/8626627912470024302/5115457206417440128?hl=en-GB


1. once case is created, automatically timespan record need to create with current time as start date.


For this scenario, we need to write the plugin, based on the plugin we need to create the record automatically.

We need to run the plugin with On create of incident (Case) in post operation as pipeline.

Step1: Create the option set same as case status and values of the both option sets should be same, at that time it help to create easy way.

Step2: write the plugin code to create the timespan record by reading the status of the case.

Step3: write code to create the record of timespan with status of  the case and start time without end date.

Step4: Give the lookup of the target entity, because we need to create the relation between these two entity's.

C# code for the above steps

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Query;

using Microsoft.Xrm.Sdk.Workflow;

using System;

using System.Activities;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace Week3rdtasks

{

    public class Timespancreationoncase : IPlugin

    {

        

        public void Execute(IServiceProvider serviceProvider)

        {

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

            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            IOrganizationService service = (IOrganizationService)factory.CreateOrganizationService(context.UserId);

            ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            try

            {

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

                {


                    {

                        tracing.Trace("entered to create record");

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

                        Entity TimespanObject = new Entity("cr8d7_timespan");

                        TimespanObject["cr8d7_name"] = "Timespan" + DateTime.Now;

                        DateTime Stattime = DateTime.UtcNow;

                        TimespanObject["cr8d7_casestart"] = Stattime;

                 //       TimespanObject["cr8d7_caseenddate"] = null;

                        TimespanObject["cr8d7_casestatus"] = new EntityReference(Caseobject.LogicalName, Caseobject.Id);

                        service.Create(TimespanObject);

                        return;

                    }

                }


            } catch (Exception ex)

            {

                throw new InvalidPluginExecutionException(ex.Message);

            }      

    }

    }  

}

After writing the code, build the solution and register.

You will get the following output after trigger




Comments

Popular posts from this blog

create HTML page show all contact related to specific account, add this webresource as a subgrid in the form

Connect MS Crm To Postman For Webapi Calls.

create one PCF control and embed into crm