5

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()){
        CustomObjectsList.add(f.getDescribe().getName());
    }
}

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

7
  • 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. Jan 30, 2015 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. Jan 30, 2015 at 10:18
  • @SamuelDeRycke Is there any way to get CreatedByUser of Custom Object in Salesforce ?
    – ChamingaD
    Jan 30, 2015 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, 2015 at 10:32
  • Ok, I misunderstood your question then. Jan 30, 2015 at 10:35

2 Answers 2

5

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.setMethod('GET');
    request.setEndpoint(endpoint);
    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.

1

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.

1
  • 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, 2016 at 5:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .