+ Start a Discussion
cjarliecjarlie 

Working with SObject Lookup References?

Hi Everyone,

 

i've got some trouble by using Lookup Reference Fields from sObjects. 

Ive created a sObject called 'Shapes' with fields 'Shape' and 'Name' as autonummered id field. Secondly, ive created a sObject 'Materials' with an Look-up field related to 'Shapes':

 

 

for(Material__c mtr : [select Name, Shape_ID__c from Material__c]){ String shapeID = (mtr.Shape_ID__c);//...

 returns me a Reference (Exp: a07A0000000dVGCIA2

 How do i Approach the corresponding sObject to get its field value 'Shape' ?

 

Ive tried:

 

shapeC = [select Shape__c from Shape__c where Name = mtr.Shape_ID__c];

 but this is obv. not working...

 

andy ideas? 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
mikefmikef

You need to use the relationship name.

 

String shapeName; for(Material__c mtr : [select Id, Shape__r.Shape__c from Material__c]){ shapeName = mtr.Shape__r.Shape__c; } //this assumes that Shape__c is the field name on the Shape sObject that you store the name of the shape. If you use Name then change it to Shape__r.Name