Answered by:
Plugin not updating Date Time Field using service.Update...

Question
-
Hi everyone.
I have 3 custom fields. Paused Date, Resume Date and SLA date.
These values are populated using JS via a button.
I want to recalculate the SLA date based on the date/time difference of the Pause and Resume date.Plugin runs on Post-Operation, synchronous update.
namespace CalculatePausedTime { using System; using System.ServiceModel; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; public class CalculatePauseDate : IPlugin { public void Execute(IServiceProvider serviceProvider) { //Obtain the execution context from the service provider. IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); //Get a reference to the organisation service. IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = factory.CreateOrganizationService(context.UserId); // Get a reference to the tracing service. ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); try { Entity entity = (Entity)context.InputParameters["Target"]; Entity preImage = (Entity)context.PreEntityImages["preImage"]; Entity postImage = (Entity)context.PostEntityImages["postImage"]; if (context.Depth > 1) { return; } tracingService.Trace("Pre Image and Post Image Contains Status code"); OptionSetValue waitForInfo = preImage.Attributes["statuscode"] as OptionSetValue; OptionSetValue resumeNew = postImage.Attributes["statuscode"] as OptionSetValue; if (waitForInfo.Value == 345550005 && resumeNew.Value != 345550005) { //Get Time of Paused Date DateTime pauseDate = (DateTime)preImage.Attributes["apitil_pausedate"]; tracingService.Trace("Pause Date = " + pauseDate); //Get Time of Resume Date DateTime resumeDate = (DateTime)entity.Attributes["apitil_resumedate"]; tracingService.Trace("resumeDate = " + resumeDate); //Get Time of the SLA Due date DateTime slaDueDate = (DateTime)preImage.Attributes["apitil_sladuedate"]; tracingService.Trace("slaDueDate = " + slaDueDate); //Check the time difference between 2 dates TimeSpan span = resumeDate - pauseDate; tracingService.Trace("span= " + span); //Add the Difference to the SLA Due date slaDueDate += span; tracingService.Trace("New SLA Due Date= " + slaDueDate); service.Update(entity); } } catch (Exception e) { throw new InvalidPluginExecutionException("Error" + e); } } } }
It seems like when I debug the code it seems to be working fine and the slaDueDate variable contains the new Date.
But for some reason it is not updating the field?#
Here a pic of the tracing.. I have added a throw Invalidpluginexecution at the end of my code to see the trace.
I have removed this line when I try to run the plugin properly.
As you can see I have a new SLA Due date.... but it is not updating my field... I must be doing something stupid.
Anyone shed some light? Sorry I am still kind of new doing C# and plugin development.
Many Thanks.
Dave
Saturday, December 7, 2013 3:21 PM
Answers
-
Hi,
You need to add new SLA Due date to entity object like :
slaDueDate += span;
tracingService.Trace("New SLA Due Date= " + slaDueDate);entity.Attributes["new_sladuedate"] = slaDueDate;
service.Update(entity);
Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !!!
Vikram Singh. !!! My Blog- Marked as answer by davdatong Sunday, December 8, 2013 12:28 AM
Saturday, December 7, 2013 3:58 PM
All replies
-
Hi,
You need to add new SLA Due date to entity object like :
slaDueDate += span;
tracingService.Trace("New SLA Due Date= " + slaDueDate);entity.Attributes["new_sladuedate"] = slaDueDate;
service.Update(entity);
Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !!!
Vikram Singh. !!! My Blog- Marked as answer by davdatong Sunday, December 8, 2013 12:28 AM
Saturday, December 7, 2013 3:58 PM -
Hi,
Many thanks for this! I knew I missed out something!!
Works now!
Kind regards,
Dave
Sunday, December 8, 2013 12:28 AM