Asked by:
Error when running plugin

Question
-
I have a plugin that sets a case (incident) to resolved when a drop down (vs_casestatus) is set to 'Closed'. However, I'm getting an error re a workflow, but I can't work out what it actually is. ALl workflows to do with the Case entity have be deactivated, so I don't think it's actually to do with a workflow.
Any ideas anyone?
Here's the plugin code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; namespace MyRules { public class CaseResolution : PluginBase, IPlugin { private const int CaseStatus_Active = 0; private const int CaseStatus_Open = 717770000; private const int CaseStatus_Resolved = 717770005; private const int CaseStatus_Closed = 717770006; private const int IncidentResolutionStatus_Closed = 2; private const int IncidentStatusCode_ProblemSolved = 5; public void Execute(IServiceProvider serviceProvider) { try { PluginSetup(serviceProvider); Trace("Here 1"); if (ContextContainsRequiredImages()) { Trace("Here 2"); tracingService.Trace("Context Contains Required Images.."); tracingService.Trace(context.PrimaryEntityName); if (CaseIsBeingReactivated()) { if (CaseIsNotOpen()) MarkCaseAsOpen(); } else if (CaseIsActive()) { if (CaseStatusChangedToClosed()) { tracingService.Trace("CaseStatusChangedToResolved"); ResolveCase(); } } } Trace("Setup Complete Plugin has ended.."); //throw new InvalidPluginExecutionException(); } catch (Exception ex) { if (ex.Message.Contains("because there are open activities")) { throw new InvalidPluginExecutionException(ex.Message); } else throw new InvalidPluginExecutionException("An error occurred in the MyRules.CaseResolution.Execute plug-in: " + ex.Message + " ", ex); } } private bool CaseIsBeingReactivated() { Trace("CaseIsBeingReactivated"); bool IsReactivation = false; if (IsUpdate() && (preImage.Attributes.Contains("statecode") && ((OptionSetValue)preImage.Attributes["statecode"]).Value != CaseStatus_Active && postImage.Attributes.Contains("statecode")) && ((OptionSetValue)postImage.Attributes["statecode"]).Value == CaseStatus_Active) IsReactivation = true; Trace(IsReactivation.ToString()); return IsReactivation; } private bool CaseIsNotOpen() { return (postImage.Attributes.Contains("vs_casestatus") && ((OptionSetValue)postImage.Attributes["vs_casestatus"]).Value != CaseStatus_Open); } private void MarkCaseAsOpen() { try { Trace("Going to Try and Reactivate"); Entity incident = new Entity("incident"); incident["incidentid"] = postImage.Id; incident["vs_casestatus"] = new OptionSetValue(CaseStatus_Open); service.Update(incident); Trace("Reactivate Completed"); } catch (Exception ex) { throw new InvalidPluginExecutionException("CloseIncidentProblem" + ex.Message, ex); } } private bool CaseIsActive() { return ((postImage.Attributes.Contains("statecode")) && ((OptionSetValue)postImage.Attributes["statecode"]).Value == CaseStatus_Active); } //private bool CaseStatusChangedToResolved() private bool CaseStatusChangedToClosed() { const string MethodName = "CaseStatusChangedToResolved"; TraceMethodStart(MethodName); bool CaseStatusSetToResolved = false; if (context.MessageName == "Create") { Trace("Processing Create"); if ((postImage.Attributes.Contains("vs_casestatus")) && ((OptionSetValue)postImage.Attributes["vs_casestatus"]).Value == CaseStatus_Closed) CaseStatusSetToResolved = true; } else if (context.MessageName == "Update") { Trace("Processing Update"); if ( (preImage.Attributes.Contains("vs_casestatus")) && ((postImage.Attributes.Contains("vs_casestatus")) && (preImage.Attributes["vs_casestatus"] != postImage.Attributes["vs_casestatus"]) && ((OptionSetValue)postImage.Attributes["vs_casestatus"]).Value == CaseStatus_Closed)) CaseStatusSetToResolved = true; } TraceMethodExit(MethodName); Trace("CaseStatusSetToResolved" + CaseStatusSetToResolved); return CaseStatusSetToResolved; } /// <summary> /// Resolve the case(incident) - default the subject and the timespent /// </summary> /// <param name="service"></param> /// <param name="entity"></param> private void ResolveCase() { Trace("Starting Resolve Case"); Guid IncidentId = postImage.Id; Trace("PostImage Logical Name = " + postImage.LogicalName); Trace("Case Id = " + IncidentId); int timespent = CalculateTimeSpentInMinutes(); Trace("Time Spent = " + timespent); CloseIncident(IncidentId, timespent); } private void CloseIncident(Guid IncidentId, int timespent) { const string MethodName = "CloseIncident"; TraceMethodStart(MethodName); Trace("IncidentId: " + IncidentId); Trace("TimeSpentInMinutes: " + timespent); try { Entity resolution = new Entity("incidentresolution"); resolution["subject"] = "Case Resolved"; resolution["incidentid"] = new EntityReference("incident", IncidentId); resolution["timespent"] = timespent; resolution["statuscode"] = new OptionSetValue(IncidentResolutionStatus_Closed); CloseIncidentRequest closeincidentRequest = new CloseIncidentRequest() { IncidentResolution = resolution, Status = new OptionSetValue((int)IncidentStatusCode_ProblemSolved) }; string test = resolution.Attributes["subject"].ToString(); Trace("subject: " + test); test = resolution.Attributes["incidentid"].ToString(); Trace("Incident Id: " + test); test = resolution.Attributes["timespent"].ToString(); Trace("timespent: " + test); test = resolution.Attributes["statuscode"].ToString(); Trace("statuscode: " + test); test = closeincidentRequest.IncidentResolution.Id.ToString(); Trace("closeincidentRequest.IncidentResolution: " + test); test = closeincidentRequest.Status.Value.ToString(); Trace("closeincidentRequest.Status: " + test); service.Execute(closeincidentRequest); } catch (Exception ex) { if (ex.Message.Contains("because there are open activities")) { throw new InvalidPluginExecutionException(ex.Message); } else throw new InvalidPluginExecutionException("CloseIncidentProblem: " + ex.Message, ex); } TraceMethodExit(MethodName); } private int CalculateTimeSpentInMinutes() { const string MethodName = "CalculateTimeSpentInMinutes"; TraceMethodStart(MethodName); int timeInMinutes = 0; //if ((postImage.Attributes.Contains("vs_timeframenumberpart")) && // (postImage.Attributes.Contains("vs_timeframedurationtypepart"))) //{ // tracingService.Trace("CaseResolutionPlugin - Required Attributes Exist"); // int timeframe = ((OptionSetValue)postImage.Attributes["vs_timeframedurationtypepart"]).Value; // decimal timeframenumberpart = (decimal)postImage.Attributes["vs_timeframenumberpart"]; // switch (timeframe) // { // case (int)TimeFrame.Minutes: // timeInMinutes = (int)Math.Round(timeframenumberpart); // break; // case (int)TimeFrame.Hours: // timeInMinutes = (int)Math.Round(timeframenumberpart * 60); // break; // default: // break; // } //} Trace("TimeSpent" + timeInMinutes); TraceMethodExit(MethodName); return timeInMinutes; } private bool ContextContainsRequiredImages() { if ((IsUpdate() && PreImage != null && PostImage != null) || (IsCreate() && PostImage != null)) { return true; } else return false; } } }
Here's the log file:
Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: This workflow job was canceled because the workflow that started it included an infinite loop. Correct the workflow logic and try again. For information about workflow logic, see Help. Detail: <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts"> <ErrorCode>-2147220891</ErrorCode> <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic"> <KeyValuePairOfstringanyType> <d2p1:key>OperationStatus</d2p1:key> <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value> </KeyValuePairOfstringanyType> </ErrorDetails> <Message>An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: An error occurred in the MyRules.CaseResolution.Execute plug-in: CloseIncidentProblem: This workflow job was canceled because the workflow that started it included an infinite loop. Correct the workflow logic and try again. For information about workflow logic, see Help. </Message> <Timestamp>2013-09-20T11:25:23.4407166Z</Timestamp> <InnerFault i:nil="true" /> <TraceText> [Case: MyRules.CaseResolution] [b0eb6fb9-e221-e311-b6b4-3c4a92dbdc39: MyRules.CaseResolution: Update of incident] </TraceText> </OrganizationServiceFault>
Thanks.Friday, September 20, 2013 11:54 AM
All replies
-
Put the logic inside this statement:-
if (context.Depth == 1)
{//Put Logic here
}
Regards Faisal
Friday, September 20, 2013 1:17 PM