function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
rbohnrbohn 

Custom Button with conditional logic

I need a custom button who's logic is conditional based upon the STAGE field on the opportunity object.

Here is an actual example from Salesforce's help documentation.   It's basically a conditional test of country and then invokes several different URLs.

{!  IF(Sample.BillingCountry = "US", "http://maps.google.com/maps?q="&Sample.BillingStreet& "+"&Sample.BillingCity&"+"&Sample.BillingState&"+"&Sample.BillingCountry, (IF(Sample.BillingCountry = "UK", "http://maps.google.co.uk/maps?q="&Sample.BillingStreet &"+"&Sample.BillingCity&"+"&Sample.BillingCountry, "http://maps.google.com"))) }

For test purposes I've simplified the above example to the following:

{! 
IF( ISPICKVAL (Opportunity.StageName, "Approved"), 
"http://www.google.com", 
"http://maps.google.com" ) 
}

This button code, which is just a simplification of the Salesforce example, gives a "URL DOES NOT EXIST ERROR" and results in the following URL when invoked.
 https://na8.salesforce.com/servlet/http%3A%2F%2Fwww.google.com 

Can anyone provide pointers on how to implement a customer button that can invoke both external URLs and Visualforce pages? 
Prafull G.Prafull G.

Yeah, its not working as mentioned in the documents.

 

Here is the other way around you can try:

1. Change the Behaviour to 'Execute JavaScript"

2. Change Content Source to 'OnClick JavaScript"

 

Add the following code

 

window.location.href = "{!IF( ISPICKVAL (Opportunity.StageName, "Approved"), 
"http://www.google.com", 
"http://maps.google.com")}"

Hope it helps.

 

 

Shannon HaleShannon Hale

Hi rbohn,

 

I've just created a bug for my team based on the behavior you've described - thanks for bringing it to my attention.

 

You can still use the URL return type if you do the following, and move the "http://" part of the URL outside the merge field context:

 

http://{! IF( ISPICKVAL (Opportunity.StageName, "Approved"), 
  "www.google.com", 
  "maps.google.com" ) 
}

 

I'm the first to admit this is not the best way to do it. but it's probably the best workaround until we can get a fix in.

Shannon HaleShannon Hale

Oops - just noticed you have Visualforce and external URLs, which I'm assuming have mixed http and https.

 

I think my workaround will only work when the URL is the same for both type of links -- if you need the mixed case, you'll probably need to use the JavaScript button type and the workaround @crmtech21 suggested.