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 :
Customer experience | Sales, Customer Insights,...
Answered

getEventArgs().preventDefault(); not doing anything

(0) ShareShare
ReportReport
Posted on by

Hi,

i tried to prevent saving a record, however the record still gets saved. i dont know how to debug this problem, since there is no feedback.
here my code

function preventSave(econtext) {
    var eventArgs = econtext.getEventArgs();
	var formContext = econtext.getFormContext();
	var requesttype = formContext.getAttribute("go_requesttype").getValue();
    if ((eventArgs.getSaveMode() == 70 || eventArgs.getSaveMode() == 2 || eventArgs.getSaveMode() == 1 || eventArgs.getSaveMode() == 59) && requesttype == 936520001) {
	var confirmStrings = { text:"You are about to delete this record and associated records.", title:"Delete Confirmation" };
	var confirmOptions = { height: 200, width: 450 };
	Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(
	function (success) {    
		if (success.confirmed){
			console.log("Dialog closed using OK button.");
		}else{
			console.log(eventArgs);
			eventArgs.preventDefault();
			console.log("Dialog closed using Cancel button or X.");
		}
	});  
    }
}

preventsave.JPG

preventsave2.JPG

any ideas? am i using this method correctly?

I have the same question (0)
  • ajyendra Profile Picture
    1,738 on at

    When you will call this function? on which event?

  • Joana Pinto Profile Picture
    740 on at

    Hi,

    Did you try to debugg it, see if you are hitting the correct part of your code?

  • Verified answer
    ajyendra Profile Picture
    1,738 on at

    Hi,

    I have made some Changes in your code and its work.Call this function in OnSave Event

    var isConfirmed = false;

    function preventSave(econtext) {
    var eventArgs = econtext.getEventArgs();
    var formContext = econtext.getFormContext();
    var requesttype = formContext.getAttribute("go_requesttype").getValue();

    if ((eventArgs.getSaveMode() == 70 || eventArgs.getSaveMode() == 2 || eventArgs.getSaveMode() == 1 || eventArgs.getSaveMode() == 59) && requesttype == 936520001) {
    var confirmStrings = {
    text: "You are about to delete this record and associated records.",
    title: "Delete Confirmation"
    };
    var confirmOptions = {
    height: 200,
    width: 450
    };
    if (!isConfirmed) {
    eventArgs.preventDefault();
    Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(
    function (success) {
    if (success.confirmed) {
    isConfirmed = true; //to make sure we don't display alert dialog from onSave this time
    Xrm.Page.data.save();
    console.log("Dialog closed using OK button.");
    } else {
    console.log(eventArgs);
    eventArgs.preventDefault();
    isConfirmed = false;
    console.log("Dialog closed using Cancel button or X.");
    }
    });
    } else {
    isConfirmed = false;
    }
    }
    }

    Note: As per your code it will popup dialog box in auto Save also

  • erhan.keskin Profile Picture
    2,253 on at

    Hi,

    I believe you call this method on Form OnSave event. So, put "return false;" line after this statement;

    eventArgs.preventDefault();

    console.log("Dialog closed using Cancel button or X.");

    return false;

    Regards,

  • Suggested answer
    nerdifand Profile Picture
    on at

    wow thank you very much Ajyendra for your fast answer! maybe you can explain why you preventDefault() 2x times? i dont understand.

    sincerely

  • Suggested answer
    ajyendra Profile Picture
    1,738 on at

    because when we click on save button it automatically called savemethod Xrm.Page.data.save();.As you are doing here is openconfirmdialog but before open the openconfirmdailog function it already saved the record. So we have to restrict it save that why it call before the openconfirmdialog

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 61

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 52 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans