Update of case update timespan and create new timespan

 2) If case status changed to InProgress then, update the timespan record with current time as end date and calculate the time difference of start and end time update the timespan field.

3.create new timespan record with status Inprogress and current time as start time.

This plugin works on the update of case status and pre operation pipeline and retrieving the timespan records which does not have end date.

by using fetch xml we can get the data 

Step1: Read  the status of the case.

Step2: Create the fetch xml in correct formate.

Step3: Retrive the all the recrods using fetch xml and store in entity collection.

Step4: By using for each read  the individual record data.

Step5: Get the start date of each record and substract the start date with utcNow().

Step6: Update the end date in end date filed.

Step7: Update the timediffrence in the totlstime filed and update the timespan record.

After Foreach write the code to creating on record with case case.


C# Code is below

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 UpdateofCasestatuschnage :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

            {

                tracing.Trace("entity update");

                if (context.Depth == 1)

                {

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

                    {

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

                        OptionSetValue Casestatus = Caseobject.Contains("cr8d7_casestatus") ? (OptionSetValue)Caseobject["cr8d7_casestatus"] : null;

                        tracing.Trace(Casestatus.Value + "");

                            string Query = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +

      "<entity name='cr8d7_timespan'>" +

        "<attribute name='cr8d7_timespanid' />" +

        "<attribute name='cr8d7_name' />" +

         "<attribute name='cr8d7_casestart' />" +

        "<attribute name='createdon' />" +

        "<order attribute='cr8d7_name' descending='false' />" +

        "<filter type='and'>" +

          "<condition attribute='cr8d7_casestatus' operator='eq' uitype='incident' value='" + Caseobject.Id + "' />" +

          "<condition attribute='cr8d7_enddate' operator='null' />" +

        "</filter>" +

      "</entity>" +

    "</fetch>"

                            EntityCollection Timespandata = service.RetrieveMultiple(new FetchExpression(Query));

                            foreach (var Alldata in Timespandata.Entities)

                            {

                                DateTime Startdate = Alldata.GetAttributeValue<DateTime>("cr8d7_casestart");//cr8d7_casestart

                                tracing.Trace(Startdate.ToString());

                                DateTime dtime = DateTime.UtcNow;

                                TimeSpan timeDiff = dtime - Startdate;

                                tracing.Trace("total minutes" + timeDiff.Minutes);

                                Alldata["cr8d7_enddate"] = DateTime.UtcNow;

                                Alldata["cr8d7_totaltimeoncase"] = timeDiff.Minutes;

                                service.Update(Alldata);

                            }

//This is for the 3rd Question for creating the new timespan based on the case status

                            Entity TimespanObject = new Entity("cr8d7_timespan");

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

                            DateTime Stattime = DateTime.UtcNow;

                            TimespanObject["cr8d7_casestart"] = Stattime;

                            TimespanObject["new_statusofcases"] = new OptionSetValue(Casestatus.Value);

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

                            service.Create(TimespanObject);

                            return;

                        }

                }

            }

            catch (Exception Ex)

            {

                throw new InvalidPluginExecutionException(Ex.Message);

            }

        }

    }

}

For update the record, better to use the Depth to control the infinity loop like this

Context.Depth==1


After Trigger we will get the output like below updating the timespan record


Creating of new timespan record with case status.




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