I have written a few scripts for CRM and I have experience with C#.
I am trying to write a plugin in the SDK. I have to conditions that I need to check in an Order entity. If neither of those conditions are met, the next step is to check a Product status in the Order Product entity, alert the user to the problem and not the let the user save until the issue is resolved. I feel like I am close, but I am missing something. Any help iis appreciated. Thank you in advance.
// <copyright file="PreOrderUpdate.cs" company=""> // Copyright (c) 2014 All Rights Reserved // </copyright> // <author></author> // <date>5/9/2014 11:39:16 AM</date> // <summary>Implements the PreOrderUpdate Plugin.</summary> // <auto-generated> // This code was generated by a tool. // Runtime Version:4.0.30319.1 // </auto-generated> using System.Windows.Forms; using System.ServiceModel; using Microsoft.Xrm.Sdk; namespace NABC_CRM_Plugins { /// <summary> /// PreOrderUpdate Plugin. /// Fires when the following attributes are updated: /// xrm_orderstagecode,xrm_ordertype /// </summary> public class PreOrderUpdate : Plugin { /// <summary> /// Alias of the image registered for the snapshot of the /// primary entity's attributes before the core platform operation executes. /// The image contains the following attributes: /// xrm_orderstagecode,xrm_ordertype /// </summary> private const string PreImageAlias = "preorder"; /// <summary> /// Initializes a new instance of the <see cref="PreOrderUpdate"/> class. /// </summary> public PreOrderUpdate() : base(typeof (PreOrderUpdate)) { base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(20, "Update","salesorder", new Action<LocalPluginContext>(ExecutePreOrderUpdate))); // Note : you can register for more events here if this plugin is not specific to an individual entity and message combination. // You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change. } /// <summary> /// Executes the plug-in. /// </summary> /// <param name="localContext">The <see cref="Plugin.LocalPluginContext"/> which contains the /// <see cref="IPluginExecutionContext"/>, /// <see cref="IOrganizationService"/> /// and <see cref="ITracingService"/> /// </param> /// <remarks> /// For improved performance, Microsoft Dynamics CRM caches plug-in instances. /// The plug-in's Execute method should be written to be stateless as the constructor /// is not called for every invocation of the plug-in. Also, multiple system threads /// could execute the plug-in at the same time. All per invocation state information /// is stored in the context. This means that you should not use global variables in plug-ins. /// </remarks> protected void ExecutePreOrderUpdate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(PreImageAlias)) ? context.PreEntityImages[PreImageAlias] : null; var orderType = Xrm.Page.getAttribute("xrm_ordertype").getValue(); var orderStateCode = Xrm.Page.getAttribute("statecode").getValue(); int totalCount; string fetchXml = null; if (orderType == "Fundraising" || orderStateCode == "Fufilled") return; try { { strDebug = "101"; Entity orderEntity = service.Retrieve("salesorderdetail", orderId, new ColumnSet(new string[] {"salesorderid", "New_Status"})); EntityCollection unresolvedProductCount = service.RetrieveMultiple(new FetchExpression(fetchXml)); if (unresolvedProductCount.Entities.Count > 0) { totalCount = ((AliasedValue) unresolvedProductCount.Entities[0]["product_count"]).Value; } fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical'distinct='false'>"; fetchXml += "<entity name='salesorderdetail'>"; fetchXml += "<attribute name='productid'/>"; fetchXml += "<attribute name='productdescription'/>"; fetchXml += "<attribute name='priceperunit'/>"; fetchXml += "<attribute name='quantity'/>"; fetchXml += "<attribute name='extendedamount'/>"; fetchXml += "<attribute name='alesorderdetailid'/>"; fetchXml += "<order attribute='productid' descending='false'/>"; fetchXml += "<filter type=and>"; fetchXml += "<condition attribute='new_status' operator='in'>"; fetchXml += "<value>1</value>"; fetchXml += "<value>163650000</value>"; fetchXml += "</condition>"; fetchXml += "</filter>"; fetchXml += "</entity>"; fetchXml += "</fetch>"; unresolvedProductCount = service.RetrieveMultiple(new FetchExpression(fetchXml)); if (unresolvedProductCount.Entities.Count > 0) MessageBox.Show(string.Format("Please up date all product status before continuing")); throw new InvalidPluginExecutionException("Total: "); } } catch (e) { consoleLog("Error in " + e.message); } } } }