Sunday, June 20, 2010

Extended handling of save event in forms with close dialog (like opportunitites)

All developers who developed JavaScripts for CRM read this article. But there is no info about getting data from fields of "Close dialogue" (like opportunity close). I've made a little research on this theme and here is the result:


First of all - when you close opportunity event.Mode equals 5 (Deactivate event mode). New state, new status and close info you can get using following scripts placed in OnSave event handler of opportunity form, save form, publish entity:

if (event.Mode == 5)
{
alert(crmFormSubmit.crNewState.value + '\n' + crmFormSubmit.crNewStatus.value + '\n' + crmFormSubmit.crActivityXml.value);
}


Create and close opportunity with won status:



You will see following result:



If you will try to close opportunity with lost status:



You will get following result:



How this can be used - for example you want make Description field required in the case when opportunity is won and competitor field when opportunity is lost. Put following script to OnSave event handler of opportunity form:

if (event.Mode == 5)
{
var status = crmFormSubmit.crNewState.value;
var xml = crmFormSubmit.crActivityXml.value;
var XmlDoc = new ActiveXObject("Microsoft.XMLDOM");
XmlDoc.async = false;
XmlDoc.loadXML(xml);

//If status is won
if (status == 1)
{
var descriptionnode = XmlDoc.selectSingleNode("//opportunityclose/description");
if (descriptionnode == null || descriptionnode.nodeTypedValue == "")
{
alert("Opportunity can't be closed!\nFill description field in Close Opportunity Dialogue");
event.returnValue = false;
return false;
}
}
else if (status == 2)
{
var competitornode = XmlDoc.selectSingleNode("//opportunityclose/competitorid");
if (competitornode == null || competitornode.nodeTypedValue == "")
{
alert("Opportunity can't be closed!\nFill competitor field in Close Opportunity Dialogue");
event.returnValue = false;
return false;
}
}
}


Save form and publish opportunity entity. Then try to close opportunity.
Won scenario:




Lost Scenario:




This customizations are not documented so it seems that this is unsupported approach to customization...