I need to get list of Custom Objects in a Salesforce org and find Created By User of each of them (Created By of object, not the records) .

Is it possible? I can get string list of custom objects as follows:

List<String> CustomObjectsList  = new List<String>();
List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();  
for(Schema.SObjectType f : gd)
    if (f.getDescribe().isCustom()){

Now I need to find object creator of each (not record creator).

  • ChamingaD, could you be more specific about what you're trying to do and what isn't working for you ? Are you doing this from apex,reports, api,.. ? Consider that providing more information in a question will lead to better answers. – Samuel De Rycke Jan 30 '15 at 10:17
  • 1
    If I'm not getting your question wrong, the answer is trivial, and you should use google instead of asking question. – Bart Juriewicz Jan 30 '15 at 10:18
  • @SamuelDeRycke Is there any way to get CreatedByUser of Custom Object in Salesforce ? – ChamingaD Jan 30 '15 at 10:31
  • @BartJuriewicz Google didn't help. I need to query createdby field of a custom object (not created by in object records). – ChamingaD Jan 30 '15 at 10:32
  • Ok, I misunderstood your question then. – Bart Juriewicz Jan 30 '15 at 10:35

As of API Version 33.0, you can query CreatedBy information via the Tooling API. Queries including this field and using API Version 32.0 or below will fail.

SELECT CreatedById, CreatedBy.Name, DeveloperName FROM CustomObject

Query Editor (Use Tooling API)

And since you asked how to do it via Apex, you can hit the Tooling API from Apex as follows:

public static Map<SObjectType, Id> getCustomObjectCreatedBy()
    String query = EncodingUtil.urlEncode(
        'SELECT CreatedById, DeveloperName FROM CustomObject', 'UTF-8'
    String endpoint = URL.getSalesforceBaseUrl().toExternalForm() +
        '/services/data/v33.0/tooling/query?q=' + query;

    HttpRequest request = new HttpRequest();
    request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
    request.setHeader('Content-Type', 'application/json');
    String response = new Http().send(request).getBody();
    Map<String, Object> payload = (Map<String, Object>)JSON.deserializeUntyped(response);

    Map<String, SObjectType> sObjects = Schema.getGlobalDescribe();
    Map<SObjectType, Id> output = new Map<SObjectType, Id>();
    for (Object data : (List<Object>)payload.get('records'))
        Map<String, Object> record = (Map<String, Object>)data;
        String developerName = (String)record.get('DeveloperName');
        String createdById = (String)record.get('CreatedById');
        output.put(sObjects.get(developerName), createdById);
    return output;

I cached the global describe because it makes this snippet run about 7x faster in my sandbox. It's still not exactly fast...This snippet takes about a half second to run.


Update: This answer is outdated,the solution is posted in Adrian's answer. No, I don't think so. While you can use Schema to describe the field, i'm not sure how to get the actual data.

System.debug(Schema.SObjectType.Vehicle__C.fields.createdBy); //does work

I would imagine this info to be available through the Metadata or Tooling API, and neither include the field in their Custom Object type documentation either.

customObject in the Metadata API , customObject in the Tooling API

This usually means it's not available from apex either.

  • Fun fact, it was introduced in Version 33. Looks like that was Spring 15, released just a couple weeks after you posted this. Yay. It doesn't look like they included any mention of it in the Release Notes, though they do mention many new fields available to the Tooling API. – Adrian Larson Aug 31 '16 at 5:47

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.