2nd & 3rd Case status changed to OnHold 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.
2. If case status changed to OnHold 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.
First we need to update the case status.
Next we should go for Pre-Operation in execution pipe line and retrieve the time span record which does not contains end date.
For this we will use fetch xml by this we can get the data
Step1: Read the status of the case.
Step2: Create the fetch xml in correct formate.
Step3: Retrive 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 totaltime filed and update the timespan record.
In Foreach write the code to creating on record with in the case.
Plugin code :
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 CreateTimeSpp
{
public class SpanInto : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
try
{
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)
{
Entity updateTimeSpent = (Entity)context.InputParameters["Target"];
if (updateTimeSpent.Contains("statuscode"))
{
Guid guid = new Guid(updateTimeSpent.Id.ToString());
tracing.Trace("Guid is {0}", guid.ToString());
string dynamicGuid = guid.ToString();
dynamicGuid = dynamicGuid.Replace("{", "").Replace("}", "");
tracing.Trace("DynamicsGuid is {0} ", dynamicGuid);
OptionSetValue statusOptionSetValue = (OptionSetValue)updateTimeSpent["statuscode"];
int statusValue = statusOptionSetValue.Value;
tracing.Trace("status code value is {0} ", statusValue);
if (Context.Depth= =1)
{
string fetchXml = "<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' />" +
" <attribute name='effizz_start' />" +
" <attribute name='effizz_end' />" +
" <attribute name='createdby' />" +
" <order attribute='effizz_name' descending='false' />" +
" <filter type='and'>" +
" <condition attribute='effizz_case' operator='eq' uiname='TestSpan01' uitype='incident' value='" + dynamicGuid + "' />" +
" </filter>" +
" </entity>" +
"</fetch>";
EntityCollection updateTimeSpentRecord = service.RetrieveMultiple(new FetchExpression(fetchXml));
tracing.Trace("Count is {0} ", updateTimeSpentRecord.Entities.Count);
foreach (Entity timespanEntity in updateTimeSpentRecord.Entities)
{
DateTime startDate = (DateTime)timespanEntity["effizz_start"];
tracing.Trace("Start date in update {0} ", startDate.ToString());
DateTime dateTimes = new DateTime();
dateTimes = DateTime.UtcNow;
timespanEntity["effizz_end"] = dateTimes;
tracing.Trace("End date {0} ", dateTimes.ToString());
DateTime endDate = dateTimes;
TimeSpan timeDifference = endDate - startDate;
tracing.Trace("Timespan is {0} ", timeDifference.ToString());
double minutesDiff = timeDifference.TotalMinutes;
tracing.Trace("End date time Difference {0} ", minutesDiff.ToString());
int timeDiff = Convert.ToInt32(minutesDiff);
timespanEntity.Attributes["effizz_timedifference"] = timeDiff;
int optionValue = 684200000;
OptionSetValue value = new OptionSetValue(optionValue);
timespanEntity["effizz_newstatus"] = value;
service.Update(timespanEntity);
}
//Hear I wrote code for new time span record after changing the status to On Hold in case
DateTime dateTime = new DateTime();
dateTime = DateTime.UtcNow;
tracing.Trace("Date ts {0} ", dateTime.ToString());
Entity TimeSpanEntity = new Entity("effizz_timespan");
int newoptionsetValue = 684200001;
OptionSetValue newvalue = new OptionSetValue(newoptionsetValue);
TimeSpanEntity["effizz_newstatus"] = newvalue;
TimeSpanEntity.Attributes["effizz_name"] = "TimeSpan for new case";
TimeSpanEntity.Attributes["effizz_case"] = new
EntityReference(updateTimeSpent.LogicalName, updateTimeSpent.Id);
TimeSpanEntity.Attributes["effizz_start"] = dateTime;
service.Create(TimeSpanEntity);
}
}
}
}
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
In plugin Registration for Registration a step fill this details from the below picture.
Step 8: Now go to status of the case and change the status to OnHold , Check end date is coming or not and also check difference between start time and end time is coming or no


.png)


.png)
.png)
Comments
Post a Comment