Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Javascript returns undefined when called from command button function

Posted on by 5

Hi,

I have the following JS code

function getFormRedirectAttribute(formContext,formRedirect){
    alert(formRedirect); //parameter string
}

This is called via form OnLoad Event. This returns a parameter string I've included in the form event. I've confirmed this returns the parameter string in an alert message when the form loads (formRedirect).

I then have the following command bar button code which should execute the function above when dialog is confirmed (getFormRedirectAttribute). The expected result is to return the parameter string as an alert exactly like it does on form load, however the alert string always returns "Undefined" when called via the button. I'm not sure why?

function buttonRequestAccessRecord(formContext) {
	var confirmStrings = { text:"Run function", title:"Confirmation Dialog" };
	var confirmOptions = { height: 200, width: 450 };
	Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(

	function (success) {    
		if (success.confirmed)        
			getFormRedirectAttribute();
		else
			getFormRedirectAttribute();
	});
}

Any help is massively appreciated!

Many thanks 

  • JamesWils Profile Picture
    JamesWils 5 on at
    RE: Javascript returns undefined when called from command button function

    Hi Matt,

    Unfortunately the above code doesn't have the expected result. Within the success function, rather than have "This is my alert", I simply need to execute getFormRedirectAttribute with both formContext and parameter which is passed on form load from getFormRedirectAttribute function

    Something like

    function getFormRedirectAttribute(formContext,myParameterString){
    
     alert(myParameterString); 
    
    // This is called on Form Load event and returns myParameterString value as SpongeBob Squarepants
    // This function is also called via function buttonRequestAccessRecord but returns "Undefined"
       
    }
    
    function buttonRequestAccessRecord(formContext) {
    	var formRedirect = formRedirect
    	var confirmStrings = { text:"Run function", title:"Confirmation Dialog" };
    	var confirmOptions = { height: 200, width: 450 };
    	Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(
    
    	function (success) {    
    		if (success.confirmed)        
    			getFormRedirectAttribute(formContext,myParameterString);
    		else
    			getFormRedirectAttribute(formContext,myParameterString);
    	});
    }

  • Suggested answer
    - Matt Bayes - Profile Picture
    - Matt Bayes - 890 on at
    RE: Javascript returns undefined when called from command button function

    Hi James,

    I may be misunderstanding, but based on the code you supplied I would expect you to have to pass the getFormRedirectAttribute() method in your success function some parameters.

    Since you are looking to alert the "formRedirect" parameter, the expected format of the function call should be something like:

    function buttonRequestAccessRecord(formContext) {
    	var confirmStrings = { text:"Run function", title:"Confirmation Dialog" };
    	var confirmOptions = { height: 200, width: 450 };
    	Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(
    
    	function (success) {    
    		if (success.confirmed)        
    			getFormRedirectAttribute(formContext, "This is my alert");
    		else
    			getFormRedirectAttribute(formContext, "This is also my alert");
    	});
    }

    I may have misunderstood what you referred to when you said that it worked in the OnLoad event, but I believe this would be why your parameter string is Undefined because the variable used actually isn't being passed in.

    Thanks!

    Matt Bayes

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans