Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two objects:

Inquiry
Patient

Patient has many inquiries so there is a lookup field on the inquiry object.

now here is my code:

Patient__c p = [Select Id from Patient__c where id='00QJ000000B4yXa'];
SObject container;
Schema.sObjectType sType = Schema.getGlobalDescribe().get('inquiry__c');
container = sType.newSObject();

I need a way to get the name of the lookup field on the child object .

in this example I would need the patient__c fieldname on the child record

share|improve this question
up vote 4 down vote accepted

You could use the SObject's getDescribe method (from the parent object), then call the DescribeSObjectResult's getChildRelationships() method. This will give you a list of Schema.ChildRelationship entries, each one providing the name (getRelationshipName) you'd use to query with, and the field (getField) that is the lookup field on the child object (it's an SObjectField, so you may need to run a describe on it if you need more information.


SObjectField parentField;
for(ChildRelationship rel: Patient__c.SObjectType.getDescribe().getChildRelationships()) {
    if(rel.getSObjectType() == Inquiry__c.SObjectType) {
        parentField = rel.getField();
        break;
    }
}
// Here, parentField is the Inquiry__c's lookup field to Patient__c
share|improve this answer
    
Damn, looks like you beat me to it :P – RenegadeCoder Mar 11 at 19:17

Here is one way to do that starting from the child side using the Contact object as an example:

SObjectType t = Contact.SObjectType;
for (SObjectField f : t.getDescribe().fields.getMap().values()) {
    if (f.getDescribe().getType() == DisplayType.Reference) {
        System.debug(f + ' is a lookup');
    }
}

See the DescribeFieldResult documentation for more detail on the field information that is available.

If you know the parent object (as you appear to) sfdcfox's approach is better.

(Incidentlly this describe API feels a bit strange in this area in that you declare the dependency at the child side but the parent-side has the more convenient API i.e. there is no getParentRelationships method.)

share|improve this answer

You can use the describe for Patient__c to get the child relationships, and loop through them until you find the one for Inquiry__c and get the field name from that like so:

for(Schema.ChildRelationship relation : sObjectType.Patient__c.getChildRelationships()){
    if(relation.getChildSObject() == Inquiry__c.sObjectType ){
        FieldTokenYouWant = relation.getField();
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.