You need to sign in to do that
Don't have an account?
Morgan Marchese
When converting a lead to an account, I want the new account's OwnerId copied to another field, but I've encountered a problem I can't figure out (code inside)
Hi all,
Here is some very basic before insert code I am using in an account trigger - my intent is to copy the OwnerId (or OwnerName) to a text field called Original_Account_Owner__c. This field would retain a historical record of who the original account owner was so that we can always report on that data even if the current owner is changed.
This works perfectly if I convert a lead for myself, with myself set as the Record Owner during the conversion process...
However:
If I have two employees (Emp A and Emp B) and Emp A is converting a lead but during the conversion process he/she sets the Record Owner to Emp B, the end result after my trigger runs is that the "Original Account Owner" is showing Emp A and the "Account Owner" is showing Emp B when in reality I want the "Original Account Owner" and the "Account Owner" to BOTH be Emp B because that was the Record Owner selected during the conversion.
My assumption was that if the record owner is selected during conversion, it would be the one that the new account record is created with - so my trigger should just pick up the Account Owner prior to insert and set it on my custom field... instead, it looks like it assumes that I am the Record Owner during insert and then quickly changes it afterwards?
Is there any way I can combat this and get the end result I am looking for, or am I stuck because of the nature of the account creation/reassignment process during conversion?
Many thanks for your input everyone!
Here is some very basic before insert code I am using in an account trigger - my intent is to copy the OwnerId (or OwnerName) to a text field called Original_Account_Owner__c. This field would retain a historical record of who the original account owner was so that we can always report on that data even if the current owner is changed.
if (Trigger.isBefore && Trigger.isInsert) { for(Account acct : Trigger.new){ // Check that the owner is a user (not a queue) if( ((String)acct.OwnerId).substring(0,3) == '005' && acct.Original_Account_Owner__c == null ){ acct.Original_Account_Owner__c = acct.OwnerId; } else{ // In case of Queue (which shouldn't happen), System Debug. System.debug('found a queue when we shouldn't have...' + acct.OwnerId); } }
This works perfectly if I convert a lead for myself, with myself set as the Record Owner during the conversion process...
However:
If I have two employees (Emp A and Emp B) and Emp A is converting a lead but during the conversion process he/she sets the Record Owner to Emp B, the end result after my trigger runs is that the "Original Account Owner" is showing Emp A and the "Account Owner" is showing Emp B when in reality I want the "Original Account Owner" and the "Account Owner" to BOTH be Emp B because that was the Record Owner selected during the conversion.
My assumption was that if the record owner is selected during conversion, it would be the one that the new account record is created with - so my trigger should just pick up the Account Owner prior to insert and set it on my custom field... instead, it looks like it assumes that I am the Record Owner during insert and then quickly changes it afterwards?
Is there any way I can combat this and get the end result I am looking for, or am I stuck because of the nature of the account creation/reassignment process during conversion?
Many thanks for your input everyone!
Go to
App Setup > Customize > Leads > Settings
enable "Enforce Validation and Triggers from Lead Convert".
Does this fix your problem?
Ty,
Adrian
Thanks for your reply. I looked and I don't see an option for "Enforce Validation and Triggers from Lead Convert", but I think that it may have been renamed because I do have an option for "Require Validation for Converted Leads" which says "When users convert leads, enforce: required field settings, field validation rules, workflow actions, and Apex triggers."
That option is already turned on though. The Apex trigger IS running, it's just grabbing the wrong OwnerId because (I presume) that Before Insert the owner is still being considered as Emp A instead of Emp B even though I selected Emp B for Record Owner during convert.
Also, you can replace the trigger with a WF rule ("clicks" are better than code :) ). Just be careful for when you fire it (insert/update etc)
Another solution would be to enable Field History on the Account Owner field. You can then report on the changes and see its initial value
Ty,
Adrian
The reason that the WFR didn't work I presume is because I only want it to fire when Original_Account_Owner__c == null so by nature of WFR it fires during create and sets the field to the wrong person and will never set it correctly again because I don't want to overwrite the field - if that makes sense.
Regarding your Field History solution - would this allow me to report on, for example "Cancelled accounts by Original Account Owner" ? Because our scenario is that when employees leave, we transfer their accounts to another rep, but for historical purposes in tracking cancellations etc, we want to know who the real original was.
We need to know exactly the order of operations when user clicks Convert...
IF it goes like 1. insert Account, 2. update Account Owner; 3. update Lead with isConverted and related ConvertedAccountId, ConvertedContactId etc fields then we might be able to use a trigger on Lead:
Smth like:
(it's not compiled so please excuse errors; also not bulkified)
But this would only work if the Lead update occurs after the Account update...
Put some System.debugs or use the Dev Console to see order of operations. Let me know how it goes
As for the field history stuff, again, best way is to test it :) . I remember that you could report on AccountFieldHistory or some sort of sObject, but that's all
Happy Friday,
Adrian