I have a use case where I need to update the Lead.OwnerId
during lead conversion. I want the resulting Account, Opportunity, Task & email to go to the new owner.
I thought that I would be able to simply create a before update
trigger, test if it's converting and then update the lead.OwnerId
. I created a very simple proof of concept to test this and got some odd results:
trigger LeadTrigger on Lead (before update, after update) {
System.debug((Trigger.isBefore ? 'Before' : 'After') + ' Update');
System.debug(lead.OwnerId);
if(trigger.isBefore){
for(Lead lead : (List<Lead>) trigger.new) {
if (Lead.IsConverted){
lead.OwnerId = '0054C000000jHDD';
}
System.debug('New Owner: ' + lead.OwnerId);
}
}
}
After converting the Account, Opportunity, Task owners are NOT updated to 0054C000000jHDD
. The debug output is very strange as System.debug(lead.OwnerId);
outputs the string OwnerId
instead of a user id.
14:40:58.601 (2611942429)|USER_DEBUG|[15]|DEBUG|Before Update
14:40:58.601 (2612031531)|USER_DEBUG|[16]|DEBUG|OwnerId
14:40:58.601 (2612297809)|USER_DEBUG|[23]|DEBUG|New Owner: 0054C000000jHDDQA2
14:40:58.663 (2665290643)|USER_DEBUG|[15]|DEBUG|After Update
14:40:58.663 (2665365454)|USER_DEBUG|[16]|DEBUG|OwnerId