Bhoopathi

"Be Somebody Nobody Thought You could Be"

Tuesday, March 21


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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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;
}