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 

Stop Account trigger when lead converting

Hi,

I need to stop account trigger(Insert Event) while im converting lead to account.

trigger accounttrigger on Account(after insert)
{
 set<Id>ids=new Set<Id>();
  if(trigger.isAfter && trigger.Isinsert){

    for(Account a:trigger.new){
      ids.add(a.id);
}

if(ids.size()>0){
//do my stuff
//this block cannot be called while Lead Convertion

}

}

}

So,please can any one help on this.

 
Best Answer chosen by sandy sfdc
pconpcon
If your conversion occurs inside a @future call you need to set the Boolean just before updating / insert the account record.  The life of boolean is reset when you go into the future call because it is a different transaction.

All Answers

pconpcon
To do this you'll need to have a static var in a non-trigger class that you set when you are doing your lead conversion.  This is much easier if you classify your trigger [1].  So you would have something like
 
public class AccountTrigger {
    public static Boolean CONVERTING_LEAD = false;

    // Trigger code starts here
        if (!CONVERTING_LEAD) {
            // do stuff
        }
}

and then in your lead convert code you would set CONVERTING_LEAD to true
 
AccountTrigger.CONVERTING_LEAD = true;

[1] http://blog.deadlypenguin.com/blog/2012/02/13/classifying-triggers-in-salesforce/
Praveen PosaPraveen Posa
The above solution will work for Account Trigger, If you want to stop all the triggers,Uncheck the following checkbox..
Setup--> Lead Setting --> Require Validation for Converted Leads .

It will stop the following actions as well. 
field validation rules, workflow actions, and Apex triggers. 
sandy sfdcsandy sfdc
Hi Praveen,
 
Even though it was unchecked,triggers are firing on Account,Contact and Opportunity
sandy sfdcsandy sfdc
Hi pcon,

we implemented below flow but we never succeded.can you please update us.i.e,we were failed to stop Account trigger(Insert) when Lead convertion.



My Lead Trigger

trigger Leadtrigger on Lead (after update) {

  set<Id>lids=new Set<Id>();
  if(trigger.isAfter && trigger.Isupdate){

    for(Lead a:trigger.new){
      lids.add(a.id);
      
      if(a.IsConverted==true){
        
             stopexceutioncontroller.CONVERTING_LEAD=true;
      }
}
if(lids.size()>0){

// calling my api call
myownclass.mymethod2(lids);
}
}
}

MY Account Trigger

trigger accounttrigger on Account(after insert)
{
  set<Id>ids=new Set<Id>();
  if(trigger.isAfter && trigger.Isinsert){
  
  if(!stopexceutioncontroller.CONVERTING_LEAD){
    //just im lookking this block never called when Lead convertion
    for(Account a:trigger.new){
      ids.add(a.id);
}

if(ids.size()>0){
//calling my api class
myownclass.mymethod(ids);
}}}}

MY API class

public with sharing class myownclass{

@Future(callout=true)
  
  public static void mymethod(Set<Id>ids) 
  
   { 
          system.debug('------------------Account API future-------------'+ids);
          //http callouts
   }
  
  public static void mymethod2(Set<Id>ids) 
  
   { 
          system.debug('------------------Lead API future-------------'+ids);
          //http callouts
   }

  
}

MY Static Class
public class stopexceutioncontroller{
    public static Boolean CONVERTING_LEAD = false;
}
pconpcon
If your conversion occurs inside a @future call you need to set the Boolean just before updating / insert the account record.  The life of boolean is reset when you go into the future call because it is a different transaction.
This was selected as the best answer
sandy sfdcsandy sfdc
Thanks pcon, Now its working..