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
MBrady325MBrady325 

If statement in custom button

I'm trying to create a custom button that will auto-select the From address when sending an email from a case, depending on a custom field on the case record.  I've tried several ways of doing this, but none of them seem to be working correctly.  One of the posts I found on the board uses Javascript to hack the URL, and I've tried doing this, but I get the error "unexpected token ILLEGAL" when I try it.  Here's the code I'm using:

 

IF({!Case.CustomField__c} = 'Option 1', location.replace('/email/author/emailauthor.jsp?retURL=/{!Case.Id}&p3_lkid={!Case.Id}&rtype=003&p2_lkid={!Case.ContactId}&p26=address1@gmail.com;, 

(IF({!Case.CustomField__c}='Option 2', location.replace('/email/author/emailauthor.jsp?retURL=/{!Case.Id}&p3_lkid={!Case.Id}&rtype=003&p2_lkid={!Case.ContactId}&p26=address2@gmail.com;, 

location.replace('/email/author/emailauthor.jsp?retURL=/{!Case.Id}&p3_lkid={!Case.Id}&rtype=003&p2_lkid={!Case.ContactId};)))

 

I imagine I'm doing this wrong, since this seems to be mixing Salesforce formulas with Javascript.  Is this the correct way to approach my issue, or is there a way to make the button link to the desired URL without using Javascript?

werewolfwerewolf

No, you have to use Javascript.

MayeUPAEPMayeUPAEP

You could use JavaScript,  You need to make a HiddenField "theField" with the value of your Case.CustomField__c

 

<script>

 

function newTask()

{

var Tmp = document.getElementById("{!$Component.ThePage.TheForm.TheBlock.theField}").value;

if(Tmp == 'Option 1') var newURL = "ALL YOUR URL";

                ...etc...

window.parent.location.replace(newURL);

}

 

</script>

 

 

<apex:pageBlockButtons >
<apex:commandButton value="Tarea Nueva" onclick="newTask()"/>
<apex:commandButton value="Evento Nuevo"  onclick="newEvent()"/>
</apex:pageBlockButtons>  

 

<apex:pageBlockButtons >

  <apex:commandButton value="Button 1" onclick="newTask()"/>

 </apex:pageBlockButtons>  

MBrady325MBrady325

Okay, so if I want to customize the "Send an Email" button in Setup/Customize/Activities/Activity Buttons (or another custom button defined in Tasks/Activities), I would change that button to us a custom S-Control, right?  How do I define the S-Control to execute this script?  From what I can tell, I would set the type to HTML and include the script as defined in your example, but what is the command to execute that script?

MayeUPAEPMayeUPAEP

 

 

Ok sorry I didn't understand that you were overriding a button. I thought that you have a custom button in a VF.

 

When you click in your actual button you've got an URL, this url may have in the "retURL" or in the "Id", the value of your id Lead.  Something like that:   cs2.salesforce.com/......?retURL=/XXXX&id=XXXXX

Check which variable has the id of the lead

 

Now You could override your button to a VF instead an S-Control, and in your class make a sql of the value of your custom field.

In your VF something like:

 

 

<apex:page standardController="Account"
           extensions="Class_Account" 
           action="{!redirect}">
<apex:detail />
</apex:page>

 

 

In your Class

 

 

public class Class_Account
 {
 private String ID, FIELD_VALUE;

  
 //Constructor
 public Class_Account(ApexPages.StandardController controller)
  {
  //The code you need
  }

 public PageReference redirect() 
  {
  //Get the Id of your lead for example if is in the retURL
  ID = ApexPages.currentPage().getParameters().get('retURL'); 
  ID = ID.substring(1,ID.length());

  //Sql using your ID
  FIELD_VALUE = SQL...

  if(FIELD_VALUE='Option1')
    {
     PageReference PageRef= new PageReference('/email.....');
     PageRef.setredirect(true);
     PageRef.getParameters().put('id', ID);
     return pageRef;      
    } 
  }
}