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
Sushant RaoSushant Rao 

Traversing look-up field relationships in Javascript.

I have button for an opportunity on which I have the following javascript for an onclick event. Can I not use cross-refrence look-ups in javascript like can in validation rules.

 

if( {!Opportunity.Account.Name} == 'xxx' ) {
 alert('This account cannot be created !');

}

 

Error: Field Opportunity.Account.Name does not exist. Check spelling.

 

My original intent was to use a custom object like so ...

 

if( {!Opportunity.Contract__r.status__c} == 'xxx' ) {
 alert('This  contract name cannot be created !');

}

 

Any help would be appreciated ! Thanks !

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force

If you want to access Cross reference record Data in Javascript,

Simply use Ajax toolkit and get the Parent record by SOQL

and Validate it

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")}

var result = sforce.connection.query("Select field1,field2 from Contract where OpportunityId = '"+i.id+"'"); 

on the basis of your  result drive your validate logic

}

Thanks,

Bala

All Answers

Pradeep_NavatarPradeep_Navatar

In my opinon, you can’t use cross object reference in java script for custom button. You should use validation rule to validate lookup field. You can access reference object’s fields there.

 

Hope this helps.

b-Forceb-Force

If you want to access Cross reference record Data in Javascript,

Simply use Ajax toolkit and get the Parent record by SOQL

and Validate it

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")}

var result = sforce.connection.query("Select field1,field2 from Contract where OpportunityId = '"+i.id+"'"); 

on the basis of your  result drive your validate logic

}

Thanks,

Bala

This was selected as the best answer
Sushant RaoSushant Rao

Thanks! I guess both these solutions would work.

 

I just added the fields that I wanted as formula fields and then added it to the js validation.

b-Forceb-Force

If solution work for you,

Then please accept particular Post as solution and Helps our community

 

Enjoy with SF

 

Thanks,

Bala