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
Raghu Sharma 6Raghu Sharma 6 

list button: logic to allow button to be conditionally performing action

I need to create a list button on 'contact related list' under Account object. I want to write a java script to check whether the account billing state is 'CA'. I want to execute further logic (apex page/apex class) only if the condition matches. But when I tried this below logic, it didnt work.and I see that Account id is not being displyed when I tried to display it. What is missing here? how to get the account id on this button logic?
 
{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")} 

var AcctId = '{!Contact.Account}';
var Bill_State = sforce.connection.query("Select State__c From Account WHERE Id = '{!Contact.Account}'");
var rec = Bill_State.getArray("records");

if(rec[0].State__c == 'CA') 
{
alert("This is CA.");
//will replace later with logic to call vf page/apex class
}
else
{
alert("This is not CA");
}

 
Prabhat Kumar12Prabhat Kumar12
Hi Raghu,

Related fields are not supported in Javascript button in Salesforce as of now.

To solve the problem create a formula field called "Accountid" on Contact.

Formula for Accountid = Account.Id

and then use following code.
 
{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")} 


var Bill_State = sforce.connection.query("Select State__c From Account WHERE Id = '{!Contact.AccountId__c}'");
var rec = Bill_State.getArray("records");

if(rec[0].State__c == 'CA') 
{
alert("This is CA.");
//will replace later with logic to call vf page/apex class
}
else
{
alert("This is not CA");
}

Above code is working fine in developer org. Hope it will work for you.

Please let me know if you have any question.
Thanx.