Tuesday, April 6, 2010

How to Prevent Closing an Opportunity

If you ever need to execute some logic when a user clicks "Close Opportunity" from the Opportunity form, then a good technique is to wrap CRM's native close javascript function with some custom javascript.

The following javascript should be ran onload of the form.

First we will declare our function that will return a boolean based on whether the user can close the Opportunity or not.

canCloseOpp = function()
     var canClose = false;

     // validate if you can close the opp or not

     return canClose; 
Then we find the "Close Opportunity" button by id and if it exists 
we will change the action property which will get executed when
the user clicks the button.  We will first call our custom function 
to see if the user can close the opportunity.  If it returns true we 
will call "complete" which is CRM's native function to close an 
opportunity.  If it returns false we will display an alert explaining
why the Opportunity can't be closed. 
var btnClose = document.getElementById('_MIcomplete');
if (btnClose)
   btnClose.action = 
      "if (canCloseOpp()) { complete(); }" +
      "else { alert('Opportunity can not be closed.'); }";

No comments:

Post a Comment