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
Ankit Singh 6Ankit Singh 6 

Write a trigger/class which fires when new lead/contact gets created

Hi

I need to write a trigger/class which fires when a new lead/contact gets created in salesforce, also in the same flow the trigger/class calls an 3rd party api.

Is it possible to call a 3rd party api from trigger/class, if so please let me know how to achieve this.

Thanks
 
Jigar.LakhaniJigar.Lakhani
Hello,

We cannot call any API from apex trigger, you must need to use asynchronous methods like @future(Future Method) or Queueable Interface from that you can execute your API request.

Future Method - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_invoking_future_methods.htm
Queueble Interface - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm

Thanks & Cheers,
Jigar
Amit Chaudhary 8Amit Chaudhary 8
PLease create trigger Framework according to below blog. That will help you
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

Create one Trigger "AccountTrigger"
 
trigger AccountTrigger on Account( after insert, after update, before insert, before update)
{

    AccountTriggerHandler handler = new AccountTriggerHandler(Trigger.isExecuting, Trigger.size);
    
    if( Trigger.isInsert )
    {
        if(Trigger.isBefore)
        {
            handler.OnBeforeInsert(trigger.New);
        }
        else
        {
            handler.OnAfterInsert(trigger.New);
        }
    }
    else if ( Trigger.isUpdate )
    {
        if(Trigger.isBefore)
        {
            handler.OnBeforeUpdate(trigger.New ,trigger.Old,Trigger.NewMap,Trigger.OldMap);
        }
        else
        {
            handler.OnAfterUpdate(trigger.New ,trigger.Old,Trigger.NewMap,Trigger.OldMap);
        }
    }
}

Create one Trigger Handler Class
public with sharing class AccountTriggerHandler 
{
    private boolean m_isExecuting = false;
    private integer BatchSize = 0;
    public static boolean IsFromBachJob ;
    public static boolean isFromUploadAPI=false;
    
    public AccountTriggerHandler(boolean isExecuting, integer size)
    {
        m_isExecuting = isExecuting;
        BatchSize = size;
    }
            

    public void OnBeforeInsert(List<Account> newAccount)
    {
        system.debug('Account Trigger On Before Insert');
    }
    public void OnAfterInsert(List<Account> newAccount)
    {
        system.debug('Account Trigger On After Insert');
    }
    public void OnAfterUpdate( List<Account> newAccount, List<Account> oldAccount, Map<ID, Account> newAccountMap , Map<ID, Account> oldAccountMap )
    {
        system.debug('Account Trigger On After Update ');
        AccountActions.updateContact (newAccount);
    }
    public void OnBeforeUpdate( List<Account> newAccount, List<Account> oldAccount, Map<ID, Account> newAccountMap , Map<ID, Account> oldAccountMap )
    {
        system.debug('Account Trigger On Before Update ');
    }

    @future 
    public static void OnAfterUpdateAsync(Set<ID> newAccountIDs)
    {

    }      
    public boolean IsTriggerContext
    {
        get{ return m_isExecuting;}
    }
    
    public boolean IsVisualforcePageContext
    {
        get{ return !IsTriggerContext;}
    }
    
    public boolean IsWebServiceContext
    {
        get{ return !IsTriggerContext;}
    }
    
    public boolean IsExecuteAnonymousContext
    {
        get{ return !IsTriggerContext;}
    }
}

Create one Trigger Action Class
public without sharing class AccountActions 
{
    public static void updateContact ( List<Account> newAccount)
    {
        // Add your logic here
    }
}



NOTE:- If you want to call API from Trigger then you should add Future method.

Please mark this as solution if this will help you.

Thanks
Amit Chaudhary
Jitendra RawatJitendra Rawat
Hi,

You have to use @future anotation in trigger to call the external api through trigger. You have to write some thing like below code :
public class TriggerHelper{
     @future(callout=true)
     public static void callApi(List<sObject> objlst){
          //write your code here for call the api
     }
}

call this utility method in your trigger like as following :
trigger contactTriggger on contact(after insert){
   TriggerHelper.callApi(trigger.new);
}

Please let me know if this solution help you.

Thanks
Jitendra