I have written a plugin based on Quotes entity that only fires on records returned by a query, however I get failed system jobs against quote records that i know do not meet the query in the plugin
The error I receive is:
Unexpected exception from plug-in (Execute): ClosePreviousWestQuote.ClosePreviousWestQuoteV2Plugin: System.NullReferenceException: Object reference not set to an instance of an object.
The plugin does work however, I just want to know why the plugin doesnt just skip if the record doesnt meet the query criteria?
Here's my code:
using System; using System.ServiceModel; using Microsoft.Crm.Sdk.Messages; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; namespace ClosePreviousWestQuote { public class ClosePreviousQuoteV2Plugin : IPlugin { public void Execute(IServiceProvider serviceProvider) { // Extract the tracing service for use in debugging sandboxed plug-ins. ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); // Obtain the execution context from the service provider. IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); // The InputParameters collection contains all the data passed in the message request. if (context.InputParameters.Contains("Target")&&context.InputParameters["Target"] is Entity) { // Obtain the target entity from the input parameters. Entity entity = (Entity)context.InputParameters["Target"]; // get the quote number and quote version from the quote record string new_quote_number = entity.GetAttributeValue("vendor_quote_number"); string new_quote_ref = entity.GetAttributeValue("quote_ref"); int new_quote_version = entity.GetAttributeValue("version_number"); bool startsWithWQ = new_quote_ref.StartsWith("WQ-"); // only fire this plugin if the entity is a quote and vendor quote number is not null and the version number is greater than 1 if (entity.LogicalName == "quote" && new_quote_number != null && startsWithWQ == true && new_quote_version > 1) { // Obtain the organization service reference which you will need for web service calls. IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); try { // search for quotes with the same vendor quote number, the quote version is less than the newest quote version & alos they are not closed string fetchxml = @" "; EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchxml)); foreach (var c in result.Entities) { if (result != null && result.Entities.Count > 0) { foreach (Entity _entity in result.Entities) { string QuoteID = c.Attributes["quoteid"].ToString(); //string QuoteVersion = c.Attributes["version_number"].ToString(); int QuoteStatus = ((OptionSetValue)(c.Attributes["statecode"])).Value; Guid _QuoteId = new Guid(QuoteID); // check the quote is not closed already if (QuoteStatus != 3) { // Create the Request Object SetStateRequest setStateRequest = new SetStateRequest(); // In my case i'm Cancelling Task Activity var quoteReference = new EntityReference("quote", _QuoteId); try { // "statecode" Closed = "3" // "statuscode" Revised = "7" // Set the State and Status OptionSet Values to Closed / Revised CloseQuoteRequest closeQuoteRequest = new CloseQuoteRequest(); Entity QuoteClose = new Entity("quoteclose"); QuoteClose.Attributes["quoteid"] = new EntityReference("quote", _QuoteId); QuoteClose.Attributes["subject"] = "This quote has been closed due to a new version " DateTime.Now.ToString(); closeQuoteRequest.QuoteClose = QuoteClose; closeQuoteRequest.Status = new OptionSetValue(7); // Execute the Response service.Execute(closeQuoteRequest); } catch (Exception ex) { tracingService.Trace("WestQuoteV2Plugin: {0}", ex.ToString()); throw; } } else { return; } } } // skip plugin if no results else { return; } } } catch (FaultException ex) { throw new InvalidPluginExecutionException("An error occurred in QuoteV2Plugin.", ex); } catch (Exception ex) { tracingService.Trace("QuoteV2Plugin: {0}", ex.ToString()); throw; } } // skip plugin if it doesnt match else { return; } } else { return; } } } }