Validate Opportunity CloseDate when it is closed as won or lost in Dynamics CRM 2011

Create a Plugin step with following details
Message: Create
Primary Entity: opportunityclose
Execution Stage: Pre-operation
Execution Mode: Synchronous


// Validate Opportunity CloseDate for preventing Opportunity closed as won or lost in the future.
 public void Execute(IServiceProvider serviceProvider)
        {
            try
            {
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

                if (context.Stage == 20) // Pre-Stage
                {
                    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                    {
                        Entity entity = (Entity)context.InputParameters["Target"];

                        if (entity.Attributes.Contains("actualend") && entity.Attributes["actualend"] != null)
                        {
                            DateTime closedOn = entity.GetAttributeValue("actualend");

                            if (closedOn.Date > DateTime.Now.Date)
                            {
                                throw new InvalidPluginExecutionException(OperationStatus.Canceled, "Close Date cannot be in the future");
                            }
                        }
                    }
                }
            }
            catch (InvalidPluginExecutionException ex)
            {
                throw ex;
            }
        }

We can also use following JavsScript code to validate Opportunity CloseDate when it is closed as Won or Lost. Call ValidateOpportunityCloseDate() in the OnSave method. In fact, you can use this code to validate other fields(Status Reason, Actual Revenue, Close Date, Competitor) on Close Opportunity dialog.


function ValidateOpportunityCloseDate() {
    var arry = GetCloseOpportunityInformation();
    if (arry[3] != undefined && arry[3] != null) {
        var today = new Date();
        var formattedDate = ('' + arry[3]).replace(/-/g, "/").replace(/[TZ]/g, " ");
        var actualend = new Date(formattedDate);
        if (actualend > today) {
            alert("Close Date cannot be in the future");
            AbortSave();
        }
    }
}
function GetCloseOpportunityInformation() {
    var arr = new Array();
    var arrFields = new Array();
    arrFields[0] = 'statecode';  //1 = Won, 2 = Lost    
    arrFields[1] = 'statuscode'; //Status Reason   
    arrFields[2] = 'actualrevenue'; //Actual Revenue   
    arrFields[3] = 'actualend';  //Close Date   
    arrFields[4] = 'competitorid'; //Competitor 
    arrFields[5] = 'description'; //Description  
    var xml = crmFormSubmit.crActivityXml.value;
    var XmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    XmlDoc.async = false;
    XmlDoc.loadXML(xml);
    for (var i = 0; i < arrFields.length; i++) {
        //get close out information     
        if (i > 1) {
            var xmlnode = XmlDoc.selectSingleNode("//opportunityclose/" + arrFields[i]);
            if (xmlnode != null) {
                arr[i] = xmlnode.nodeTypedValue;
            } else {

                arr[i] = "";
            }
        }
    }
    return arr;
}
function AbortSave() {
    event.returnValue = false;
    return false;
}
Advertisement

2 thoughts on “Validate Opportunity CloseDate when it is closed as won or lost in Dynamics CRM 2011

  1. What if we want to make sure certain fields in the opportunity form have values before the opportunity is closed via create order dialog through Quotes?
    Thanks in advance

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s