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 :
Microsoft Dynamics CRM (Archived)

Prevent form from saving on clicking the save button using javascript

(1) ShareShare
ReportReport
Posted on by 1,672

Hi,

How can we prevent a from from saving on clicking the save button until certain validations are met?

Because of certain conditions I cannot  make the fields mandatory so I have used java script, but it is not working.

I have tried  return false , eventArgs.preventDefault(); but still its not preventing the save 

following is  my code sample.

 if (condition)
            {
                alert("Message ");
                //
                
               
               preventSave()

                //return false;
               
            }


        

function preventSave(econtext) {
    if (econtext != null && econtext.getEventArgs() != null) {
        var eventArgs = econtext.getEventArgs();
        if (eventArgs.getSaveMode() == 1) {
            eventArgs.preventDefault();
        }
    }
}

 Can somebody Please help me  to solve this issue ?

Kind Regards.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    you need to call your function from the OnSave event defined in the form AND select "pass the context as first parameter" when you add the function.

  • Nijos Profile Picture
    1,672 on at

    Hi Guido Preite,

    yes I am calling this from the on save and the pass context option is selected .

    but it is saving my form.

    thanks for the response

  • Guido Preite Profile Picture
    54,086 Moderator on at

    are you sure that is the function preventSave that is called inside the OnSave event?

    what is the before code you posted before the function?

  • Verified answer
    Rajkumar Rajaraman Profile Picture
    on at

    Hi Nijo,

    Refer this link:

    www.mscrm.in/.../prevent-stop-save-form-operation-using.html

    Hope this helps.

    Regards,

    R.Rajkumar

    "Please mark my answer as verified if you found it helpful"

  • Rajkumar Rajaraman Profile Picture
    on at

    Hi Nijo,

    Refer this link also:

    blogs.msdn.com/.../how-to-manage-auto-save-for-individual-forms-of-entity.aspx

  • Nijos Profile Picture
    1,672 on at

    It worked

    Thank you Rajkumar I called the preventDefault() method from  another function that was the issue.

  • Community Member Profile Picture
    on at

    Hi Rajkumar,

    your link doesn't work anymore

  • Verified answer
    Community Member Profile Picture
    on at

    Hi Nijos, 

    As your issue is resolved, I would like to give my inputs for other viewers who are still struggling with this issue. 

    Kindly follow the steps: 
    1. Create the below mentioned javascript function.

    function preventSave(econtext) {

                  if (econtext != null && econtext.getEventArgs() != null) {
                          var eventArgs = econtext.getEventArgs();
                           if (eventArgs.getSaveMode() == 1) {
                                 eventArgs.preventDefault();
                             }
                   }
       }

    2. Open the event handler and check the "pass execution context as first parameter"

     pic1111.png


    this is the way to prevent the form from save.
    Thanks,
    Shashank
  • Nijos Profile Picture
    1,672 on at

    Hi Shashank,

    Really appreciate the effort.

    Nijo

  • Community Member Profile Picture
    on at

    This is link is not working.

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans