+ Start a Discussion
NjangaNjanga 

Get a value from json

i  have a javascript that is querying my custom object. what i want is to get an email value. this is my javascript

 

{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}
var result = sforce.connection.query("Select Main_Email__c From Candidate_ATS__c where Name ='{!JobApplicant_ATS__c.Candidate_Name__c}' ");
var records = result.getArray("records");

//If there are no  record
if (records.length == 0){
alert("no record");
}
//If there is a record
else{
alert(records);
}

 

When i alert records this is what am getting:

 

{type:'Candidate_ATS__c', Id:null, Main_Email__c:'abc@abc.com', }

 

pliz help get my email i.e. abc@abc.com

WizradWizrad

First, I would study up on javascript.

 

Second, I would change my alert statement to:

 

...
else {
  var record = records.length ? records[0] : records;
  alert(record.Main_Email__c);
}
...

 

Also, it seems short sighted to have a where clause select, what should be a single record, based off a name.  What happens when there are two "Tom Smith" in your system?

 

NjangaNjanga

Thank you for reply. 

You have really help me alot.

I realised that and the problem i am having is to get the parent id from the child object.

i used same query in APex code but canged Name=:JobApplicant_ATS__c.Candidate_Name__c to id=:JobApplicant_ATS__c.Candidate_Name__c in where clouse and it worked well. but in javascript its returning name instead of id so am getting invalid id.