+ Start a Discussion
AAIAAI 

Custom button with multiple if conditions

Hello All,

I have create a custom button. I need that the code validation that: "Lead status" is open and custom field "country" is not null. Any idea??.
Here is my code:
{!REQUIRESCRIPT("/soap/ajax/38.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/38.0/apex.js")}

status = "{!Lead.Status}"; 
Country__c = "{! Lead.Country}";
if (status == ('Open'),
if (Country__c  != ('null'){ 
var lead = new sforce.SObject('Lead'); 
lead.id = "{!Lead.Id}"; 
lead.Status = "Pending conversion"; 
result = sforce.connection.update([lead]); 
location.reload(true); 
}
else{ 
alert("Invalid status. You can only submit to conversion the Lead if the status is ('Open"); 
}
 
Best Answer chosen by AAI
Amit Singh 1Amit Singh 1
Hi Amya,

Try blow code. The error was due to that you were using, instead of { after first IF condition.
{!REQUIRESCRIPT("/soap/ajax/38.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/38.0/apex.js")}

var status = "{!Lead.Status}"; 
var Country = "{!Lead.Country}";
if (status == "Open - Not Contacted"){
  if(Country!= null && Country!=""){ 
    var lead = new sforce.SObject('Lead'); 
    lead.id = "{!Lead.Id}"; 
    lead.Status = "Closed - Converted"; 
    result = sforce.connection.update([lead]); 
    location.reload(true); 
  }else{
   alert('Country Can not be null');
}
 }else{ 
        alert("Invalid status. You can only submit to conversion the Lead if the status is ('Open"); 
}

Note: - Change the values of Status as per your requirement in both places in Condition Check and Updating to new values as I tried this in my dev org in Order to make this work.

It will do the trick.

Mark as best answer if this work :)

Thanks,
Amit Singh

All Answers

AAIAAI
Thanks Amit. I tried it with your code and I had the same error. Unexpected token. Any idea?

Thanks in advance
Amaya
Amit Singh 1Amit Singh 1
Hi Amya,

Try blow code. The error was due to that you were using, instead of { after first IF condition.
{!REQUIRESCRIPT("/soap/ajax/38.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/38.0/apex.js")}

var status = "{!Lead.Status}"; 
var Country = "{!Lead.Country}";
if (status == "Open - Not Contacted"){
  if(Country!= null && Country!=""){ 
    var lead = new sforce.SObject('Lead'); 
    lead.id = "{!Lead.Id}"; 
    lead.Status = "Closed - Converted"; 
    result = sforce.connection.update([lead]); 
    location.reload(true); 
  }else{
   alert('Country Can not be null');
}
 }else{ 
        alert("Invalid status. You can only submit to conversion the Lead if the status is ('Open"); 
}

Note: - Change the values of Status as per your requirement in both places in Condition Check and Updating to new values as I tried this in my dev org in Order to make this work.

It will do the trick.

Mark as best answer if this work :)

Thanks,
Amit Singh
This was selected as the best answer
AAIAAI
Many thanks. It was very useful