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 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

I have the same question (0)
  • BharatPremji Profile Picture
    2,485 on at

    Hi Vijay,

    Have you tried this:

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

    Bharat

  • VPrashant Profile Picture
    675 on at

    Hi Vijay,

    You can try this notification to show the message and not alert box.

    if (a!=b) {

          SetPageNotification("info", "INFO", "InfoNotification");

                       args.preventDefault();

                   }

                   else {

                       ClearAllNotifications();

                   }

    function SetPageNotification(aMessage, aType, aNotificationName) {

           // Set the notification

           Xrm.Page.ui.setFormNotification(

             aMessage,

             aType,

             aNotificationName

           );

       }

    function ClearAllNotifications() {

           ClearPageNotification("InfoNotification");

    }

    I think this is the right way to do it. At least I have used this. Please correct me if I am in wrong direction, CRM expert guys !!!

    Thanks

    Prashant

  • vijay12872 Profile Picture
    1,000 on at

    Thanks for your reply prashant . But still the record is saving while a!=b .the sam problem still exists

  • vijay12872 Profile Picture
    1,000 on at

    Thanks for your reply prashant . But still the record is saving while a!=b .the same problem still exists

  • Suggested answer
    Varsha deshpande Profile Picture
    135 on at

    Hi vijay,

    Try this code:

    if(a != b)

    {

    Xrm.Utility.alertDialog("a and b are not equal",function(){
    Xrm.Page.data.entity.addOnSave(function (execContext) {
    execContext.getEventArgs().preventDefault();//will prevent CRM form save
    });
    });

    }

    Hope this helps,

    Regards,

    Varsha Deshpande

  • Suggested answer
    Community Member Profile Picture
    on at

    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

  • Suggested answer
    Hemant Kumar Sahu Profile Picture
    1,829 on at

    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
    Jharana Baliyar Singh Profile Picture
    2,667 on at

    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
    Neil Parkhurst Profile Picture
    10,727 User Group Leader on at

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

     }

    }

  • VPrashant Profile Picture
    675 on at

    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

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