Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

prevent save record through Javascript

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
    Community Member Microsoft Employee 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
    Iron Arm 129 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
    Community Member Microsoft Employee 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
    Community Member Microsoft Employee 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
    Saddamk206 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
    VPrashant 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
    Neil Parkhurst 10,725 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
    Jharana Baliyar Singh 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
    Hemant Kumar Sahu 1,825 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
    Community Member Microsoft Employee 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans