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,...
Suggested Answer

Javascript returns undefined when called from command button function

(0) ShareShare
ReportReport
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 

I have the same question (0)
  • Suggested answer
    - Matt Bayes - Profile Picture
    890 on at

    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

  • JamesWils Profile Picture
    5 on at

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

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 171 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 83

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans