web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Prevent saving the form using javascript

(0) ShareShare
ReportReport
Posted on by 30

I have a script on OnSave of the form.

While saving the form if there is an alert than it should prevent from saving the form. 

I have use the following in the script:

var eventArgs = executionContext.getEventArgs();  
     eventArgs.preventDefault(); 

Still it is saving the record.

Any help is appreciated.

I have the same question (0)
  • Saad Kabarousse Profile Picture
    734 on at
    RE: Prevent saving the form using javascript

    Hello,

    your code seems fine, add a  return  after the as shown below

    executionContext.getEventArgs().preventDefault();

               return;

    it may do the job :)

  • Tjalui Profile Picture
    30 on at
    RE: Prevent saving the form using javascript

    This is the function

    processRetrievedInvoices : function(result, formContext,executionContext)

       {

           debugger;

           try {

               if (result !== null && result !== undefined) {

                   if (result.entities !== null && result.entities !== undefined) {

                       if (result.entities.length > 0) {

                           //var saveEvent = executionContext.getEventArgs();

                           // Xrm.Navigation.openErrorDialog({ errorCode:"ABC7123", details:"These are the details that will go into the downloaded file", message:"Here's the message" }).then( function (success) {

                           //     console.log(success);        

                           // },

                           // function (error) {

                           //     var eventArgs = executionContext.getEventArgs();  

                           //     eventArgs.preventDefault();

                           //     console.log(error);

                           // });

                           var message = "Invoice Number " +invoiceNumber + " already exists. Please enter correct Invoice Number. ";

                           var type = "WARNING";  //INFO, WARNING, ERROR

                           var id = "101";  //Notification Id

                           var time = 20000;  //Display time in milliseconds

                           //Display the notification

                           Xrm.Page.ui.setFormNotification(message, type, id);

                           //Wait the designated time and then remove the notification

                           //setTimeout( function () {Xrm.Page.ui.clearFormNotification(id);}, time );

                           //alert("Invoice Number" +invoiceNumber + " already exists. Please enter correct Invoice Number. ");

                           var eventArgs = executionContext.getEventArgs();

                           alert(eventArgs.getSaveMode());

                           if (eventArgs.getSaveMode() == 70 || eventArgs.getSaveMode() == 1) //manual save or Xrm save

                           {

                               eventArgs.preventDefault();  

                           }

                       }

                   }

               }

           } catch (e) {

               Xrm.Utility.alertDialog(e.message);

           }

       },

  • meelamri Profile Picture
    13,216 User Group Leader on at
    RE: Prevent saving the form using javascript

    Hi,

    Can you please share all your code ?

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 258

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 170

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 116 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans