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
csathishbabucsathishbabu 

Custom Button URL Control Statements

I have created a Custom Button in Opportunity tab called Invoice,and wen Click the custom Button It has to generate a Invoice for a particular Opportunity ID in a apex Page, Using Condition Only when Opportunity Name Equal to 'Testing' if not it has to be null.I have tried Several times using IF Control Statement,but im getting a error of URL NO LONGER EXIST, Here is my CUstom Button Code {!IF( Opportunity.Name = 'Testing' , '/apex/Invoice_1?id='&Opportunity.Id , null)} but its working fine wen the code is like below /apex/Invoice_1?id={!Opportunity.Id} Kindly Suggest me a Example for how to use IF statemnts in Calling URL Thanks in advance Sathish
Best Answer chosen by Admin (Salesforce Developers) 
SargeSarge

Satish,

 

   Now it is clear that you have detail page button, the code is simple. You keep the content of th button as "OnClick Javascript" and use the code below

 

 

if('{!Opportunity.Name}' == "Testing"){
    window.parent.location.href = "/apex/Invoice_1?id="+'{!Opportunity.Id}';
}else{
   //do nothing
}

 

 

All Answers

SargeSarge

Hi Satish,

 

     For such scenarios please use "OnClick Javascrip" content for your custom button an use a javascript code to navigate to some url based on the conditions.

  Also because you mentioned that this custom button is in Opportunity tab, I assume that this a List button. For your scenario I guess, the user will select one Opportunity record by ticking checkbox beside the row in the list and navigate to Invoice genration page .

 

a sample code might be the following (assumin list button. If the assumption is wrong then code will be different)

 

 

{!REQUIRESCRIPT("/soap/ajax/17.0/connection.js")} 
//get record id for selected opportunity
var oppRecordId = {!GETRECORDIDS( $ObjectType.Opportunity )}; if(oppRecordId.length > 1){   
   //Throw error message if user selects more than one
alert("Error: You have to select only one Opportunity"); }else if(oppRecordId.length== 0){         //Throw error if user selects no opportunity
alert("Error: You have to select one Opportunity record"); }else if(oppRecordId.length == 1){
        //Test for name by querying the relavent opprtnity record
var qResults = sforce.connection.query("Select Id, Name from Opportunity where Id = '"+oppRecordId[0]+"'");
        //alert(qResults); //debugging purposes
if(qResults.records.Name == "Testing"){
            //since only one record is expected to return, if condition will have //qResults.records.name otherwise qResults.records[i].name as array of records are ret.
            window.parent.location.href = "/apex/Invoice_1?id="+oppRecordId[0];
        }else{ //do nothing
        } }

 

Hope this helps

 

 

 

csathishbabucsathishbabu
this above code not works for me , Im using Detail Page button that too im calling the button from Opportunity detail page.i need how the code should change as per detail page button
SargeSarge

Satish,

 

   Now it is clear that you have detail page button, the code is simple. You keep the content of th button as "OnClick Javascript" and use the code below

 

 

if('{!Opportunity.Name}' == "Testing"){
    window.parent.location.href = "/apex/Invoice_1?id="+'{!Opportunity.Id}';
}else{
   //do nothing
}

 

 

This was selected as the best answer
csathishbabucsathishbabu
Thanks a lot Dude,works great.