2

Requirement: How can i Extract API names of all object in One shot into an external file format such as Excel. I have 300 objects and I need API names of all of them.

Please suggest.

6
  • Thanks.But i need API name and Object names of 200 objects among 1000 objects.Is this poosible to get extract in Excel sheet for specif objects – ajay bagdad Nov 4 '15 at 10:45
  • what is the criteria you will follow to get 200 out of 1000 – Himanshu Nov 4 '15 at 10:46
  • simply i have objects labels with me and i need objects API names for those objects.I am doing it manually by going salesforce org and copying the object names ,but is there any way to fetch directly in excel .Please help.urgent – ajay bagdad Nov 4 '15 at 10:53
  • @Himanshu this is slightly different from the question you reference owing to wanting to have it in Excel/spreadsheet format. – pchittum Nov 4 '15 at 12:03
  • @Peter yah I understood, I marked that duplicate as excel sheet requirement was not part of initial question. It came in later discussion. I will delete my previous comment. – Himanshu Nov 4 '15 at 12:06
3

The way I would do this (but it will take permissions to be able to create apex code in your org) would be as follows:

First have an Apex class that gets the objects you want. In my example it will do every object...and that is obviously overkill. In this instance I'm creating a simple getter method that returns the list of objects as wrapper class instances.

public class ObjectOutput {

  public List<ObjectWrapper> getObjects(){

    List<ObjectWrapper> objList = new List<ObjectWrapper>();

        Map<String, Schema.SObjectType> globalDescribe = Schema.getGlobalDescribe();

    for(Schema.SObjectType objType : globalDescribe.values()) { 

        // Describe each object and walk through each field
        Schema.DescribeSObjectResult objDescribe = objType.getDescribe();

        ObjectWrapper thisObj = new ObjectWrapper();

        thisObj.objApiName = objDescribe.getName();
        thisObj.objPrefix = objDescribe.getKeyPrefix();
        thisObj.objLabel = objDescribe.getLabel();

        objList.add(thisObj);
    }
    return objList;
  }

  public class ObjectWrapper {

    public String objPrefix {get;set;}
    public String objApiName {get;set;}
    public String objLabel {get;set;}

  }

}

To get it output to a format readable by Excel, you create a VF page.

<apex:page controller="ObjectOutput" contentType="application/vnd.ms-excel#objects.xls" >

  <apex:dataTable value="{!objects}" var="anObj">
    <apex:column headerValue="Name" value="{!anObj.objApiName}"/>
    <apex:column headerValue="Label" value="{!anObj.objLabel}"/>
    <apex:column headerValue="Prefix" value="{!anObj.objPrefix}"/>
  </apex:dataTable>

</apex:page>

It is worth noting that you're not actually creating an excel spreadsheet here. You're really just tricking the browser into thinking it is excel. And tricking excel into opening it. If you look at the actual content of the file, it is HTML. Fortunately, Excel is all too happy to make its best attempt to put it into a spreadsheet once you attempt to open it. And if you attempt to save it, you will, at that point, have a proper excel file.

Edit

For good measure, and to wrap this up nicely, here is a test class. If you want to use this in production you would need this as well.

@isTest
public class ObjectOutputTest {

    @isTest
    public static void testObjectOutput(){

        Map<String,ObjectOutput.ObjectWrapper> expectedObjects = new Map<String,ObjectOutput.ObjectWrapper>();

        Map<String, Schema.SObjectType> globalDescribe = Schema.getGlobalDescribe();

        for(Schema.SObjectType objType : globalDescribe.values()) { 

            // Describe each object and walk through each field
            Schema.DescribeSObjectResult objDescribe = objType.getDescribe();

            ObjectOutput.ObjectWrapper thisObj = new ObjectOutput.ObjectWrapper();

            thisObj.objApiName = objDescribe.getName();
            thisObj.objPrefix = objDescribe.getKeyPrefix();
            thisObj.objLabel = objDescribe.getLabel();

            expectedObjects.put(thisObj.objApiName,thisObj);
        }

        ObjectOutput controller = new ObjectOutput();

        List<ObjectOutput.ObjectWrapper> controllerObjs = controller.getObjects();

        System.assertEquals(expectedObjects.size(),controllerObjs.size(),'there are a different number of test objects than controller objects');

        for (ObjectOutput.ObjectWrapper obj: controllerObjs){
            ObjectOutput.ObjectWrapper compareObj = expectedObjects.get(obj.objApiName);
            System.assertEquals(compareObj.objApiName,obj.objApiName,'the expected prefix of object '+obj.objApiName+' does not match the test example');
        }

    }

} 
0

You can use getDescribe().getLabel() method to get label and filter the results as shown below.

List<string> SObjectList = new List<string>();

for(Schema.SObjectType objTyp : Schema.getGlobalDescribe().Values()){
String name = objTyp.getDescribe().getName();
// Exclude all the unwanted Sobjects e.g. History, Share etc..

if(!name.containsignorecase('history') && !name.containsignorecase('tag')&&
!name.containsignorecase('share') && !name.containsignorecase('feed')){  

 /********Compare label with your custom object name*********/
 if(objTyp.getDescribe().getLabel()=='YOUROBJECTNAME')
 {    
  SobjectList.add(name);
  System.debug( 'Name : ' + name);
  }
}

}
0

If you require production objects in Excel, but have no time for a deployment: Tag values as html table >> send yourself an email >> copy & paste in textfile >> save as .html >> open with browser >> copy & paste into Excel. Thanks Peter for the great example!

String objectNamesAndLabels = '<table style="width:100%"><tr><th>Name</th><th>Label</th></tr>';
Map<String, Schema.SObjectType> globalDescribe = Schema.getGlobalDescribe();

for(Schema.SObjectType objType : globalDescribe.values()) {
    
    Schema.DescribeSObjectResult objDescribe = objType.getDescribe();   
            objectNamesAndLabels = objectNamesAndLabels + '<tr><td>' 
                                                        + objDescribe.getName() 
                                                        + '</td><td>' + objDescribe.getLabel() 
                                                        + '</td></tr>';
}
objectNamesAndLabels = objectNamesAndLabels + '</table>';

Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new String[] { 'name@domain.com' };
message.optOutPolicy = 'FILTER';
message.subject = 'Objects Names & Labels';
message.plainTextBody = objectNamesAndLabels;
Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message};
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
 
if (results[0].success)
{
    System.debug('The email was sent successfully.');
} else
{
   System.debug('The email failed to send: ' + results[0].errors[0].message);
}

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.