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
JJJenkinsJJJenkins 

Previous Owner Field update

I'm still learning apex so I wanted to see if this was even possible.

 

I have a user look up field on the lead that I want filled in automatically with the previous owner when the lead owner is changed.

 

Thoughts?

 

Thanks everyone!

Best Answer chosen by Admin (Salesforce Developers) 
JJJenkinsJJJenkins

here's the code if anyone was interested:

 

trigger PrevLeadOwner on Lead (before update) {
   
    map<id,id> LeadIdOldRepId = new map<id,id>();
    map<id,string> userorque = new map<id,string>();
    list<user> AllUsers = [SELECT Id
                           FROM User];
    for(user u:AllUsers)
        {
            userorque.put(u.id,'here');
        }                           
                           
    for (Lead l : trigger.old)
        {
            string inmap = userorque.get(l.ownerid);
            if(inmap != null)
            {
                LeadIdOldRepId.put(l.id,l.ownerid);
            }
        }
    for (Lead l : trigger.new)
        {
            id shouldiupdate = LeadIdOldRepId.get(l.id);
            if(shouldiupdate != null)
                {
                    l.previous_lead_owner__c = LeadIdOldRepId.get(l.id);
                }    
        }    
           

 

All Answers

Shashikant SharmaShashikant Sharma

Yes you can do it with A trigger on lead. Before update trigger on lead.

 

Here is your trigger

trigger updateOldOwner on Lead (before update) 
{
    for(Lead l : trigger.new)
    {
        if(l.OwnerId != trigger.OldMap.get(l.id).OwnerId)
        {
            l.MyLookupField__C = trigger.OldMap.get(l.id).OwnerId;
        }
    }
}

 

 

 MyLookupField__C change this with your lookup field api name

JJJenkinsJJJenkins

I'm receiving this error

 

Error: Apex trigger PrevLeadOwner caused an unexpected exception, contact your administrator: PrevLeadOwner: data changed by trigger for field Previous Lead Owner: id value of incorrect type: 00G800000021t6SEAQ

 

When I changed lead from my name to the queue it worked correctly but when I try to remove it out of a queue I received the error.  Any suggestions?

 

Thanks,

Ankit AroraAnkit Arora

Can you please explain your error more, as Shashikant's trigger is absolutely fine.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

JJJenkinsJJJenkins

I received the error when the previous owner was a Queue because it was trying to inser the queue id into a user id field.  I have this working now; thanks for the jumping off poing.

JJJenkinsJJJenkins

here's the code if anyone was interested:

 

trigger PrevLeadOwner on Lead (before update) {
   
    map<id,id> LeadIdOldRepId = new map<id,id>();
    map<id,string> userorque = new map<id,string>();
    list<user> AllUsers = [SELECT Id
                           FROM User];
    for(user u:AllUsers)
        {
            userorque.put(u.id,'here');
        }                           
                           
    for (Lead l : trigger.old)
        {
            string inmap = userorque.get(l.ownerid);
            if(inmap != null)
            {
                LeadIdOldRepId.put(l.id,l.ownerid);
            }
        }
    for (Lead l : trigger.new)
        {
            id shouldiupdate = LeadIdOldRepId.get(l.id);
            if(shouldiupdate != null)
                {
                    l.previous_lead_owner__c = LeadIdOldRepId.get(l.id);
                }    
        }    
           

 

This was selected as the best answer
Shashikant SharmaShashikant Sharma

Gr8 You Sorted it Out,

 

And Thanks for sharing your solution.

Ankit AroraAnkit Arora

Thanks for sharing this.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

alok29novalok29nov

Hi JJJenkins,

 

I have the same kind of requirement for opportunity. I tried this code but only issue is that I get only previous owner id not the name in  my previous owner field(PreviousOwner__c). Do you also get the id only in field which is being populated.

 

If you are getting name, then why in my case it is happening that I am getting id. Any pointers.

 

Hope for a quick reply!