web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 184 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 125

#3
CU11031447-0 Profile Picture

CU11031447-0 100

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans