4th Create total timespan field in Case, update this field value based on timespan update in child records.

Create total timespan field in Case, update this field value based on timespan update in child records.

Step1: Create Total timespan field in Case entity.

 

Step2: Create the fetch xml to retrieve the records which having  total time in timespan

 

Step3: Get the all the records using fetch xml

 

Step4: Using foreach get the total time in timespan and add the total time using foreach

 

Step5: Update the that time in case entity


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Query;


namespace TimeSpanFRR

{

    public class Totaltime : IPlugin

    {

        public void Execute(IServiceProvider serviceProvider)

 

 

        {

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

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

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

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

 

 

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

            {

               

                try

                {

 

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

                    tracing.Trace(" start case ");

 

 

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

"  <entity name='effizz_timespan'>" +

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

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

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

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

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

"    <filter type='and'>" +

"      <condition attribute='effizz_case' operator='eq' uiname='remo4ss' uitype='incident' value='" + caseEntity.Id + "' />" +

"    </filter>" +

"  </entity>" +

"</fetch>";

 

tracing.Trace("fetchxmlcode: {0}", timespanfetch);

 

EntityCollection timespan = service.RetrieveMultiple(new FetchExpression(timespanfetch));

 

 int totaltime = 0;

 

                    foreach (var item in timespan.Entities)

                    {

                        int timew = item.GetAttributeValue<int>("effizz_timedifference");

                        totaltime = totaltime + timew;

                        tracing.Trace("Time is:{0}", totaltime);

                    }

 

                    caseEntity["new_caseresolution"] = totaltime;

                    tracing.Trace("total time:{0}", totaltime);

 

                    if (context.Depth == 1)

                    {

                        service.Update(caseEntity);

                    }

                }

 

                catch (Exception ex)

                {

                    throw new InvalidPluginExecutionException(ex.Message);

                }

            }

        }

    }

}

 

Create Total timespan (CaseResolution) field in Case entity. 


Update the name because took filter attribute is Case Title.










Comments

Popular posts from this blog

If any case created, then check for the same user how many cases are created with in 30 days, if more than 2 and less than 5 send a mail to team lead, if more than 5 and less than 9 then send a mail to manager using power automate.

Create approve & reject ribbon buttons, once click on approve it should change the status field to approve.If clicked on reject it should change to Reject. Based on status field change trigger work flow to send a email to stating request is approved/Rejected.

How to get and set values in plugins?