0

Can any one suggest me how I can get related objects and their fields of a parent object.Here is the code I have tried so far:

public void discoverRelatedAccessibleFields(sObject newObj) {
        this.accessibleRelatedFields = new List<String>();
        Map<String, Schema.SobjectField> fields = 
            newObj.getSObjectType().getDescribe().fields.getMap();
            System.debug(fields);
        for (String s : fields.keySet()) {
                System.debug(s);
            if ((s != 'Name') && (fields.get(s).getDescribe().isAccessible())) {
                this.accessibleRelatedFields.add(s);
            }
        }
    }
public Map<String,String> getRelatedObjects(String masterObjectName){
    System.debug(masterObjectName);
        Map<String,String> relatedObjectsMap = new Map<String,string>();
        List<Schema.Childrelationship> relatedObjectsList = Schema.getGlobalDescribe().get(masterObjectName).getdescribe().getChildRelationships();
        for (Schema.Childrelationship  relatedObject : relatedObjectsList) {
             if(relatedObject.getChildSObject().getDescribe().isUpdateable()
                    && 
                    relatedObject.getChildSObject().getDescribe().getKeyPrefix()!=null

                    &&
                    relatedObject.getChildSObject().getDescribe().isCreateable()
                )
                {
                    System.debug('***********');
                    relatedObjectsMap.put(relatedObject.getChildSObject().getDescribe().getName(),relatedObject.getChildSObject().getDescribe().getLabel());
                    String qid = ApexPages.currentPage().getParameters().get('id');
                    System.debug(qid);
                    for (String s : relatedObjectsMap.keyset())
                    {                   
                      string strName1 = relatedObjectsMap.get(s);  
                      System.debug(strName1);

                     discoverRelatedAccessibleFields(strName1);
                      String theQuery = 'SELECT ' + joinList(accessibleRelatedFields, ', ') + 
                                      ' FROM ' + strName1+ 
                                      ' WHERE Id = :qid';
                      System.debug(theQuery);                       
                    }

                    //return(Database.query(theQuery));    
                }
       }
       System.debug(relatedObjectsMap);
        return relatedObjectsMap;
    }

@strName1-getting related object name and getting compile error

"Method does not exist or incorrect signature: void discoverRelatedAccessibleFields(String) from the type DynamicObjectHandler".

Please help me with this

1 Answer 1

0

The error is telling you exactly where the issue lies. You have a type mismatch.

public void discoverRelatedAccessibleFields(sObject newObj)

This method takes an sObject instance. You're calling it with a String:

String strName1 = relatedObjectsMap.get(s);  
discoverRelatedAccessibleFields(strName1);

You can either change the method signature to take a String representing an sObject's API name or you can change the call to generate an sObject instance to pass. The former makes more sense, and would simply require you to change the way you call the Describe API inside discoverRelatedAccessibleFields() to use, for example, Schema.getGlobalDescribe() like you already do elsewhere.

3
  • Thanks for your response @David Reed.I understood the error but ho can I pass related object instance as parameter to call discoverRelatedAccessibleFields() method.Can you give any example ?
    – sivanee
    Aug 10, 2018 at 13:47
  • @sivanee, I would recommend, as discussed in my answer, that you instead change the method to accept a string instead of an sObject instance.
    – David Reed
    Aug 10, 2018 at 13:47
  • Thanks.Almost solved issue by writing this line in discoverRelatedAccessibleFields(): public void discoverRelatedAccessibleFields(String objectType) { //sObject updateObj; Schema.sObjectType objectDef = Schema.getGlobalDescribe().get(objectType).getDescribe().getSObjectType(); System.debug(objectDef);} But facing another issue while executing vf page the error is: "Attempt to de-reference a null object". Map<String, Schema.SobjectField> fields = objectDef.getDescribe().fields.getMap();
    – sivanee
    Aug 13, 2018 at 6:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .