+ Start a Discussion
cjencjen 

JavaScript Button help IF statement

Hi, I need to create a button that will be used on an opportunity record to create a new related record. But i want this button to determine which record type to use based on if the logged in user is the opportunity owner. I don't have a clue how to write javascript to make this happen, can anyone help?

Here is what works in a regular button:
/a4J/e?
saveURL={!Opportunity.Id}
&retURL=%2F{!Opportunity.Id}
&RecordType=012W00000008qvd
&CF00NW0000000wUyk={!Opportunity.Name}
&ent=Partner

But, that is just for when Opportunity Owner Id does not equal User Id. I need to create a similar record, but change the record type if Opportunity Owner ID does equal Used Id. Ideally would like it to work from just one button.

Thanks!
Best Answer chosen by cjen
@Karanraj@Karanraj
Try the below code
In the if and else conditions update with your URLs
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
if("{!Opportunity.OwnerId}" == "{!$User.Id}"){
window.location = "/a4J/e?saveURL={!Opportunity.Id}&retURL=%2F{!Opportunity.Id}&RecordType=012W00000008qvd
&CF00NW0000000wUyk={!Opportunity.Name}&ent=Partner";
}
else{
window.location = "/a4J/e?saveURL={!Opportunity.Id}&retURL=%2F{!Opportunity.Id}&RecordType=012W00000008qvd
&CF00NW0000000wUyk={!Opportunity.Name}&ent=Partner";
}

 

All Answers

@Karanraj@Karanraj
Try the below code
In the if and else conditions update with your URLs
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
if("{!Opportunity.OwnerId}" == "{!$User.Id}"){
window.location = "/a4J/e?saveURL={!Opportunity.Id}&retURL=%2F{!Opportunity.Id}&RecordType=012W00000008qvd
&CF00NW0000000wUyk={!Opportunity.Name}&ent=Partner";
}
else{
window.location = "/a4J/e?saveURL={!Opportunity.Id}&retURL=%2F{!Opportunity.Id}&RecordType=012W00000008qvd
&CF00NW0000000wUyk={!Opportunity.Name}&ent=Partner";
}

 
This was selected as the best answer
cjencjen
Hi, first of all thanks so much for helping me! I tried the code, but i get a message, unterminated string contstant. Any ideas? Thanks!
cjencjen
@karanraj hi, i figured it out, had spaces in my location strings. now it works beautifully! Thanks so much!