Salesforce Apex Trigger "isAPI" Context Variable

Is there a way to determine if a trigger is being executed by an API call or through the Salesforce web interface?

I would like to do something like this:

trigger Update_Last_Modified_By_API on My_Object__c (before update) { for (My_Object__c o : Trigger.New) { if (isAPI) { o.Last_Modified_By_API__c = datetime.now(); } } } 

(Currently using API version 25.0, although it will be updated to 26.0 soon)

+6
source share
1 answer

There is currently no standard way to report in a trigger what actually caused an update or insert (API, standard page layout, VF page and controller, some other Apex code, etc.). Here is a complete list of trigger context variables .

To achieve this, I would suggest creating a custom check box for the object, something like IsAPI__c (with a default value of false ). Then you will need to go through true for this field with any API call, and then check the field in the trigger for each entry in the package (just make sure you remember that reset is false when you make subsequent calls from user interface are not considered as API calls).

 trigger Update_Last_Modified_By_API on My_Object__c (before update) { for ( My_Object__c o : Trigger.New ) { if ( o.IsAPI__c ) { o.Last_Modified_By_API__c = datetime.now(); } o.IsAPI__c = false; } } 
+4
source

Source: https://habr.com/ru/post/925766/


All Articles