Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Prevent form save

Posted on by Microsoft Employee

I'm trying to achieve certain type of logic onSave event via js as follows:

If form lookup equals "Encounter", check that fields contain values, if not alert the user and disable the form from saving. 

I'm getting an error "getEventArgs' of undefined at onSave" on my last function.

I can get my onSave function run by itself when I pass execution context as first parameter in handler properties section.

function workOrderTypeOnSave(){
	getLookup("msdyn_workordertype");
}

function getLookup(lookupSchemaName) {
	var lookupObj = Xrm.Page.getAttribute(lookupSchemaName);
	if (lookupObj != null) {
		var lookupObjValue = lookupObj.getValue();
		if (lookupObjValue != null) {
			var lookupEntityType = lookupObjValue[0].entityType, 
			lookupRecordGuid = lookupObjValue[0].id, 
			lookupRecordName = lookupObjValue[0].name; 
			if (lookupRecordName = "Encounter") {
				onSave();
			}
		}
	}
}

function onSave(context) {
	var saveEvent = context.getEventArgs();
	if (Xrm.Page.getAttribute("msdyn_serviceaccount").getValue() == null || Xrm.Page.getAttribute("cog_dateofbirth").getValue() == null) {
		Xrm.Utility.alertDialog("Please fill all the required fields.");
		saveEvent.preventDefault();
	}
}


 

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Prevent form save

    Okay I see what you're saying.

    Also, is there a way to specifically include what required field is null?

    Currently my if statement (in the function onSave) runs on just a couple of fields. But perhaps in the future I will have to add more fields for validation.

    How exactly can I express what fields are null out of the array?

    lets say in the scenario below. I've got 5 fields for validation. The user didn't fill out fields B & C.

    I want to make life easier for the user and specifically include in my alert what fields he left out.

    Like so: "Please fill all the required fields: B, C."

    function onSave(context) {

    var saveEvent = context.getEventArgs();

    if (Xrm.Page.getAttribute("msdyn_serviceaccount").getValue() == null || Xrm.Page.getAttribute("cog_dateofbirth").getValue() == null || Xrm.Page.getAttribute("cog_fieldA").getValue() == null || Xrm.Page.getAttribute("cog_fieldB").getValue() == null || Xrm.Page.getAttribute("cog_fieldC").getValue() == null) {

    Xrm.Utility.alertDialog("Please fill all the required fields.");

    saveEvent.preventDefault();

    }

    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Prevent form save

    Yea, that solved my issue.

    However my alert message keeps coming back. It of course stops when I finally saves the record. Not sure what's that about?  

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Prevent form save

    I mean "workOrderTypeOnSave" is the only function that I've added to my save event...

    I've checked the pass context as first parameter checkbox and it's still resulting with the same error.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Prevent form save

    I see.. What do I need to change though?

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans