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
sandy sfdcsandy sfdc 

Execution of Trigger (api or salesforce)

Hi All,

how come i know whether the trigger is executed by api or from normal salesfroce function.
Best Answer chosen by sandy sfdc
pconpcon
Again, you'd have to restrict it down to only a couple of users that only interact with the API.  Then just add logic to not operate your triggers if these users are the ones doing you DML operations.  This is much easier if you classify your triggers [1] then you could just do
 
User currentUser = [
    select isAPIUser__c
    from User
    where Id = :UserInfo.getUserId()
];

if (currentUser.isAPIUser__c) {
    return;
}

[1] http://blog.deadlypenguin.com/blog/2012/02/13/classifying-triggers-in-salesforce/

All Answers

pconpcon
You cannot know.  If you have to, you could always add a flag to a User and only use those users to call the api.  Then in your trigger query the current user and check that flag.
sandy sfdcsandy sfdc
Thanks pcon for ur reply,
 
Can u suggest me for the below Queries,

How can i avoid trigger firing from API calls??

We are working on integrating SF-Leads to third party and vice versa.
when any lead insert into SF,my trigger fires and sent particular Lead to third party through Future Callouts.But when Lead is added from API, again the particular  trigger fires and sent Lead info to third party(Creating Duplicate Lead).By avoding this type of unnecessary Api calls we are stopping calls by using extra parameter like "Source" field .


We are succeded in stoping the API-calls when inserting the Leads.But we are facing struggels doing update calls .Can you give me any ideas on this.

How can i stop update call when update comming from API??


 
pconpcon
Again, you'd have to restrict it down to only a couple of users that only interact with the API.  Then just add logic to not operate your triggers if these users are the ones doing you DML operations.  This is much easier if you classify your triggers [1] then you could just do
 
User currentUser = [
    select isAPIUser__c
    from User
    where Id = :UserInfo.getUserId()
];

if (currentUser.isAPIUser__c) {
    return;
}

[1] http://blog.deadlypenguin.com/blog/2012/02/13/classifying-triggers-in-salesforce/
This was selected as the best answer
sandy sfdcsandy sfdc
Thanks for the Update