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
tbardhantbardhan 

Apex before Insert trigger on Account and lead conversion

I have some Apex code that kicks off from the before Insert trigger for Account, conditionally updating some custom fields etc.  It appears to be working as intended when a new Account is created using the "New" button on the Account tab.

However, the same Apex does not appear to execute when Lead Conversion causes a new Account to be created.  The new Account from the converted Lead shows up fine except the fields that were supposed to be set by my Apex code were not set.

Being a newbie, before I dive into my code to try to figure out if something's wrong with it, I wanted to know if this is a known gotcha.  I.e., should the before insert trigger for Account be kicked off for a new Account from lead conversion in the same way as if the same Account data were entered manually via the "New" button?

(If not, any suggestions for a simple, maintainable workaround?)

I put a debug statement into the Apex method called by the before insert trigger, and unless I am not reading it correctly, it appears to be logged when the "New" button is used to create the Account but not when lead conversion is used to implicitly create the Account.

In this case, the development and testing is occurring on a Sandbox instance (logging in from tapp0.salesforce.com).

Thanks in advance.

T
steve_andersensteve_andersen
all my triggers that fire on Contact insert work from lead conversion. I don't have any Account insert triggers, so I can't say about your exact case, but I would be very surprised if lead conversion created an Account and didn't trigger your code.

Steve
tbardhantbardhan
Ok, I tested this further.  I 'unhooked' all the code in my Apex classes and just added simple field edits in 4 different triggers:  before Account insert, after Account insert, before Contact insert, after Contact insert.  And here's what does work and doesn't work:

AFTER insert triggers - work fine in all cases.

After Account insert and after Contact insert triggers DO fire whether the Account/Contact were created via the "New" button or via Lead conversion.  I tested this both on our 'DEV' snapshot for my customer (on test.salesforce.com) and on my own dev. ORG.

However:

BEFORE insert triggers appear to fire ONLY when the Account or Contact are created from the GUI (via the "New" button etc.).  They DO NOT fire when the Account or Contact are created via Lead conversion.

Examples of my simple test code for this are below.

Steve - one question:  are your triggers on Contact insert that work fine on lead conversion before or after insert triggers?

---- Test code below ---
(This is only to test the phenomenon I'm describing, not actual functionality ... each trigger updates the Account or Contact Description field respectively to keep track of which did what.)

BEFORE CONTACT INSERT:

trigger beforeContactInsert on Contact (before insert)
{
    /* TEMP -- REMOVE */
    for (Contact c : Trigger.new)
    {
        if (c.Description == null)
        {
            c.Description = '(before Contact insert trigger wrote this)';
        }
        else
        {
            c.Description = c.Description + '(before Contact insert trigger wrote this)';
        }
       
    }
}


AFTER CONTACT INSERT:

trigger afterContactInsert on Contact (after insert)
{
    /* TEMP -- REMOVE */
    for (Contact cReadOnly : Trigger.new)
    {
        Contact c = [select Id,Description from Contact where Id = :cReadOnly.Id];
        if (c.Description == null)
        {
            c.Description = '(after Contact insert trigger wrote this)';
        }
        else
        {
            c.Description = c.Description + '(after Contact insert trigger wrote this)';
        }
        update c;
    }
}


BEFORE ACCOUNT INSERT:

trigger beforeAccountInsert on Account (before insert)
{
    /* TEMP - REMOVE */
    for (Account acc : Trigger.new)
    {
        if (acc.Description == null)
        {
            acc.Description = '(before Account insert trigger wrote this)';
        }
        else
        {
            acc.Description = acc.Description + '  (before Account insert trigger wrote this)';
        }
    }
}


AFTER ACCOUNT INSERT:

trigger afterAccountInsert on Account (after insert)
{
    /* TEMP -- REMOVE */
    for (Account accReadOnly : Trigger.new)
    {
        Account acc = [select Id,Description from Account where Id = :accReadOnly.Id];
        if (acc.Description == null)
        {
            acc.Description = '(after Account insert trigger wrote this)';
        }
        else
        {
            acc.Description = acc.Description + '  (after Account insert trigger wrote this)';
        }
        update acc;
    }
}

------------------------- Results: ------------------------

When a Contact is created from scratch using the "New" button, the Contact description field reads:
'(before Contact insert trigger wrote this)(after Contact insert trigger wrote this)'

When an Account is created from scratch using the "New" button, the Account description field reads:
'(before Account insert trigger wrote this) (after Account insert trigger wrote this)'

BUT

When a Contact is created via Lead conversion, the Contact description field reads:
'(after Contact insert trigger wrote this)'

When an Account is created via Lead coversion, the Account description field reads:
'(after Account insert trigger wrote this)'


....

So what am I missing?  Might I have something configured wrong, or is it that you just can't use BEFORE insert triggers for Account and Contact if you need them to fire on lead conversion?  If the latter, that's good to know, albeit a bit lame.

Thanks.
steve_andersensteve_andersen
The only before trigger I have on Contact is to check to see if it has an Account Id. So, I wouldn't see that fire in Lead Conversion since Lead Conversion is handling the account assignment. Interesting behavior you're seeing.

Can anyone from sf.com shed some light on this?

Steve
tbardhantbardhan
CONFIRMED by Salesforce - this is a BUG.

My case with Salesforce support was escalated and finally sent to QA, who has confirmed this as a bug.

Before insert triggers (at least on Account/Contact) do NOT currently work on Lead conversion.  Use after insert triggers instead if you will be doing any Lead conversion.

They said they will get back to me on bug fixes.

T
dmchengdmcheng
For other readers having this problem: you need to enable Enforce Validation and Triggers for Lead Convert under Setup -> Customize -> Leads -> Settings.  If you don't see this checkbox, you have to open a ticket with Salesforce to switch on the feature and then you can tick the checkbox.
giribabu gedagiribabu geda

hi,

 

Is the bug fixed or still exists.Please let me know.

 

actually i have a contact after trigger which fires and updates the account field.

 

but this throws an error when creating a new account  via lead conversion.

 

the trigger ;

 

trigger Contact_AfterInsert on Contact (after Insert) 
{
    //Instanciating list of account object.
    List<Account> accounts =new List<Account>();
    for(Integer i=0; i< trigger.new.size();i++)
    {
      Integer NumberOfContacts=0;
      if(trigger.new[i].AccountId<>null)
      {   
          //get the number of contacts associated with the Lookup account   
          NumberOfContacts=[Select Count() from Contact where AccountId=: Trigger.New[i].AccountId];
          if(NumberOfContacts>0)
          {
            Account account =new Account(Id=trigger.new[i].AccountId);            
            account.NumberOfContacts__c=NumberOfContacts;
            accounts.add(account);
          }
          //Update the number of contacts for the Associated account
    		Update accounts;
      }
      else
      {
          //do nothing.
      }
    }
    
   

 the following is the error:

 

Error: Contact_AfterInsert: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 001R000000baMtrIAE; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please enter the Shipping Address fields.: [] Trigger.Contact_AfterInsert: line 19, column 7
dmchengdmcheng

It's in your error message.  You have an active validation rule.

giribabu gedagiribabu geda

hi,

thanks for the reply.

it was my mistake.i thought  the error was caused by the trigger.Because i tried it by deactivating the validation rule and

some body activated it again it seems.

 

latter when i tried by deactivating it it worked.

 

thanks.

JayantJayant

This is not a bug.

 

Default behavior on Lead Conversion does not fire any before insert  triggers on Account, Contact or Opportunity as well as before update triggers on Account and Contact.

 

Go to : Setup | Customize | Leads | Settings and check the check-box called Enable Validation and Triggers from Lead Convert.

This feature was released in Spring 08 and if your Production Org was created before that you should get it enabled by raising a case with SFDC.

 

Thing to note here is that if enabled it won't come alone but with the following as well -

 

- Before Insert triggers of Account, Contact and Opportunity would start firing during Lead Conversion.

- Before Update triggers of Account and Contact would start firing during Lead Conversion.

- Validation Rules on Account, Contact, Opportunity and Activity would start firing.

- Required custom fields on Account, Contact, Opportunity and Activity would be enforced.

- Workflow Rules that are not Time-Based on Lead object would start firing.

 

All these actions doesn't take place on Lead Conversion if the above mentioned feature is not activated.

 

You may find more details on this in SFDC Online Help. Just search for Lead Settings and this will be the first result.