locked
CRM 2013 - Stop the save event from a custom ribbon button RRS feed

  • Question

  • Good morning,

    I'm using a standard button named "Activate Quote" to activate quotes.

    When a specific value on a field form of quote entity is selected, i wan't to show a pop-up "You have to choose x value to activate quote" and i want to stop the save event from a script that is executed only on the click event of "Activate Quote" button.

    When i use this code :

    function AccessActivateDevisOption(eContext) {
        var montanttotal = Xrm.Page.getAttribute("sch_montanttotal").getValue();
        var remiseTotal = Xrm.Page.getAttribute("totallineitemdiscountamount").getValue();
        if (montanttotal <= 2000 && remiseTotal <= 20) {
            alert("Vous ne pouvez pas valider le devis. Un devis ne peut-être activé que lorsque le montant du devis dépasse les 2000€ ou lorsque la remise total du devis représente plus de 20%.");
            Xrm.Page.context.getEventArgs.preventDefault();
        }
    }

    I get an error "eContext" is undefined.

    I assume that's because i'm calling the script from ribbon and not from form.

    How can i stop the save event from a ribbon button click event ?

    Thank you very much for your help.

    Regards,

    Jean-Michel


    • Edited by DOS-SANTOS Tuesday, July 1, 2014 4:55 PM
    Tuesday, July 1, 2014 4:27 PM

All replies

  • Hi,

    You have two options:

    • Make your "x" field mandatory, so user has to provide a value for the Quote to be activated, saved, etc.
    • If all you need to do using your custom button is check whether the "x" field value has been provided, I would recommend doing the same on Form OnSave. Use the following code in that case:

    function preventActivation(econtext)
    {
    	if (econtext.eventArgs.getSaveMode() == 29 || econtext.eventArgs.getSaveMode() == 6) 
    	{
    		var fieldVal = Xrm.Page.getAttribute("x").getValue();
    		
    		if (fieldVal == null)
    		{
    			eventArgs.preventDefault();
    		}
    	}
    }


    Admin QuikView Solution for CRM 2013



    Wednesday, July 2, 2014 5:28 AM
  • Hi Dynamotion,

    Thanks for your help. 

    To give you more details, i want to stop the "activate Quote" when the total amount of the quote is < 2000€.  This price is automatically calculated when you add, modify or delete Quote details. 

    I don't have problem with the add or delete event of quote details because when you add or delete a quote details, the quote is saved to calculate the total amount, so i can do a refresh of the ribbon and put a enable rule to disable the button after the refresh.

    The problem is when i modify a quote details because this will not save or refresh the quote. I have to refresh the quote manually. For exemple :

    > Step 1 : I add a quote detail by the subgrid with an amount of 10000€ so the quote will be saved to calculate the total amount and the ribbon is refreshed to enable "Activate quote".  

    > Step 2 : I decide to change the amount in the quote detail from 10000€ to 1000€ (1000€ < 2000€) and save the quote details. The quote is not saved or refreshed and i can click on "Activate quote". 

    If there's some possibilities to refresh the subgrid or save and refresh the ribbon...or a way to stop the "Activate quote" option after a modification on quote details ?

    Thank you. 

    Regards,

    Jean-Michel

    Wednesday, July 2, 2014 8:39 AM
  • Hi,

    When you change your Quote Detail from 10000 to 1000 and you click on "Activate Quote", the form will get saved internally. So if you have a script registered on save of the Form (like the example I gave above), you should be able to check the value of the Amount and restrict it from activating and probably even show an alert.

    So instead of

                    var fieldVal = Xrm.Page.getAttribute("x").getValue();
    		
    		if (fieldVal == null)
    		{
    			eventArgs.preventDefault();
    		}

    You will use

                    var fieldVal = Xrm.Page.getAttribute("x").getValue();
    		
    		if (fieldVal < 2000)
    		{
    			eventArgs.preventDefault();
                            alert("More than 2000 required");
    		}


    Admin QuikView Solution for CRM 2013


    Wednesday, July 2, 2014 10:36 AM
  • Hi Dynamotion,

    Thanks for your response.

    I've already done this test before but the statecode of quote is changed before the save event.

    There's no way to stop the statecode change on the click of the ribbon button "Activate Quote" ? 

    Thanks for your help again. 

    regards,

    Jean-Michel

    Wednesday, July 2, 2014 11:58 AM