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?
2 Answers
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?
-
All information on object is necessary to me: fields, field type, and its values. Thanks.– PavelCommented Apr 5, 2014 at 8:14
-
@Pavel You are going to hit governor limits pretty quickly then as you will have to make a describe call for each field. The governor limit I though was 100 of these calls but I vaguely recollect reading somewhere that this may no longer be applied. Best to test before you go much further.– Keith CCommented Apr 5, 2014 at 14:24
-
@Pavel If you want all the field meta data you should see if DescribeFieldResult itself can be serialized to JSON to save you having to reproduce all the fields and assign them. You are aware that the platform has web service APIs to extra this information?– Keith CCommented Apr 5, 2014 at 14:26
-
Thanks for information. I only started studying salesforce therefore I don't know much– PavelCommented Apr 5, 2014 at 14:59
-
Hi, I'm trying to do something similar... a json result set of all objects, and their respective fields with all attributes (name, type, id, etc.). Where do you run your code above from? Where do you view the result set? Thank you.– joshCommented Feb 16, 2016 at 7:35
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);
}
-
The error is because you can only "add" once an array (or list) as assigned to "fields". Or you could do it all in one go by just using "s.Fields = new List<String>(fieldMap.keySet());" which turns the set into a list and then assigns it (eliminating the loop).– Keith CCommented Apr 5, 2014 at 14:20