Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

prevent save record through Javascript

(0) ShareShare
ReportReport
Posted on by 1,000


I use the following condition to check a,b values before save.And I call this function form's onsave Event.

if the field values a == b means i need to save the record.Else I need to show the alert.

if a!= b condition the infinite loop will occur which means the alert message shows continuously .My problem is i need to prevent save record if(a!=b) . I used eventArgs.preventDefault(); but its not working.

Am working in CRM 2016. 

function onsavecall()
{
if(a != b)
{
alert("alert a is not equal to b .so please change value B");
}
else
{
Xrm.Page.data.save.then(function(){
Xrm.Page.data.refresh();
});
}
}

Thanks in Advance

*This post is locked for comments

  • Community Member Profile Picture
    on at
    RE: prevent save record through Javascript

    I've found the easiest way to restrict saving is by setting notifications on form control, as a bonus you can display to the user way they can't save:

    if(foo) {

               formContext.getControl("rsf_type").setNotification('Cannot enter a PTO. No remaining balance.');

           } else {

               formContext.getControl("rsf_type").clearNotification();

           }

  • Suggested answer
    Iron Arm Profile Picture
    131 on at
    RE: prevent save record through Javascript

    setNotification will block saving.  

    preventDefault is not necessary, nor is entity.save.

    All you need is setNotification if data is bad, and clearNotification if it's good.

  • Community Member Profile Picture
    on at
    RE: prevent save record through Javascript

    Hello everyone!

    I got the functionality to work and successfully prevent a save. (In this case, I'm trying to make sure a date is in the future.)

    Here's my code:

    function checkEstCloseDate(context)
    {
      var saveEvent = context.getEventArgs();
      var today = new Date();
    
      if (Xrm.Page.getAttribute("estimatedclosedate").getValue() < today)
      {
        Xrm.Page.getControl("estimatedclosedate").setNotification("Est Close Date is in the past. Please change it to a future date.", "1");
    
        saveEvent.preventDefault();
      }
    
      else
      {
        Xrm.Page.ui.clearFormNotification("1");
        Xrm.Page.data.entity.save();
      }
    }


    The above code successfully checks the date and applies the error message.

    The problem arises when I try to change the date in the field. My code does not recognize a change has been made, and thus does not turn off the error. How do I fix this? I want to be able to change the value of the field and have the error message go away so I can save the form.

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: prevent save record through Javascript

    function onsave(context) {

    // Prevent the event execution

    context.getEventArgs().preventDefault();

    }

    pass_2D00_execution_2D00_context.png

  • Suggested answer
    Saddamk206 Profile Picture
    777 on at
    RE: prevent save record through Javascript

    Hi Please try this code

    //Save Form changes Using JS

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

  • VPrashant Profile Picture
    675 on at
    RE: prevent save record through Javascript

    Hey Vijay,

    Any updates on the answers that are specified below. If any given solution has worked for you please mark that post as answer. Or else let us know what is bugging still in the code?

    We will help you out.

    -Prashant

  • Suggested answer
    Neil Parkhurst Profile Picture
    10,727 User Group Leader on at
    RE: prevent save record through Javascript

    I have described the approach in this blog post.

    neilparkhurst.com/.../javascript-prevent-save

    First you need to select the option which is "Pass execution contact as first parameter".

    Then you will need code something like .....

    function onSave(context) {

     var saveEvent = context.getEventArgs();

     if (<<a>>!=<<b>>) {

       alert("Put in a phone number!");

       saveEvent.preventDefault();

     }

    }

  • Suggested answer
    Jharana Baliyar Singh Profile Picture
    2,665 on at
    RE: prevent save record through Javascript

    Hi Vijay,

    Check the below code :

    function onSaveCall(context)

    {

        var a= Xrm.Page.getAttribute("fieldname").getValue();

        var b= Xrm.Page.getAttribute("fieldname").getValue();

       if(a !=b)

      alert( "Unable to save as values are not equal..." );

      context.getEventArgs().preventDefault();

       }

    Then onSave event call this function & check the checkbox"Pass execution context as first parameter".

    You can check this link :www.infinite-x.net/.../crm-2011-canceling-the-save-operation

    Thanks

  • Suggested answer
    Hemant Kumar Sahu Profile Picture
    1,829 on at
    RE: prevent save record through Javascript

    Hi Vijay,

    Use this code, Trigger a method on save and pass the context.

     function onSave(context) {    

           if(a != b)) {

               context.getEventArgs().preventDefault();

           }

       }

    Hope this helps you !

    Thanks

    Hemant

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: prevent save record through Javascript

    Hi Vijay12872

    It looks like eventArgs.preventDefault() is not working.

    Make sure you check the "Pass execution context as first parameter" while adding your function to onSave event handlers.

    0804.Save.PNG

    Thanks

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,261 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,996 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans