You need to sign in to do that
Don't have an account?
tbardhan
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
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
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:
When an Account is created from scratch using the "New" button, the Account description field reads:
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.
Can anyone from sf.com shed some light on this?
Steve
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
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 ;
the following is the error:
It's in your error message. You have an active validation rule.
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.
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.