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...

5 comments:

  1. very informative and helpful post.. it really shows the hard work of Andriy.. it helped me to do customization which i thought, was not possible by supported way before. Thanx Andriy.. :)

    ReplyDelete
  2. I'm happy that this post helped you, Yatin.

    Actually this way is unsupported too. But this customization can be copied to other production using only import/export functionality.

    ReplyDelete
  3. saved a lot of my time, really great post , cheers buddy

    ReplyDelete
  4. really nice post, thanks a lot, saved me tons of time

    ReplyDelete