Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

How to disable prevent save (preventDefault) on form with Javascript with certain condition?

(0) ShareShare
ReportReport
Posted on by 10

Hi guys,

I am new in Javascript and D365.  My objective is : When adjustment type = Credit notes it will triggered the JS in adjustment invoice (adjustment amount >= invoice amount) alert will prompt, and prevent the adjustment invoice form from save. For adjustment type = debit notes, it will save no mater what condition of the adj amount / invoice amount.

My issue was when i choose adjustment type = debit notes , it triggered the preventDefault. How can i disable it for the debit notes. I have included my code for review. Thank you so much to this community.

Regards,

Farhan

Overview of the flow

//Issue: On Debit notes when adjamount > invoice it should be save. But its not.

function adjustmentInvoiceApproveAmount(executionContext) {
    try {
        // Get the form context
        var formContext = executionContext.getFormContext();
        var saveEvent = executionContext.getEventArgs();
        var adjustmentamount = formContext.getAttribute("case_adjustmentamount").getValue();
        var amountdue = formContext.getAttribute("case_amountdue").getValue();
        
        //Check Condition before checking whether it is credit or debit
        if (adjustmentamount >= amountdue) {
            saveEvent.preventDefault();
        }
        // Get ajustment GUID
        var adjustment = formContext.getAttribute("case_adjustment").getValue();
        if (adjustment != null) {
            var adjustmentGUID = adjustment[0].id.substring(1, 37);
        }
        console.log("GUID \"case_adjustment\" = "   adjustmentGUID   " ; "   typeof adjustmentGUID);
        
        //WebApi call under adj primary entity (adjustment) > adjtype > expand AdjustmentType > get case_name value = Credit Notes
             Xrm.WebApi.retrieveMultipleRecords("case_adjustment", "?$select=_case_adjustmenttype_value&$expand=case_AdjustmentType($select=case_name)&$filter=case_adjustmentid eq "   adjustmentGUID).then(
                function success(results) {
                     debugger
                     
                    for (var i = 0; i < results.entities.length; i  ) {
                        console.log(results.entities[i]);
                        var _case_adjustmenttype_value = results.entities[i]["_case_adjustmenttype_value"];
                        console.log("case_name= "   _case_adjustmenttype_value);
                        var casename = results.entities[i]["case_AdjustmentType"]["case_name"];
                        console.log("case_name= "   casename);

                        
                        if (casename == "Credit notes") {
                            if (adjustmentamount >= amountdue) {
                                alert("Adjustment Amount cannot be more than Invoice Amount.");
                            }
                            else {
                                console.log("This can be save");
                                parent.Xrm.Page.data.entity.save();
                            }
                        }
                        else {
                            //only here cannot be use
                            console.log("This is debit notes, can save");
                            parent.Xrm.Page.data.entity.save();
                        }
                    }
            },
            function (error) {
                Xrm.Utility.alertDialog(error.message);
            }
        );
    } catch (error) {
        console.log(error);
    }
}

  • Suggested answer
    Pradeep Rai Profile Picture
    Pradeep Rai 5,482 Super User 2025 Season 1 on at
    RE: How to disable prevent save (preventDefault) on form with Javascript with certain condition?

    Hi @Farhan,

    Please try below code:

    try {
    
           // Get the form context
    
           var formContext = executionContext.getFormContext();
    
           var saveEvent = executionContext.getEventArgs();
    
           var adjustmentamount = formContext.getAttribute("case_adjustmentamount").getValue();
    
           var amountdue = formContext.getAttribute("case_amountdue").getValue();
    
           var stopSave=false;
    
           // //Check Condition before checking whether it is credit or debit
    
           // if (adjustmentamount >= amountdue) {
    
           //     saveEvent.preventDefault();
    
           // }
    
           // Get ajustment GUID
    
           var adjustment = formContext.getAttribute("case_adjustment").getValue();
    
           if (adjustment != null) {
    
               var adjustmentGUID = adjustment[0].id.substring(1, 37);
    
           }
    
           console.log("GUID \"case_adjustment\" = "   adjustmentGUID   " ; "   typeof adjustmentGUID);
    
           //WebApi call under adj primary entity (adjustment) > adjtype > expand AdjustmentType > get case_name value = Credit Notes
    
                Xrm.WebApi.retrieveMultipleRecords("case_adjustment", "?$select=_case_adjustmenttype_value&$expand=case_AdjustmentType($select=case_name)&$filter=case_adjustmentid eq "   adjustmentGUID).then(
    
                   function success(results) {
    
                        debugger
    
                       for (var i = 0; i < results.entities.length; i  ) {
    
                           console.log(results.entities[i]);
    
                           var _case_adjustmenttype_value = results.entities[i]["_case_adjustmenttype_value"];
    
                           console.log("case_name= "   _case_adjustmenttype_value);
    
                           var casename = results.entities[i]["case_AdjustmentType"]["case_name"];
    
                           console.log("case_name= "   casename);
    
                           if (casename == "Credit notes") {
    
                               if (adjustmentamount >= amountdue) {
    
                                   //alert("Adjustment Amount cannot be more than Invoice Amount.");
    
                                   stopSave=true;
    
                               }
    
                               else {
    
                                   console.log("This can be save");
    
                                   stopSave=false;
    
                                   //parent.Xrm.Page.data.entity.save();
    
                               }
    
                           }
    
                           else {
    
                               //only here cannot be use
    
                               console.log("This is debit notes, can save");
    
                               //parent.Xrm.Page.data.entity.save();
    
                           }
    
                       }
    
                       if(stopSave)
    
                       {
    
                           alert("Adjustment Amount cannot be more than Invoice Amount.");
    
                           saveEvent.preventDefault();
    
                       }
                       else{
                       formContext.data.entity.save();
                       }
    
               },
    
               function (error) {
    
                   Xrm.Utility.alertDialog(error.message);
    
               }
    
           );
           
            saveEvent.preventDefault();
    
       } catch (error) {
    
           console.log(error);
    
       }
    
    

    In above code i made small changes added prevent default inside the try block at LINE 112.

    So, When above code executed it will not save the record. Once WebAPI response return then only it will save the record.

    Thanks,
    Pradeep.

    Please mark this as VERIFIED if it helps.

  • Farhan A Profile Picture
    Farhan A 10 on at
    RE: How to disable prevent save (preventDefault) on form with Javascript with certain condition?

    Hi @Pradeep Rai,

    Your code almost correct but for situation "credit notes > triggered alert  > prevent save" , it saved on my side. Other condition is good except that one. The alert message is triggering but it save i assume it auto save for whole page.

  • necsa Profile Picture
    necsa 3,455 on at
    RE: How to disable prevent save (preventDefault) on form with Javascript with certain condition?

    Hi,

    please get as a reference following link

    community.dynamics.com/.../1117632

  • Suggested answer
    Charan Raju C R Profile Picture
    Charan Raju C R 7 Moderator on at
    RE: How to disable prevent save (preventDefault) on form with Javascript with certain condition?

    Hi Farhan,

    Condition to compare Adjustment Amount and Amount Due at line #12 should me pushed inside the retrieve multiple success function where Adjustment Type is checking. Please check if the below code can help to address your issue.

    On a side note, you can replace retrieveMultiple with retrieveRecord function as you already know the GUID of the record to be retrieved.

    var adjustmentamount, amountdue;
    function adjustmentInvoiceApproveAmount(executionContext) {
        try {
            // Get the form context
            var formContext = executionContext.getFormContext();
            var saveEvent = executionContext.getEventArgs();
            adjustmentamount = formContext.getAttribute("case_adjustmentamount").getValue();
            amountdue = formContext.getAttribute("case_amountdue").getValue();
            
            // Get ajustment GUID
            var adjustment = formContext.getAttribute("case_adjustment").getValue();
            if (adjustment != null) {
                var adjustmentGUID = adjustment[0].id.substring(1, 37);
            }
            console.log("GUID \"case_adjustment\" = "   adjustmentGUID   " ; "   typeof adjustmentGUID);
            
            Xrm.WebApi.retrieveMultipleRecords("case_adjustment", "?$select=_case_adjustmenttype_value&$expand=case_AdjustmentType($select=case_name)&$filter=case_adjustmentid eq "   adjustmentGUID).then(
                function success(results) {
                    for (var i = 0; i < results.entities.length; i  ) {
                        console.log(results.entities[i]);
                        var _case_adjustmenttype_value = results.entities[i]["_case_adjustmenttype_value"];
                        console.log("case_name= "   _case_adjustmenttype_value);
                        var casename = results.entities[i]["case_AdjustmentType"]["case_name"];
                        console.log("case_name= "   casename);
    
                        if (casename == "Credit notes" && adjustmentamount >= amountdue) {
                            alert("Adjustment Amount cannot be more than Invoice Amount.");
                            saveEvent.preventDefault();
                        }
                        else {
                            console.log("This can be save");
                            parent.Xrm.Page.data.entity.save();
                        }
                    }
                },
                function (error) {
                    Xrm.Utility.alertDialog(error.message);
                }
            );
        } 
        catch (error) {
            console.log(error);
        }
    }

  • Suggested answer
    Pradeep Rai Profile Picture
    Pradeep Rai 5,482 Super User 2025 Season 1 on at
    RE: How to disable prevent save (preventDefault) on form with Javascript with certain condition?

    Hi Farhan,

    Please check below:

    function adjustmentInvoiceApproveAmount(executionContext) {

       try {

           // Get the form context

           var formContext = executionContext.getFormContext();

           var saveEvent = executionContext.getEventArgs();

           var adjustmentamount = formContext.getAttribute("case_adjustmentamount").getValue();

           var amountdue = formContext.getAttribute("case_amountdue").getValue();

           var stopSave=false;

           // //Check Condition before checking whether it is credit or debit

           // if (adjustmentamount >= amountdue) {

           //     saveEvent.preventDefault();

           // }

           // Get ajustment GUID

           var adjustment = formContext.getAttribute("case_adjustment").getValue();

           if (adjustment != null) {

               var adjustmentGUID = adjustment[0].id.substring(1, 37);

           }

           console.log("GUID \"case_adjustment\" = " + adjustmentGUID + " ; " + typeof adjustmentGUID);

           //WebApi call under adj primary entity (adjustment) > adjtype > expand AdjustmentType > get case_name value = Credit Notes

                Xrm.WebApi.retrieveMultipleRecords("case_adjustment", "?$select=_case_adjustmenttype_value&$expand=case_AdjustmentType($select=case_name)&$filter=case_adjustmentid eq " + adjustmentGUID).then(

                   function success(results) {

                        debugger

                       for (var i = 0; i < results.entities.length; i++) {

                           console.log(results.entities[i]);

                           var _case_adjustmenttype_value = results.entities[i]["_case_adjustmenttype_value"];

                           console.log("case_name= " + _case_adjustmenttype_value);

                           var casename = results.entities[i]["case_AdjustmentType"]["case_name"];

                           console.log("case_name= " + casename);

                           if (casename == "Credit notes") {

                               if (adjustmentamount >= amountdue) {

                                   //alert("Adjustment Amount cannot be more than Invoice Amount.");

                                   stopSave=true;

                               }

                               else {

                                   console.log("This can be save");

                                   stopSave=false;

                                   //parent.Xrm.Page.data.entity.save();

                               }

                           }

                           else {

                               //only here cannot be use

                               console.log("This is debit notes, can save");

                               //parent.Xrm.Page.data.entity.save();

                           }

                       }

                       if(stopSave)

                       {

                           alert("Adjustment Amount cannot be more than Invoice Amount.");

                           saveEvent.preventDefault();

                       }

               },

               function (error) {

                   Xrm.Utility.alertDialog(error.message);

               }

           );

       } catch (error) {

           console.log(error);

       }

    }

    In Above code, i have commented the red color code part which we need to remove from code. and Added the green color code.


    Now, if we read above code then it will work like below:


    1. Retrieve the Case Adjustment Type

    2. Check if casename == "Credit Notes" the set stopSave to true

    3. Else stopSave to false.

    4. After loop we have checked the stopSave field value. if true then PreventSave else proceed save.


    Thanks,
    Pradeep.

    Please mark this as VERIFIED if it helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,387 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans