I've got a plugin that is triggered when a status on a form is changed. When I change the status and click save on the form, I'm getting an 'Object reference not set to an instance of an object' error. I have tracing set up, and have trace messages in my code, but I'm not even seeing the first trace message in the resulting log file.
The plugin in registered for create & update. The create step has a post image and is a post-operation, and the update step has a pre & post image and is also a post-operation.
Any ideas?
Thanks.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; namespace MyRules { public class StatusChange : PluginBase, IPlugin { public enum formStatus { InProcess = 717770000, SentForApproval = 717770001, Approved = 717770002, Rejected = 717770003 } public void Execute(IServiceProvider serviceProvider) { try { // I'm not even seeing this trace message in the log file Trace("Here 1"); if (ContextContainsRequiredImages()) { Trace("Here 2"); // Get updated status // If it's 'Sent for Approval', email is sent if (postImage.Attributes.Contains("new_status")) { Trace("postImage.Attributes.Contains('new_status')"); Trace("postImage['new_status'].ToString()" + postImage["new_status"].ToString()); if (postImage["vs_status"].ToString() == formStatus.SentForApproval.ToString()) { Trace("Here 3"); // Send email x } else if (postImage["new_status"].ToString() == formStatus.Approved.ToString()) { Trace("Here 4"); // Send email y } } } throw new InvalidPluginExecutionException(); } catch (Exception ex) { throw new InvalidPluginExecutionException("An error occurred in the formStatus.StatusChange.Execute plug-in." + ex.Message, ex); } } private bool ContextContainsRequiredImages() { if ((IsUpdate() && PreImage != null && PostImage != null) || (IsCreate() && PostImage != null)) { return true; } else return false; } } }
Here's the error log:
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.StatusChange: .StatusChange.Execute plug-in.Object reference not set to an instance of an object.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 formStatus.StatusChange.Execute plug-in.Object reference not set to an instance of an object.</Message><Timestamp>2013-06-28T10:53:33.7969759Z</Timestamp><InnerFault i:nil="true" /><TraceText> [MyRules: MyRules.StatusChange] [012e3533-3fdf-e211-8c38-3c4a92dbc855: MyRules.StatusChange: Create of new_assessmentform] </TraceText></OrganizationServiceFault>