0

Here I am trying to get related object and their fields dynamically.In debug I am getting related objects and fields but error is while loading VF page.Here is my code: Apex Class:

public class DynamicObjectHandler {

    // This class acts as a controller for the DynamicObjectViewer component

    private String objType;
    private String relobjType;
    private String o ;
   // private String recordId ;
    private List<String> accessibleFields;
    private List<String> accessibleRelatedFields;
    public sObject obj { 
        get; 
        set {
              setObjectType(value);
              //setRelatedObjectType(value);
              System.debug(value);
              discoverAccessibleFields(value);

              obj = reloadObjectWithAllFieldData();
              getRelatedObjects(this.objType);

        } 
    }
    public sObject obj1 { 
        get; 
        set {              
              getRelatedObjects(this.objType);
              //obj1 = getRelatedObjects();              
        } 
    }

    // The sObject type as a string
    public String getObjectType() {
         o = this.objType;
          return(this.objType);

    }

    public String setObjectType(sObject newObj) {
        this.objType = newObj.getSObjectType().getDescribe().getName();
        System.debug(this.objType);

        return(this.objType);

    }

    // List of accessible fields on the sObject
    public List<String> getAccessibleFields() {
        System.debug(this.accessibleFields);
        return(this.accessibleFields);
    }
    public List<String> getAccessibleRelatedFields() {
        System.debug(this.accessibleRelatedFields);
        return(this.accessibleRelatedFields);
    }


    public void discoverAccessibleFields(sObject newObj) {
        this.accessibleFields = 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.accessibleFields.add(s);
            }
        }
    }
        public void  discoverRelatedAccessibleFields(String objectType) {
        //sObject updateObj;
        Schema.sObjectType objectDef = Schema.getGlobalDescribe().get(objectType).newSObject().getSObjectType();  
        System.debug(objectDef);

        sObject sObj = Schema.getGlobalDescribe().get(objectType).newSObject();
        System.debug(sObj);
        this.accessibleRelatedFields = new List<String>();
        Map<String, Schema.SobjectField> fields = objectDef.getDescribe().fields.getMap();
            System.debug(fields);            
        for (String s : fields.keySet()) {
                System.debug(s);
            if ((s != 'Title') && (fields.get(s).getDescribe().isAccessible())) {
                this.accessibleRelatedFields.add(s);
            }
        }
        //return();
    }
     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(Database.query(theQuery));
        return relatedObjectsMap;
    }

    public sObject reloadObjectWithAllFieldData() {

        String qid = ApexPages.currentPage().getParameters().get('id');
        System.debug(qid);
        String theQuery = 'SELECT ' + joinList(getAccessibleFields(), ', ') + 
                          ' FROM ' + getObjectType() + 
                          ' WHERE Id = :qid';
         System.debug(theQuery);                 
        return(Database.query(theQuery));       
    }

    // Join an Apex List of fields into a SELECT fields list string
    public static String joinList(List<String> theList, String separator) {

        if (theList == null)   { return null; }
        if (separator == null) { separator = ''; }

        String joined = '';
        Boolean firstItem = true;
        for (String item : theList) {
            if(null != item) {
                if(firstItem){ firstItem = false; }
                else { joined += separator; }
                joined += item;
            }
        }
        return joined;
    }
}

VF Component: --> Label API Name Type Value
Here how to initialize sObject,Can anyone suggest where I am doing and why this error shows?Any help would be really appreciated.

  • Can you update with the visualforce page where you have called the component. I want to see what attributes you are passing. – sanket kumar Aug 13 '18 at 11:07
  • 1
    Which line is throwing that exception? Can you include the full message? – Lightning Evangelist Aug 13 '18 at 11:08
  • Attempt to de-reference a null object Error is in expression '{!obj}' in component <c:dynamicobjectviewer> in component c:dynamicobjectviewer FULL MESSAGE – sivanee Aug 13 '18 at 11:19

Your Answer

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

Browse other questions tagged or ask your own question.