+1 vote
1 view
in Salesforce by (11.9k points)

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

I'd 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, though will soon be updating to 26.0)

1 Answer

+2 votes
by (31.6k points)
selected by
 
Best answer

There is no conventional way to in a trigger which can tell what actually might have caused an insert or update to happen. But, to achieve this, you can create a custom checkbox field on the object, something like IsAPI_c. After which you need to pass in true for that field with any API call and then check the field in your trigger for each record in the batch.

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; }

}

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...