function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anil KamisettyAnil Kamisetty 

Schema Question - Which sObjects support Triggers / Field Sets

Apex support Schema Call to get properties on the Salesforce objects. How can I tell if an Object Supports a Trigger or not ? Similarly, how can I say if it can have a Field Set created ? Is there any method which tell me this information.

If not using Schema, if there is another way to get this information, please share that too.

Note : If I know the Object name, I can use SCHEMA calls to get the Object and field set information (if it exists, not that it can support). Here the need is to know which objects supports Field Set / Trigger creation. If you can notice the SCHEMA.SObjectType results, many new objects will be noticed not just Salesforce objects (probably they are required for system).
Best Answer chosen by Anil Kamisetty
pconpcon
So you say you run into size issues if you run the following:
 
String sessionId = UserInfo.getSessionId();
String authorizationHeader = 'Bearer ' + sessionId;
String objectName = 'Account';
String host = URL.getSalesforceBaseUrl().toExternalForm();
String uri = host + '/services/data/v35.0/sobjects/';

Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(uri);
req.setMethod('GET');
req.setHeader('Authorization', authorizationHeader);

HttpResponse res = h.send(req);

Map<String, Object> body = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
List<Object> objs = (List<Object>) body.get('sobjects');
for (Object obj : objs) {
	Map<String, Object> objDesc = (Map<String, Object>)(obj);
    String name = (String) objDesc.get('name');
	Boolean isTriggerable = (Boolean) objDesc.get('triggerable');
    System.debug(System.LoggingLevel.ERROR, name + ': ' + isTriggerable);
}

All Answers

pconpcon
There are a lot of objects that support triggers and a log of fields that support field sets.  You can see which standard objects [1] support it by using the MetadataAPI [2] and seeing the data there under "triggerable".  (You can also get this data from workbench [3]).  I do not know of a way to programatically list if the object supports FieldSets.

[1] https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_list.htm
[2] https://developer.salesforce.com/blogs/developer-relations/2015/02/using-metadata-api-describe-objects.html
[3] http://workbench.developerforce.com
Anil KamisettyAnil Kamisetty
Hi Pcon.

Thanks for the reply. Seems like Field Sets need additional research. For the other part (Triggerable), are you saying we can use META DATA API. Workbench shows that for sure, but need an automated way to check in the APEX code.

Do u have any examples for the meta deta API to use in APEX ?
pconpcon
You can get the object information by doing the following
 
String sessionId = UserInfo.getSessionId();
String authorizationHeader = 'Bearer ' + sessionId;
String objectName = 'Account';
String host = URL.getSalesforceBaseUrl().toExternalForm();
String uri = host + '/services/data/v35.0/sobjects/' + objectName;

Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(uri);
req.setMethod('GET');
req.setHeader('Authorization', authorizationHeader);

HttpResponse res = h.send(req);

Map<String, Object> body = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
Map<String, Object> objDesc = (Map<String, Object>) body.get('objectDescribe');
Boolean isTriggerable = (Boolean) objDesc.get('triggerable');

You will need to add your Salesforce instance to your remote site settings.
Anil KamisettyAnil Kamisetty
This approach help me defitely, but running into issues. Salesforce has multiple object (more than 100). In one operation, more than 100 call outs are not allowed. Same time, if I dont pass the object name in the HTTP request, I run into size limitations (more than 32627 characters cannot be returns in one operations).

Do u have suggest any workarounds ? As of now, the workaround which I have is to process 100 http reuqests in one operation and resume it after sometime.
pconpcon
So you say you run into size issues if you run the following:
 
String sessionId = UserInfo.getSessionId();
String authorizationHeader = 'Bearer ' + sessionId;
String objectName = 'Account';
String host = URL.getSalesforceBaseUrl().toExternalForm();
String uri = host + '/services/data/v35.0/sobjects/';

Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(uri);
req.setMethod('GET');
req.setHeader('Authorization', authorizationHeader);

HttpResponse res = h.send(req);

Map<String, Object> body = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
List<Object> objs = (List<Object>) body.get('sobjects');
for (Object obj : objs) {
	Map<String, Object> objDesc = (Map<String, Object>)(obj);
    String name = (String) objDesc.get('name');
	Boolean isTriggerable = (Boolean) objDesc.get('triggerable');
    System.debug(System.LoggingLevel.ERROR, name + ': ' + isTriggerable);
}
This was selected as the best answer
Anil KamisettyAnil Kamisetty
It did work fine for me this time, not sure why I did get Size limitation (32K bytes) limitation earlier. thanks so much.

Your code provide a mechanism to obtain the list of objects which are triggerable in APEX. Will find another place to get the list of Fields Sets.