+ Start a Discussion
JeffStevensJeffStevens 

Nested SOQL from child to parent

I need to get the actual Parent ID from the Account object while querying a custom child object.  

 

I have an existing soql that looks like this...

 

// Build IDNumber list

Map<String,ID_Number__c> IDNumbers = new Map<String,ID_Number__c> (

    [select ID__c, Parent_Rate__c ),

    from ID_Number__c

    Where ID__c IN :idsToQuery] );

 

 

 

When I try this - I get an "enexpected token; 'from' error...

 

Map<String,ID_Number__c> IDNumbers = new Map<String,ID_Number__c> (

    [select ID__c, Parent_Rate__c ), (Select Parent, from Account),

    from ID_Number__c

    Where ID__c IN :idsToQuery] );

 

Any guidance?

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
VJSFDCVJSFDC

Try,

 

Select customAccountid, customobjectfield2, account__r.parentid from Customobject where customAccountid = account__r.id

 

where account__r is the relationship name from Custom Object to Account.

 

Also please check below link: which can help you in forming the Queries for the requirment.

http://www.salesforce.com/us/developer/docs/api/index_Left.htm#StartTopic=Content/sforce_api_calls_soql.htm

All Answers

VJSFDCVJSFDC

hi,

 

What is idsToQuery referring too?Also to get the Parent ID from the Custom Child Object you need to use the Relationship to Query  the corresponding Parent Id.

 

JeffStevensJeffStevens

Thanks.

 

the idsToQuery is a set that I'm building so my trigger will run in buld.

 

So, the custom object has the Account ID on it (the custom is a Master-Detail(Account) ).

 

So would the subquery be (select Parent, from Account where Account.ID = ID_Number__c.Account ) ?

spraetzspraetz

You have a comma after Parent in (Seleect Parent, from Account)

VJSFDCVJSFDC

Try,

 

Select customAccountid, customobjectfield2, account__r.parentid from Customobject where customAccountid = account__r.id

 

where account__r is the relationship name from Custom Object to Account.

 

Also please check below link: which can help you in forming the Queries for the requirment.

http://www.salesforce.com/us/developer/docs/api/index_Left.htm#StartTopic=Content/sforce_api_calls_soql.htm

This was selected as the best answer
JeffStevensJeffStevens

Thanks!