0

Is there any way i can write following information from Org.

SF-Object Name  SF-Field Name   SF-API Name SF-Type SF-Length   SF-Possible Values

I intend to write them into csv files.

I was trying something like below

 string header = 'SF-Object Name, SF-Field Name , SF-API Name,SF-Type,SF-Length,SF-Possible Values \n';
string finalstr = header ;

List<String> stdObjectNames = new List<String>();

for ( Schema.SObjectType typ : Schema.getGlobalDescribe().values() ) {
    String sobjName = String.valueOf(typ);
    System.debug('stdObjectNames: ' + sobjName);
    Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get(sobjName).getDescribe().fields.getMap();
    for(String s : objectFields.keySet())
    {
        Schema.DescribeFieldResult lfieldLabel = objectFields.get(s).getDescribe();
        Schema.DisplayType dType = lfieldLabel.getType();
        string fieldType = String.ValueOf(dType);
        String fieldLabel = objectFields.get(s).getDescribe().getLabel();
        String fieldAPI = objectFields.get(s).getDescribe().getName();
        String fieldLength;
        String possibleValues;
        if(fieldType=='String'){
        fieldLength=String.valueof(fieldType.length());}

        if(fieldType=='Picklist'){
        possibleValues=processPicklistValues(s,objectFields);

        }
        if(possibleValues==null){
            possibleValues='N/A';
        }
                string recordString = '';
                recordString = '"'+sobjName+'","'+fieldLabel+'","'+fieldAPI+'","'+dType+'","'+fieldLength+'","'+possibleValues+'"\n';
                finalstr = finalstr +recordString;


    }
}

String processPicklistValues(String s,Map<String, Schema.SObjectField> objectFields){
  Schema.DescribeFieldResult F = objectFields.get(s).getDescribe();
String ps='';
List<Schema.PicklistEntry> P = F.getPicklistValues();
for(Schema.PicklistEntry s1: p){
ps=ps+s1.getValue()+',';
}
return ps;

}

Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(finalstr);
string csvname= 'OrgMetaDate.csv';
csvAttc.setFileName(csvname);
csvAttc.setBody(csvBlob);
  • 2
    Not posting this an answer as it's shameless self promotion, but our app Drop My Dossier (not even linking it) can dump all of your objects & fields into a word doc for you. – Matt Lacey Feb 28 '17 at 23:05
0

Getting the object name should be pretty straightforward. Everything else you need is on the DescribeFieldResult.

  • getLabel()
  • getName()
  • getLabel()
  • getLength() (String fields)
  • getPrecision() (Number fields)
  • getPicklistValues()

Note that there is a CPU cost associated with describing fields, and you will be better off describing once and caching the result. I would recommend a Row inner class something like:

public class Row
{
    final String label, name, type, objectName;
    final List<String> values;
    final Integer length;
    public Row(String objectName, SObjectField field)
    {
        this.objectName = objectName;

        DescribeFieldResult describe = field.getDescribe();
        name = describe.getName();
        label = describe.getLabel();
        type = describe.getSoapType();
        length = (type == SoapType.Double) ?
            describe.getPrecision() : describe.getLength();
        values = new List<String>();
        for (PicklistEntry entry : describe.getPicklistValues())
            values.add(entry.getValue());
    }
    public override String toString()
    {
        return String.join(new List<String> {
            objectName, label, name,
            String.valueOf(type),
            String.valueOf(length),
            String.join(values, '||')
        }, ',');
    }
}

Then your csv builder can use this inner class to simplify its logic:

List<String> rows = new List<String>();
for (SObjectType sObjectType : Schema.getGlobalDescribe().values())
{
    String name = String.valueOf(sObjectType);
    for (SObjectField field : sObjectType.getDescribe().fields.getMap().values())
        rows.add(new Row(name, field).toString());
}
String csvBody = String.join(rows, '\n');
  • Thanks Adrian, you are awesome :)..Your code is so neat and clean. Thanks a lot :) – David Mycka Mar 1 '17 at 4:59

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.