How to find whether an object supports trigger in Salesforce?

How to find whether an object supports trigger in Salesforce?

triggerable property from the object schema can be used to determine whether Trigger can be developed on that object.

Sample Code(Trigger not allowed):

Map < String, Schema.SObjectType > globalDescription = Schema.getGlobalDescribe();
Schema.SObjectType sObj = globalDescription.get( 
    'ConversationEntry' 
);
Schema.DescribeSObjectResult sobjDesc = sObj.getDescribe();
String sObjStruct = JSON.serialize( 
    sobjDesc 
);
System.debug( 
    'sObjStruct is ' + 
    sObjStruct 
);
JSONParser parser = JSON.createParser( 
    sObjStruct 
);

while ( parser.nextToken() != null ) {
    
    if (
        parser.getCurrentToken() == JSONToken.FIELD_NAME &&
        parser.getText() == 'triggerable'
    ) {
        
        parser.nextToken();
        System.debug(
            'triggerable is ' + 
			parser.getBooleanValue()
		);
        break;

    }

}

Output:

Sample Code for Account Object(Trigger is allowed):

Map < String, Schema.SObjectType > globalDescription = Schema.getGlobalDescribe();
Schema.SObjectType sObj = globalDescription.get( 
    'Account' 
);
Schema.DescribeSObjectResult sobjDesc = sObj.getDescribe();
String sObjStruct = JSON.serialize( 
    sobjDesc 
);
System.debug( 
    'sObjStruct is ' + 
    sObjStruct 
);
JSONParser parser = JSON.createParser( 
    sObjStruct 
);

while ( parser.nextToken() != null ) {
    
    if (
        parser.getCurrentToken() == JSONToken.FIELD_NAME &&
        parser.getText() == 'triggerable'
    ) {
        
        parser.nextToken();
        System.debug(
            'triggerable is ' + 
			parser.getBooleanValue()
		);
        break;

    }

}

Output:

Using this sample apex code snippet, we can find whether the object supports trigger or not.

Leave a Reply