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
Shauna Davies 72Shauna Davies 72 

I need an Apex Trigger to update the Lead Owner

I need an Apex Trigger that will update and move a lead from our Content Syndication queue to the reassignment queue when a lead reaches >80%.  I cannot use a workflow, because that will only kick off on creation or edit, and the lead scoring tabulates by Einstein every hour.  Unless I do a mass update to edit/save and kick off the workflow, Salesforce help has said that an Apex Trigger is the only solution.

I have this code, but I get an error on line 5:  Error: Compile Error: Missing '<EOF>' at 'for' at line 5 column 5

trigger LeadRoutingTrigger on Lead (before update) {
    Group ReassignmentQueue = [select Id from Group where Type = 'Queue' AND NAME = 'Reassignment Queue' LIMIT 1];
    Group ContentQueue = [select Id from Group where Type = 'Queue' AND NAME = 'Content Syndication and Events Queue' LIMIT 1];
}
    for(Lead currentLead : Trigger.new) {
        if(currentLead.OwnerId == ContentQueue.Id 
           && currentLead.Lead_Insights_Value__c    > 80) {
               currentLead.OwnerId = ReassignmentQueue.Id;
           }
    }
}

Does anyone have a suggestion?  

Thanks
anka lexanka lex
Hi Shauna,
trigger LeadRoutingTrigger on Lead (before update) {
    Group ReassignmentQueue = [select Id from Group where Type = 'Queue' AND NAME = 'Reassignment Queue' LIMIT 1];
    Group ContentQueue = [select Id from Group where Type = 'Queue' AND NAME = 'Content Syndication and Events Queue' LIMIT 1];
    List<Lead> updLeadLst = new List<Lead>();
    for(Lead currentLead : Trigger.new) {
        if(currentLead.OwnerId == ContentQueue.Id 
           && currentLead.ankalex__Lead_Insights_Value__c   > 80) {
               currentLead.OwnerId = ReassignmentQueue.Id;
               updLeadLst.add(currentLead);
           }
    }
    
    if(updLeadLst.size() >0){
        Database.update(updLeadLst,false);
    }
}

please mark this as answred in case the solution is working for you.

Thanks
Prashant Pandey07Prashant Pandey07
Hi Shauna Davies,

Check the following code.
 
trigger LeadRoutingTrigger on Lead (before update) {
    Group ReassignmentQueue = [select Id from Group where Type = 'Queue' AND NAME = 'Reassignment Queue' LIMIT 1];
    Group ContentQueue = [select Id from Group where Type = 'Queue' AND NAME = 'Content Syndication and Events Queue' LIMIT 1];

    for(Lead currentLead : Trigger.new) {
        if(currentLead.OwnerId == ContentQueue.Id 
           && currentLead.Lead_Insights_Value__c    > 80) {
               currentLead.OwnerId = ReassignmentQueue.Id;
           }
    }
}

--
Thanks,
Prashant
Shauna Davies 72Shauna Davies 72
I am working on my test Class now.  Thank you both!  I will mark the best answer when I get it up and working!
 
Shauna Davies 72Shauna Davies 72
Here is the Test Class I have written (I am VERY new to this, and not very good at it).

public class LeadRoutingTrigger
{
      public static testmethod istest
           
LeadGroup=Queue;
LeadWhoid=00G00000006skVdEAI;
Lead.Lead_insights_Value__c>'80';

insert currentLead.WhoId='00G00000006t3ZFEAY';

 }

The error I get is:      Error: Compile Error: Unexpected token '='. at line 6 column 10    

 
Prashant Pandey07Prashant Pandey07
Hi Shauna,

You may try something like this..
 
@isTest

public class LeadRoutingTrigger
{
     static testmethod void istest(){
     
     Group ReassignmentQueue = [select Id from Group where Type = 'Queue' AND NAME = 'Reassignment Queue' LIMIT 1];
     Group ContentQueue = [select Id from Group where Type = 'Queue' AND NAME = 'Content Syndication and Events Queue' LIMIT 1];
  
Profile p=[select id,Name from Profile where Name='System Administrator' limit 1];

       User u = new User (Username = 'testName@c.com', 
                              EmailEncodingKey='ISO-8859-1', 
                              Alias='tdummy2', 
                              Email='testName@gmail.com', 
                              TimeZoneSidKey='America/Chicago', 
                              LanguageLocaleKey='en_US', 
                              LocaleSidKey='en_US', 
                              LastName='dummy', 
                              ProfileId=p.id,
                              Lead_Insights_Value__c =    85);
        insert u;
        
        Lead l = new Lead(LastName='dummylastname', OwnerId = ContentQueue.Id, Company = 'Dummy Company', LeadSource='test');
       
       insert l;
      
          if(l.Lead_Insights_Value__c>85){
      
       l.OwnerId=ReassignmentQueue.id;
       update l;
           }  

 }
 
 }

--
Thanks,
Prashant
Shauna Davies 72Shauna Davies 72
Hello Prashant,

The Lead_insight_Value__c is not in the User object, but the Leads object, so it is erroring out... 

Shauna
Prashant Pandey07Prashant Pandey07
remove from user object add in the lead.

Lead l = new Lead(LastName='dummylastname', Lead_insight_Value__c=85,OwnerId = ContentQueue.Id, Company = 'Dummy Company', LeadSource='test');

--
Thanks,
Prashant
Prashant Pandey07Prashant Pandey07
Hi Shauna,

I hope the above solution worked for you...Please code this thread by marking the best answer.

--
Thanks,
Prashant