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
Adria AcreeAdria Acree 

Help with creating a trigger

Hi Developers! A Salesforce admin here humbly requesting any assistance! I have request to change the workflow that I have on the leads object, that states, when a lead is placed back in Status: "Nurture" status (not on creation) or Reject, it changes the owner to a designated queue. They would now like it to return to the person that once had ownership of it automatically.  Thinking I can create a trigger on after Update event which will check the "Lead Status" and if it is equal to "Nurture" or "Reject" then it can assign the lead to the previous owner by querying on "Lead History" object to get the Previous Owner. I have watched numerous youtube videos on creating triggers and I am afraid I am no closer to coding this, then I was before I started my deep youtube dive. Any links to resources, advice you can offer or actual code would be greatly helpful!
Best Answer chosen by Adria Acree
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Adria,

Try the below code:

Lead Trigger:
 
trigger LeadTrigger on Lead (before insert,before update,after insert,after update) {
if(trigger.isBefore)
{
    if(trigger.isInsert || trigger.isUpdate)
    {
        
        LeadTriggerClass.LeadTriggerMethod(trigger.new,trigger.oldMap);
        
    }
}
}

Apex Class:
 
public class LeadTriggerClass {
public static void LeadTriggerMethod(List<Lead> newList,Map<id,Lead> oldLeadMap)
{
    system.debug('in class');
    
  for(Lead l:newList)
  {
      system.debug(l.status);
      if((l.status=='Nurture' || l.status=='Reject')&&oldLeadMap.get(l).Status!=l.Status && oldLeadMap.get(l).Status!='Nurture' && oldLeadMap.get(l).Status!='Reject')
      {
          LeadHistory lh=[SELECT Id,LeadId,CreatedDate, Field, OldValue, NewValue FROM LeadHistory where Leadid=:l.id and Field='Owner' order by CreatedDate limit 1];
          system.debug(lh);
          String LeadId=lh.OldValue.toString();
          l.ownerId=Id.valueOf(lh.OldValue.toString());
      }
  }
}
}

This code changes the owner to previous owner when the sttaus is changed to 'Nurture' or 'Reject'

Thanks
Hope this will Help.​​​​​​​

All Answers

Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Adria,

Try the below code:

Lead Trigger:
 
trigger LeadTrigger on Lead (before insert,before update,after insert,after update) {
if(trigger.isBefore)
{
    if(trigger.isInsert || trigger.isUpdate)
    {
        
        LeadTriggerClass.LeadTriggerMethod(trigger.new,trigger.oldMap);
        
    }
}
}

Apex Class:
 
public class LeadTriggerClass {
public static void LeadTriggerMethod(List<Lead> newList,Map<id,Lead> oldLeadMap)
{
    system.debug('in class');
    
  for(Lead l:newList)
  {
      system.debug(l.status);
      if((l.status=='Nurture' || l.status=='Reject')&&oldLeadMap.get(l).Status!=l.Status && oldLeadMap.get(l).Status!='Nurture' && oldLeadMap.get(l).Status!='Reject')
      {
          LeadHistory lh=[SELECT Id,LeadId,CreatedDate, Field, OldValue, NewValue FROM LeadHistory where Leadid=:l.id and Field='Owner' order by CreatedDate limit 1];
          system.debug(lh);
          String LeadId=lh.OldValue.toString();
          l.ownerId=Id.valueOf(lh.OldValue.toString());
      }
  }
}
}

This code changes the owner to previous owner when the sttaus is changed to 'Nurture' or 'Reject'

Thanks
Hope this will Help.​​​​​​​
This was selected as the best answer
Adria AcreeAdria Acree
Hi Bhargavi,

You are soooo awesome! Thank you so much for responding. I will try this and circle back as soon as I can.

Thanks again!

Adria