+ Start a Discussion
Jonathan Bernd 31Jonathan Bernd 31 

How do I bind an incoming Salesforce picklist value to a javascript variable

Newbie question.  I want to use Javascript to conditionally return values based on the incoming value from a Salesforce picklist field.
(I know I can make conditional fields in SF, but I want to figure out how to get a variable from what comes from SF so I can use it in javascript).

Here's the example without Salesforce fields that works fine:
<script>
var myfirst = 'This';
var mysecond = 'That';
var inpvariable = 'Banana';


if(inpvariable =='Coffee')
document.write(myfirst);
 else document.write(mysecond);
</script>
Here is what I want to do with an incoming picklist field
<div class = "mypage">
{!Opportunity.StageName}
</div>

<script>
var myfirst = 'This';
var mysecond = 'That';
var myother = 'Other';
var inpvariable = {!Opportunity.StageName}.value;


if(inpvariable =='Prospecting')
document.write(myfirst);
 else if (inpvariable =='Proposal/Price Quote')
document.write(second);
else
document.write(myother);
</script>
Help would be greatly appreciated.  Thanks in advance
 
Best Answer chosen by Jonathan Bernd 31
Anupam RastogiAnupam Rastogi
Hi Jonathan,

You need to get the value of within single quotes like shown below. This works in Javascript.
<div class = "mypage">
{!Opportunity.StageName}
</div>

<script>
var myfirst = 'This';
var mysecond = 'That';
var myother = 'Other';
var inpvariable = '{!Opportunity.StageName}';


if(inpvariable =='Prospecting')
document.write(myfirst);
 else if (inpvariable =='Proposal/Price Quote')
document.write(second);
else
document.write(myother);
</script>
Thanks
AR

If this resolves your problem please mark it as best answer.
 

All Answers

Anupam RastogiAnupam Rastogi
Hi Jonathan,

You need to get the value of within single quotes like shown below. This works in Javascript.
<div class = "mypage">
{!Opportunity.StageName}
</div>

<script>
var myfirst = 'This';
var mysecond = 'That';
var myother = 'Other';
var inpvariable = '{!Opportunity.StageName}';


if(inpvariable =='Prospecting')
document.write(myfirst);
 else if (inpvariable =='Proposal/Price Quote')
document.write(second);
else
document.write(myother);
</script>
Thanks
AR

If this resolves your problem please mark it as best answer.
 
This was selected as the best answer
Jonathan Bernd 31Jonathan Bernd 31
Can't believe it was so simple and that I didn't try it!  Thanks