Ask Search:
Jeff McKinnisJeff McKinnis 

Help using logical functions in custom URL button

Can you use logical functions in combination with a custom URL  in a custom button?  I would like for the formula to evaluate whether or not a field is blank and utilize one custom URL if it is and another if not.  The URLs work by themselves, but fail when I try to combine them with the logical functions.

 

{!

IF(ISBLANK(Account.Parent),

 

"/001/e?acc3={!Account.Name}&CF00N40000002GuGT={!Account.Name}&
RecordType=01240000000M4W4&ent=Account",

 

"/001/e?acc3={!Account.Name}&CF00N40000002GuGT={!Account.Parent}&
RecordType=01240000000M4W4&ent=Account")

}

Best Answer chosen by Moderator (salesforce.com) 
Marco (werewolf) CasalainaMarco (werewolf) Casalaina
No, you can't use functions from the formula language in custom button XML.  If you want, you can make a formula field of type URL and have the button emit that field.  Then your formula logic would be in the field. 

Alternately, you can write Javascript in the button, like

if ('{!Account.Parent}'=='') {
   ...do something...
} else {
   ...do something else...
}

If this answer worked for you, please mark it as the best answer.  Thanks!

All Answers

Marco (werewolf) CasalainaMarco (werewolf) Casalaina
No, you can't use functions from the formula language in custom button XML.  If you want, you can make a formula field of type URL and have the button emit that field.  Then your formula logic would be in the field. 

Alternately, you can write Javascript in the button, like

if ('{!Account.Parent}'=='') {
   ...do something...
} else {
   ...do something else...
}

If this answer worked for you, please mark it as the best answer.  Thanks!
This was selected as the best answer
Jeff McKinnisJeff McKinnis
Thanks werewolf! 

I like the Javascript option, but struggling a bit with the syntax.  Keep getting Unexpected Token ILLegal messages.  Here is what I have - any suggestions:

if ('{!Account.Parent}'=='') {
window.parent.location.href="/001/e?acc3={!Account.Name}&CF00N40000002GuGT={!Account.Name}&
RecordType=01240000000M4W4&ent=Account";
} else {
window.parent.location.href="/001/e?acc3={!Account.Name}&CF00N40000002GuGT={!Account.Parent}&
RecordType=01240000000M4W4&ent=Account"
}
Jeff McKinnisJeff McKinnis
Decided to use the formula field URL which worked perfectly for my use case.  Thanks again werewolf!
Margaret FleischakerMargaret Fleischaker
I think I'm trying to do the same thing but I'm missing a step. I would really appreciate if one of you could help guide me.

I've created a formula field that displays the URL (although the formula field is a text field) that I want the button to link to. However, how do I have the button emit that field? I tried just putting the name of the field in the Button or Link URL (see screenshot). However, when I click on the button from the page it gives me an error message (screenshot attached).

What is the best way for getting the custom button to go to the link in the formula field?
Thank you so much for your help!

Custom Button Error Message

Custom Button Setup Page
Marco (werewolf) CasalainaMarco (werewolf) Casalaina
@mgodsey What URL are you seeing in the URL bar when it gives you that error?  Perhaps your formula just has a malformed URL.
Jeff McKinnisJeff McKinnis
I ended up not using a button, but rather a formula field with a data type of text to display the hyperlink.  The syntax for the HYPERLINK function is bit tricky especially if you passing multiple variables.  I am actually using another picklist field for the users to select a pre-populated opportunity template and the value they select determines what hyperlink is displayed.  This allowed me to use only two fields to display a possible 15 different hyperlinks for the user.  Here is a small piece of the text formula with the HYPERLINK syntax included:

IF( 
ISPICKVAL(Select_Opportunity_Template__c ,"Name Here"), 

HYPERLINK("/SFrecordID/e?clone=1&cloneli=1&opp4="&Name&"&00N40000002Gzyk=&00N40000002Gzya=&opp3=","Give Your Hyperlink a Friendly Name"), 


Margaret FleischakerMargaret Fleischaker
@werewolf the thing is, if I click on the URL in the formula field it takes me to the correct page. I think there is an issue in how I am having the value of the formula field feed into the button content, if that makes sense.
Marco (werewolf) CasalainaMarco (werewolf) Casalaina
@mgodsey You may need to format the URL a bit differently to use it within a custom button.  My question remains: What URL are you seeing in the URL bar when it gives you that error?
Praveen EPraveen E
you need to use bunch of if conditions like I am setting close date on Opp  to end of quarter date  
/006/e?opp9={!
IF(month(TODAY())<12&&month(TODAY())>9, date(year(TODAY()),12,31),
IF(month(TODAY())<9&&month(TODAY())>6, date(year(TODAY()),09,30),
IF(month(TODAY())<6&&month(TODAY())>3, date(year(TODAY()),06,30),
IF(month(TODAY())<3&&month(TODAY())>0, date(year(TODAY()),03,31),date(year(TODAY()),03,31)))))}
Alice JonesAlice Jones
The biggest problem with the custom button syntax is how to account for escape characters.  In the example below, I'm routing the custom button to a custom report.  Note in the URL special characters such as /,?,=,&,etc.... are not put in quotes and not included in the conditional statement. When I placed the special characters inside the quotes or conditional it resulted in escaping issues.  The solution below works perfectly for me. 

Note, I've put the items on seperate lines for clarity.

<Report address>
<pv1 syntax>
<pv2 syntax>

/00O70000004DkMv
?pv1={!IF(ISBLANK(Account.ParentId),Account.Id,Account.ParentId)}
&pv2={!Account.Id}
Aksel ØvernAksel Øvern

This can be solved with the use of the URLFOR() function, and & instead of {!...} inside IF-statement.

For the case described above:

{!
IF(ISBLANK(Account.Parent),
 
URLFOR("/001/e?acc3=" & Account.Name & "&CF00N40000002GuGT=" & Account.Name &
"RecordType=01240000000M4W4&ent=Account"),
 
URLFOR("/001/e?acc3=" & Account.Name & "&CF00N40000002GuGT=" & Account.Parent &
"RecordType=01240000000M4W4&ent=Account")
)
}