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
NjangaNjanga 

How to get parent object using javascript

Am really stuck here!

I have 2 custom objects Candidates__c and Applicant__c.

Candidate__c is the Parent Object and Applicant__c is the child object.

Now i have a custom button on Applicant__c standard page that is executing a javascript:

What i want is to write a query to get Email from Candidate__c Object.

I have tried to write some query and this is what i have:

 

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

var result = sforce.connection.query("Select Main_Email__c From Candidate__c where Name={!Applicant__c.Candidate_Name__c}");

In this Query i can't get the unique record for that Applicant becouse am using Name in where clouse;

How do i use id in where clouse or how do i get Email for that Applicant.

Please Help!

Best Answer chosen by Admin (Salesforce Developers) 
kranjankranjan
Hi Njanga,

I think i missed a part before. You are creating your query for parent and trying to use child in filter which is not relevant. Your query should be on Applicant. SO it will be something like this:

var result = sforce.connection.query("Select Candidate_Name__r.Main_Email__c From Applicant__c where Id={!ApplicantId}");

Please replace the ApplicantId with the appropriate variable which gives you the applicant id for the current page.

Hope this helps.

All Answers

kranjankranjan
Hi Njanga,

You should use the following query for this:

var result = sforce.connection.query("Select Main_Email__c From Candidate__c where Id={!Applicant__c.Candidate_c}");

I am assuming that the API name of the Candidate field on Applicant object is Candidate__c

Hope this helps.
NjangaNjanga

Thank you for reply.

When i use Id={!Applicant__c.Candidate_Name__c} am getting this error:

 

My Candidate__c API name in Applicant__c is  Candidate_Name__c

Error:

A problem with the OnClick JavaScript for this button or link was encountered:

{faultcode:'sf:INVALID_QUERY_FILTER_OPERATOR', faultstring:'INVALID_QUERY_FILTER_OPERATOR:
Main_Email__c From Candidate__c where Id='David'
                            
ERROR at Row:1:Column:50
invalid ID field: David', detail:{UnexpectedErrorFault:{exceptionCode:'INVALID_QUERY_FILTER_OPERATOR', exceptionMessage:'
Main_Email__c From Candidate__c where Id='David'
                                         
ERROR at Row:1:Column:50
invalid ID field: David', }, }, }

 

This means {!Applicant__c.Candidate__c} is returning Name and not Id.

 

kranjankranjan
Yeah that is correct. it is storing the Name. Isn't this a lookup field? What is the type of Condidate_Name__c field?
NjangaNjanga

Candidate_Name__c is a lookup a field in Applicant__c

kranjankranjan
Hi Njanga,

I think i missed a part before. You are creating your query for parent and trying to use child in filter which is not relevant. Your query should be on Applicant. SO it will be something like this:

var result = sforce.connection.query("Select Candidate_Name__r.Main_Email__c From Applicant__c where Id={!ApplicantId}");

Please replace the ApplicantId with the appropriate variable which gives you the applicant id for the current page.

Hope this helps.
This was selected as the best answer