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
Ed055Ed055 

Costom button - execute javascript -Undefined field Error -

I'm trying to create a custom link button with "execute javascript" behavior. I'm getting error on line 
var cont = records[0].top_firm__r.Coverage_Channel__c;

Coverage_Channel__c is a picklist type field. Query using dev console does not throw an error and there is a value in that field. Also, I see values by doing the console.log(records );  I'm not quite sure why it throws  error "cannot read property 'Coerage_Channel__c' of undefined "
 
{!REQUIRESCRIPT("/soap/ajax/34.0/connection.js")}

 
    var result = sforce.connection.query(
        "SELECT Name,top_firm__r.Coverage_Channel__c " + 
        "FROM Contact " + 
        "WHERE Id = '{!Contact.Id}' " 
    );

    var records = result.getArray("records");

 
    if(records.length != 0){
        var cont = records[0].top_firm__r.Coverage_Channel__c;
window.top.location.href="someting........";
        }

 
Best Answer chosen by Ed055
rajat Maheshwari 6rajat Maheshwari 6

Ed055, 

I have changed this
var result = sforce.connection.query( "SELECT Name,top_firm__r.Coverage_Channel__c " + "FROM Contact " + "WHERE Id = '{!Contact.Id}' " );

to  var result = sforce.connection.query("SELECT Name, top_firm__r.Coverage_Channel__c FROM Contact WHERE Id = '{!Contact.Id}' " );

 

Please now check case sensitivity...please use same API Name, and be ensure you are following correct name.

 

Thanks

All Answers

LBKLBK
Can you put an alert on records and check what comes in there?
rajat Maheshwari 6rajat Maheshwari 6
Hi Ed055,

Please use below snippet, It will help you :) [Already Tested]
 
{!REQUIRESCRIPT("/soap/ajax/34.0/connection.js")}

 
    var result = sforce.connection.query("SELECT Name, top_firm__r.Coverage_Channel__c FROM Contact WHERE Id = '{!Contact.Id}' " );

    var records = result.getArray("records");

 
    if(records.length != 0){
        var cont = records[0].top_firm__r.Coverage_Channel__c;
window.top.location.href="someting........";
        }

Please let me know the results :)

Thanks
Rajat maheshwari
rajatzmaheshwari@gmail.com
Ed055Ed055
LBK - console.log shows me that 1 record was returned and had values for name and top_firm__r.Coverage_Channel__c

Rajat - not sure what change you made to my script. looks same to me. 
Ed055Ed055
just to add .. top_firm is account  lookup field on contact object.
rajat Maheshwari 6rajat Maheshwari 6

Ed055, 

I have changed this
var result = sforce.connection.query( "SELECT Name,top_firm__r.Coverage_Channel__c " + "FROM Contact " + "WHERE Id = '{!Contact.Id}' " );

to  var result = sforce.connection.query("SELECT Name, top_firm__r.Coverage_Channel__c FROM Contact WHERE Id = '{!Contact.Id}' " );

 

Please now check case sensitivity...please use same API Name, and be ensure you are following correct name.

 

Thanks

This was selected as the best answer
Ed055Ed055
This was case sensitivity issue. 
Thanks Rajat .