+ Start a Discussion
wt35wt35 

Issue with JavaScript button: value is undefined

Hi community,

 

I have a JavaScript button that calls successfully an Apex class.

However, now I want to give the Opportunity record as a parameter to my Apex method but it does not work.

(it is a detail page button)

When clicking on the button from the Opportunity, the message I get is:

 

"

A problem with the OnClick JavaScript for this button or link was encountered:
value is undefined

"

 

This is my JS button:

{!REQUIRESCRIPT("/soap/ajax/27.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}

var opp = sforce.SObject("Opportunity");
var myString = sforce.apex.execute("ClosedDuplicate","myMethod2",{contextOpp:opp});


window.alert(myString);

 

Basically, what I am unable to do is transmit the opportunity record from where the button has been clicked to my Apex method. I hope its is possible. If not can someone give me the script for trasnmitting the record ID?

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
wt35wt35

After further diggin, I found a solution thanks to this blog:

http://hometeamconsulting.com/update-records-javascript-buttons/

 

Instead of

 

var opp = sforce.SObject("Opportunity");

 

I put:

 

var opp = new sforce.SObject("Opportunity");
opp.id = "{!Opportunity.Id}";

 

That worked....

All Answers

Ashish_SFDCAshish_SFDC

Hi , 

 

For passing the parameter in such cases to controller, you must use apex:param visualforce component.

 

See the example below, 

 

http://boards.developerforce.com/t5/Visualforce-Development/Passing-record-Id-from-a-related-list-to-a-method-associated/td-p/592431

 

Regards,

Ashish

wt35wt35

Thank you, but I am asking for a solution via JavaScript, not Visualforce

wt35wt35

After further diggin, I found a solution thanks to this blog:

http://hometeamconsulting.com/update-records-javascript-buttons/

 

Instead of

 

var opp = sforce.SObject("Opportunity");

 

I put:

 

var opp = new sforce.SObject("Opportunity");
opp.id = "{!Opportunity.Id}";

 

That worked....

This was selected as the best answer
Ashish_SFDCAshish_SFDC

Thats is great! Thank you for posting here. 

 

Regards,

Ashish