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
dwickdwick 

Retrieving the id of the master from the sObject of the detail.

Hi,

I have a master-detail relationship between 2 sObjects, we'll call them master__c and detail__c to make things easier.  Here's what I'm doing:

 

 

list<detail__c> lDetail = Database.Query('SELECT ... WHERE filterCondition');

 The reason for the use of database.query is the query string is actually determined dynamically.

 

Later on in the class, I would like to retrieve the Id of master__c within a particular row of the list.

 

 

sID = lObject[i].get(master__c),

Error: Compile Error: Variable does not exist: master__c

 

 

sID = lObject[i].get(lObject[i].master__c)

Error: Compile Error: Field expression not allowed for generic SObject at line 58 column 83

 

Any ideas here?  It's probably something simple, I can never seem to find the documentation I need.

 

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

The Id of master__c is just stored as a foreign key on a field on detail__c  - it's probably just called master__c or something - check on the ldetail object. If you include it in the select, you can just retrieve it like any other field on detail...

 

sID = lDetail[i].master__c;

All Answers

BritishBoyinDCBritishBoyinDC

The Id of master__c is just stored as a foreign key on a field on detail__c  - it's probably just called master__c or something - check on the ldetail object. If you include it in the select, you can just retrieve it like any other field on detail...

 

sID = lDetail[i].master__c;
This was selected as the best answer
dwickdwick

The reason it wasn't working is because lObject was an input to a function and was declared as a generic sObject. The detail object, lDetail, was passed to that function, however I guess you only have access to custom fields if the object is declared as the specific custom sObject type.  Good to know I, thanks for the help.