Find your content:

Search form

You are here

How get all objects and their fields as Json

 
Share

By means of Schema.getGlobalDescribe.Values () getDescribe ().getName () can receive names of objects.With Using method getDescribe().fields.getMap() we can get fields of object. It is necessary to serialize a line which would contain all objects and their fields. How it is possible to make it?


Attribution to: Pavel

Possible Suggestion/Solution #1

Code like this:

public class Describer {
    public class Sob {
        public String name;
        public String[] fields;
    }
    public static String serialize() {
        Sob[] sobs = new Sob[] {};
        for (...) {           // Loop over SObject names
           Sob s = new Sob();
           s.name = ...;      // Get name from meta data
           s.fields = ...;    // Get field names from meta data
           sobs.add(s);
        }
        return JSON.serialize(sobs);
    }
}

would produce a JSON string that looks like this:

[{"name": "Contact", "fields": ["FirstName", "LastName", ...]}, ...]

Is that what you want or do you want a copy of all the field meta data? Or something else entirely?


Attribution to: Keith C

Possible Suggestion/Solution #2

VFP error: Attempt to de-reference a null object. Shows only 2 System.debug: debug __1 and __2

    public String getserialize(){
        Sob[] sobs = new Sob[]{};
        List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();
        Map<String, Schema.SObjectType> gdMap = Schema.getGlobalDescribe();
        for(Schema.SObjectType f : gd){
            Sob s = new Sob();
            s.Name = (String)f.getDescribe().getName();
            System.debug('_____1' + s.Name);
            //sobs.add(s);
            Schema.Describesobjectresult dsr = gdMap.get(s.Name).getDescribe();
            Map<String, Schema.SObjectField> fieldMap = dsr.fields.getMap();
            for(String key : fieldMap.keySet()){
                System.debug('_____2' + key);
                s.Fields.add(key);
               System.debug('_____3' + key);
            }
            System.debug('_____4' + JSON.serialize(s));
            sobs.add(s);
        }
        System.debug(JSON.serialize(sobs));
        return JSON.serialize(sobs);
    }     

Attribution to: Pavel
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31940

My Block Status

My Block Content