web
You’re offline. This is a read only version of the page.
close
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);
    }
}

I have the same question (1)
  • Suggested answer
    Pradeep Rai Profile Picture
    5,489 Moderator on at

    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.

  • Suggested answer
    Charan Raju C R Profile Picture
    7 Moderator on at

    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);
        }
    }

  • necsa Profile Picture
    3,455 on at

    Hi,

    please get as a reference following link

    community.dynamics.com/.../1117632

  • Farhan A Profile Picture
    10 on at

    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.

  • Suggested answer
    Pradeep Rai Profile Picture
    5,489 Moderator on at

    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.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 61

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 52 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans