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
MDXHikerMDXHiker 

SOQL Join

In the opp I have a lookup relationship to a custom object which is not a child.

So on the Opp record there is no related field which specifies the value that is selected on this custom object.

 

How do I write a join in SOQL to query the values for a given opp ?

 

If I bring these tables down to SQL Server the query would look something like

 

 

SELECT
o.Id [OpportunityId],
gp.CusObj_ID__c [GPID]
FROM Opportunity o
LEFT JOIN CusObj__c gp
ON o.CusObj__c = gp.Id

 Thanks

 

 

Message Edited by MDXHiker on 04-23-2009 04:24 PM
Best Answer chosen by Admin (Salesforce Developers) 
werewolfwerewolf

You reference it just as it is in the query.  In this case it would be

 

opp.custobj__r.custObj_Id__c

All Answers

SuperfellSuperfell

select id, custobj__r.custObj_Id__c from opportunity

 

see the SOQL section of the API docs. 

MDXHikerMDXHiker

Thanks Simon.

 

I have tried that but I get  'Object reference not set to an instance of an object'

SuperfellSuperfell
thats from the code reading the results, the query actually ran fine.
MDXHikerMDXHiker

Could you please elaborate ? I am testing using sforce explorer and it gives this error.

 

If I try with a particular opp which has an associated CusObj entry - same results. Maybe I am missing something

werewolfwerewolf
sforce Explorer can be a little funny sometimes.  Perhaps try it with the Force.com IDE instead -- that tends to work better.
MDXHikerMDXHiker

You are right. IDE executes this fine and I can see the result in the debug

 

How do I ref this variaable in the code ?

If I have a query like

Opportunity opp=[SELECT Name, custobj__r.custObj_Id__c FROM Opportunity where Id='xxxxx'];

 I would access name as opp.Name. How do I get custObj_id__c?

 

Thanks

 

 

 

Message Edited by MDXHiker on 04-24-2009 10:33 AM
werewolfwerewolf

You reference it just as it is in the query.  In this case it would be

 

opp.custobj__r.custObj_Id__c

This was selected as the best answer
MDXHikerMDXHiker
Thanks for your help, werewolf and Simon