locked
Prevent Close Opportunity RRS feed

  • Question

  • Hi all,

    Is there a way to prevent user to close opportunity if some certain conditions are not met without removing the Close Opportunity action?

    I need to pop up an alert that told the user to fixed the opportunity when the condition are not met, if the condition are met then it will pop up the Close opportunity web page dialog when clicking Close Opportunity from Action..

     

    Thank you for any help..

    Thursday, April 1, 2010 8:29 AM

Answers

  • try this:

    function canCloseOpp()
    {
           var canClose = false;
    
           // validate if you can close the opp or not
    
           return canClose; 
    }
    
    
    var btnClose = document.getElementById('_MIcomplete');
    if (btnClose)
    {
       btnClose.action = "if (canCloseOpp()) { complete(); } else { alert('Opportunity can not be closed.'); }";
    }

    This will change the javascript of the native "Close Opportunity" button and will first call the custom function canCloseOpp where you validate if the Opp can be closed or not.  If it can then it will call CRM's native close function "complete".  If it can't be closed then you can show a custom alert message.  You will want to run this code onload.

    Blake Scarlavai - Sonoma Partners - http://www.sonomapartners.com/
    • Proposed as answer by Shahid_Hameed Tuesday, April 6, 2010 11:00 AM
    • Marked as answer by Jim Glass Jr Tuesday, April 6, 2010 2:46 PM
    Monday, April 5, 2010 12:53 PM
  • try this:

    function canCloseOpp()
    
    {
    
           var canClose = false;
    
    
    
           // validate if you can close the opp or not
    
    
    
           return canClose; 
    
    }
    
    
    
    
    
    var btnClose = document.getElementById('_MIcomplete');
    
    if (btnClose)
    
    {
    
       btnClose.action = "if (canCloseOpp()) { complete(); } else { alert('Opportunity can not be closed.'); }";
    
    }
    
    

    This will change the javascript of the native "Close Opportunity" button and will first call the custom function canCloseOpp where you validate if the Opp can be closed or not.  If it can then it will call CRM's native close function "complete".  If it can't be closed then you can show a custom alert message.  You will want to run this code onload.

    Blake Scarlavai - Sonoma Partners - http://www.sonomapartners.com/


    Blake Scarlavai solution was realy good for overwriting Action Menu Items action.
    Just a small change required in function declaration.

    canCloseOpp = function ()
    {
           var canClose = false;
           // validate if you can close the opp or not
           return canClose;   
    }
    
    var btnClose = document.getElementById('_MIcomplete');
    if (btnClose)
    {
       btnClose.action = "if (canCloseOpp()) { complete(); } else { alert('Opportunity can not be closed.'); }";
    }

    http://www.allaboutdynamics.com
    • Marked as answer by -Pingkan- Wednesday, April 7, 2010 1:08 AM
    Tuesday, April 6, 2010 11:04 AM
  • Though you won't be able to prevent the "Close" dialog from appearing, you could perform your validation in a Plugin that is attached to the "Win/Loss" messages of the Opportunity record.  This is ideal, since the Opportunity can be closed automatically as a process of closing a child Quote record.  This, naturally, is only a concern if you use Quotes within Opportunities.


    Dave Berry
    • Marked as answer by Jim Glass Jr Tuesday, April 6, 2010 2:46 PM
    Monday, April 5, 2010 6:54 PM
    Moderator

All replies

  • With JavaScript, you can now which action as been triggered in the onSave event. A close opportunity event is like a deactivate I think...

     

    Check my blog to have the action list: http://mscrmtools.blogspot.com/2009/01/jscript-know-which-action-raised-onsave.html


    My blog : http://mscrmtools.blogspot.com
    You will find:
    Bulk Delete LauncherView Layout replicator
    ISV.Config Manager Form Javascript Manager
    Assembly RecoveryAnd others (use tool tag on my blog)
    Thursday, April 1, 2010 8:33 AM
    Moderator
  • event.Mode will be 5 when an Opportunity is closed (Won \ Lost).
    You can cehck your condition with event.mode to prevent it from saving like below

    if(event.Mode == 5)
    {
     if(<<Condition>>)
     {
       // Cancel the save
     event.returnValue = false;
     return false;
     }
    }

    Regards
    Vinoth

    Thursday, April 1, 2010 8:46 AM
  • event.Mode will be 5 when an Opportunity is closed (Won \ Lost).
    You can check your condition with event.mode to prevent it from saving like below

    if(event.Mode == 5)
    {
     if(<<Condition>>)
     {
     // Cancel the save
     event.returnValue = false;
     return false;
     }
    }

    Regards
    Vinoth

    Thursday, April 1, 2010 8:53 AM
  • Thank you for your quick response..

    But could you explain more about it..

    I want to prevent the Close Opportunity dialog to show up if the conditions are not met..

    I have been succeeded validate the opportunity when onsave event

    but there were some problem when user edit the opportunity then choose Action -> Close Opportunity without saving it first..

    And after user close the opportunity it will automatically trigger a workflow to run..

    I want to validate the opportunity form first when user choose Action -> Close Opportunity before showing up the Close Opportunity web page dialog..

    Thursday, April 1, 2010 9:01 AM
  • I want to validate the opportunity form first when user choose Action -> Close Opportunity before showing up the Close Opportunity web page dialog..

    I hope there is no supporteed way to perform this validation.

    You can validate whether your conditions are met in the OnSave event. If they are not met you can prevent the user from closing the opportunity, hence your workflow wont run since opportunity is not closed. You can also intimate the user that he has not met the requirement with the alert message.

     

    Thursday, April 1, 2010 9:09 AM
  • Hi,

    I hope the easiest way is to hide "Close Opportunity" option if your validation fails,

     


    Mahain
    Thursday, April 1, 2010 9:11 AM
    Moderator
  • i have think about it too, but my supervisor said that the user will be confuse if they want to close the opportunity and the option is not there..


    Best regards, -- Pingkan --
    Thursday, April 1, 2010 9:39 AM
  • I've tried it and it didn't work..

    Please correct me if my code is wrong,

    because I've put some code in my onSave event like this, i then add case 5 so the code like this:

     

    switch(event.Mode)

    {

      case 1:

                 if(<<Condition>>)
                 {

                     alert("a");

                     // Cancel the save
                     event.returnValue = false;
                     return false;
                  }

                 break;

       case 2:

                 if(<<Condition>>)
                 {

                     alert("a");

                     // Cancel the save
                     event.returnValue = false;
                     return false;
                  }

                  break;

        case 5:

                 if(<<Condition>>)
                 {

                     alert("a");

                     // Cancel the save
                     event.returnValue = false;
                     return false;
                  }

                  break;

    }

    but it didn't work for the case 5, when save and save and close it still work


    Best regards, -- Pingkan --
    Best regards,
    -- Pingkan --
    Thursday, April 1, 2010 9:41 AM
  • Ok,

    In that case you can try to creae another dummy "Close Opportunity" option in Action menu.

    For example if your validation fails Hide actual "Close Opportunity" link from action menu and show your dummy "Close Opportunity" which will not do anything otherwise don't show ur dummy link

    Does it help !!!!


    Mahain
    Thursday, April 1, 2010 10:03 AM
    Moderator
  • PingKanM, 

    start by putting these line in your onSave event to know wich mode is used

    alert(event.Mode);

    Then try your switch case statement with this value


    My blog : http://mscrmtools.blogspot.com
    You will find:
    Bulk Delete LauncherView Layout replicator
    ISV.Config Manager Form Javascript Manager
    Assembly RecoveryAnd others (use tool tag on my blog)
    • Marked as answer by Jim Glass Jr Thursday, April 1, 2010 5:55 PM
    • Unmarked as answer by -Pingkan- Monday, April 5, 2010 2:14 AM
    Thursday, April 1, 2010 10:13 AM
    Moderator
  • I've tried it..

    And the event mode is 5 but the event run when user click ok on close webpage dialog..

    It keeps trigger the workflow and Close the Opportunity..

    Is there someway to catch the event when user click the Close Opportunity action?


    Best regards,
    -- Pingkan --
    Monday, April 5, 2010 2:13 AM
  • try this:

    function canCloseOpp()
    {
           var canClose = false;
    
           // validate if you can close the opp or not
    
           return canClose; 
    }
    
    
    var btnClose = document.getElementById('_MIcomplete');
    if (btnClose)
    {
       btnClose.action = "if (canCloseOpp()) { complete(); } else { alert('Opportunity can not be closed.'); }";
    }

    This will change the javascript of the native "Close Opportunity" button and will first call the custom function canCloseOpp where you validate if the Opp can be closed or not.  If it can then it will call CRM's native close function "complete".  If it can't be closed then you can show a custom alert message.  You will want to run this code onload.

    Blake Scarlavai - Sonoma Partners - http://www.sonomapartners.com/
    • Proposed as answer by Shahid_Hameed Tuesday, April 6, 2010 11:00 AM
    • Marked as answer by Jim Glass Jr Tuesday, April 6, 2010 2:46 PM
    Monday, April 5, 2010 12:53 PM
  • Though you won't be able to prevent the "Close" dialog from appearing, you could perform your validation in a Plugin that is attached to the "Win/Loss" messages of the Opportunity record.  This is ideal, since the Opportunity can be closed automatically as a process of closing a child Quote record.  This, naturally, is only a concern if you use Quotes within Opportunities.


    Dave Berry
    • Marked as answer by Jim Glass Jr Tuesday, April 6, 2010 2:46 PM
    Monday, April 5, 2010 6:54 PM
    Moderator
  • try this:

    function canCloseOpp()
    
    {
    
           var canClose = false;
    
    
    
           // validate if you can close the opp or not
    
    
    
           return canClose; 
    
    }
    
    
    
    
    
    var btnClose = document.getElementById('_MIcomplete');
    
    if (btnClose)
    
    {
    
       btnClose.action = "if (canCloseOpp()) { complete(); } else { alert('Opportunity can not be closed.'); }";
    
    }
    
    

    This will change the javascript of the native "Close Opportunity" button and will first call the custom function canCloseOpp where you validate if the Opp can be closed or not.  If it can then it will call CRM's native close function "complete".  If it can't be closed then you can show a custom alert message.  You will want to run this code onload.

    Blake Scarlavai - Sonoma Partners - http://www.sonomapartners.com/


    Blake Scarlavai solution was realy good for overwriting Action Menu Items action.
    Just a small change required in function declaration.

    canCloseOpp = function ()
    {
           var canClose = false;
           // validate if you can close the opp or not
           return canClose;   
    }
    
    var btnClose = document.getElementById('_MIcomplete');
    if (btnClose)
    {
       btnClose.action = "if (canCloseOpp()) { complete(); } else { alert('Opportunity can not be closed.'); }";
    }

    http://www.allaboutdynamics.com
    • Marked as answer by -Pingkan- Wednesday, April 7, 2010 1:08 AM
    Tuesday, April 6, 2010 11:04 AM
  • Thanks for that catch.

    Blake Scarlavai - Sonoma Partners - http://www.sonomapartners.com/
    Tuesday, April 6, 2010 3:02 PM
  • Hi all,

    Thank you all for your answer..

    I had try it and it works :)


    Best regards,
    -- Pingkan --
    Wednesday, April 7, 2010 1:08 AM
  • Anrdriy Butenko has published a very much complementary article to what is discussed in this thread.

    http://a33ik.blogspot.com/2010/06/extended-handling-of-save-event-in.html

    Refer to his blog for great info on how to extend the validation and functionality at close-opportunity event.

    He published in response to my follow-up question posted here: http://social.microsoft.com/Forums/en/crmdevelopment/thread/f513e04b-0c04-4494-b8f1-3da33ef36767


    mrbello
    Sunday, June 20, 2010 4:10 PM