1

I have a custom object with many custom fields. Some of these fields are lookups to contacts. I have a visualforce page on which I need to list all contact fields for this custom object.

I am using the following code to try and get the list of related contact type fields, but it's not showing any at all, only system type fields like Attachment, ContentVersion, History, etc.

    Map<String, String> tmpList = new Map<String, String>();
    Schema.SObjectType thisObjType = Schema.getGlobalDescribe().get('My_Custom_Object__c');
    Schema.SObjectType conType = Schema.getGlobalDescribe().get('Contact');
    List<Schema.ChildRelationship> cRelations = thisObjType.getDescribe().getChildRelationShips();
    String message;
    for(Schema.ChildRelationship cr:cRelations){
        message += 'the field: ' + cr.getField().getDescribe().getLabel() +
           'the type: ' + cr.getChildSObject()  + 'and the relationship name: ' 
            + cr.getRelationshipName() + '\r\n';
        if(cr.getChildSObject() == conType){
                message += ': *******this is a con**********';
            Schema.DescribeFieldResult fdr = cr.getField().getDescribe();
            tmpList.put(fdr.getName(), fdr.getLabel());
        }
    }
    system.debug('the object type: ' + thisObjType + '\r\n' + message); 

I am able to query the related fields in the query editor, and I see them in the custom fields and relationship list.

Any ideas?

1

Try this

Map<String, Schema.SObjectField> soFields = Schema.getGlobalDescribe().get('My_Custom_Object__c').getDescribe().fields.getMap();

for(string fieldName : soFields.keySet()) {
    DescribeFieldResult soResult = soFields.get(fieldName).getDescribe();
    try {
        if( soResult.getReferenceTo().get(0) == 'Contact') {
            //
            // here you have the field and you can add your logic
            //
        }    
    }  catch(Exception e) {}
}

A bit unclear is, why you need this at all, because you should "know" you Contact-Lookups and process them statically with explicit reference and not dynamically via the schema methods.

The only use case I can think of is a package which you plan to install on different orgs which scans for an unknown amount of custom fields dynamically.

If your code is designed to run on an particular org, try to keep it simple and static.


The following is an edit by @hgolov (not tested and not verified by @UweHeim)

In order for the compare to work, you need the following:

Schema.SObjectType conType = Schema.getGlobalDescribe().get('Contact');

and in the if:

if ( soResult.getReferenceTo().get(0) == conType)
  • I need this because my client wants to be able to choose different linked contacts in different situations. – hgolov Oct 30 '14 at 9:40
  • Still not a reason: you can use e.g. the rendered attribute to control the visibility or JavaScript for the UI and the few possible field names in APEX differentiated by plain if() - Don't get me wrong, the dynamic access of myObject.get('fieldname') it totally cool - but you know those fieldnames and you can write the logic for your clients situations without the schema methods. So I still think what you're doing isn't necessary at all. But be that as it may: do my suggestion work? Where you able to use the code? If so please pick an answer & vote or let us now what is still missing. – Uwe Heim Oct 30 '14 at 10:29
  • My client wants to be able to pick from a list of related contact fields for use with a custom object. I'm writing a vf controller to allow them to do that. And yes, thank you it works! I just accepted. – hgolov Oct 30 '14 at 10:40
1

If your custom object Looks up to Contact, that means that your object is actually the child relationship. This means that Contact is actually the parent. From the way you described it, you have your logic backwards.

  • No, I think he just wants to scan dynamically for his lookups to contact. This should be possible via the schema methods. – Uwe Heim Oct 29 '14 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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