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
sudha76sudha76 

Trigger to change the Lead Owner when the Score hits more than 50

hi there,

 

Today, I managed to write yet another trigger and like always I think I have some errors. Please help:-

 

Trigger ChangeLeadOwner on Lead (after Insert)
{

    //First I will get list of LEAD Records where Pardot Score is less than 50 using a Query
    
    
    List<Lead> ParLead = [SELECT Id, Name FROM LEAD WHERE pi__score__c < 50];

    set<Id> LeadIds = new set<Id>();

    
    // Also get the list of Queues related to LEAD object and the specific queue where these leads should be assigned to.
    
    List<QueueSobject) PardotQueue = [SELECT Id, SobjectType, QueueId, Queue.Name, Queue.Email FROM QueueSobject WHERE Queue.Id=: 00GS00000016UFx AND SObjectType='Lead'];

    
    // Now, I should define a FOR loop which will search through the LEAD Records and look for the Pardot_Score field to be 50 or more than 50 then
    // reassign the LEAD to the Pardot Queue.

    for(Lead Leady1: Trigger.new)
    {
        if(pi__score__c >= '50')
        {
            newLead[0].Ownerid = PardotQueue[0].id;
        }
    }
}

 

My Goal is :-

 

On a LEAD when the field pi__score__c reached 50 or greater than 50 then it should be reassigned to a a specfic queue called - Pardot_Queue where the ID is = 00GS00000016UFx

 

 

Please help.

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

A workflow should do it

Workflow to fire each time Record is created ir edited
Criteria : Ischanged(pardot value) and pardot value > =50 and prior value < 50

But here's a trigger just in case

Trigger LeadBefore on Lead (before update ){

//retrieve pardot queue id as you have - except for its not best practice to hardcode the id - retrieve using Name
For (Lead ld: trigger.new)
if(ld.pi__score__c >= '50')
Ld.Ownerid = PardotQueue[0].id;
}

All Answers

sudha76sudha76

I want to share this with everyone - the pi__score__c is coming from the PARDOT integrated application with SFDC. When it reaches to 50 we need to assign to a specific QUEUE so some TEAM Memebers can take action.  Any thoughts? How this trigger can do that?

WF Rule may not be a good option.

 

thoughts?

SFFSFF

Try this:

 

Trigger ChangeLeadOwner on Lead (before insert, before update)
{
  // Get the ID of the Queue.
  for (Lead l : system.trigger.new)
  {
    if (pi__score__c < 50)
    {
      // Really should not be a hard-coded ID.
      l.OwnerId = '00GS00000016UFx';
    }
}

This should be a before trigger, not an after trigger - and I am unclear on where the pi__score__c value comes from, because I am guessing it is calculated either in a before-trigger or a VF page. If this is calculated in a VF page, then there isn't a problem, but if it is calculated in a before-trigger, then there could be a timing issue.

 

Alternately, you may be able to use a Lead Assignment rule, depending on when the pi__score__c is calculated.

 

Or even an @future call.

 

Hope this helps,

Ritesh AswaneyRitesh Aswaney
Hey Sudha
Is there a specific reason for ruling out a workflow field update, given that they have to be assigned to just one queue?
sudha76sudha76

 

 The pi__score__c is a custom field which is coming as a results of integration between SFDC and Marketing Automated Tool called = Pardot.

 

 

 

 

Ritesh AswaneyRitesh Aswaney

A workflow should do it

Workflow to fire each time Record is created ir edited
Criteria : Ischanged(pardot value) and pardot value > =50 and prior value < 50

But here's a trigger just in case

Trigger LeadBefore on Lead (before update ){

//retrieve pardot queue id as you have - except for its not best practice to hardcode the id - retrieve using Name
For (Lead ld: trigger.new)
if(ld.pi__score__c >= '50')
Ld.Ownerid = PardotQueue[0].id;
}

This was selected as the best answer
vishal@forcevishal@force

Hey,

 

Also I would like to add one more point here, your query for getting the QUEUE has a hard-coded ID, this may fail when you deploy this trigger (it won't have the same ID for the queue it has here in the other Orgs).

 

Modify this QUERY : List<QueueSobject) PardotQueue = [SELECT Id, SobjectType, QueueId, Queue.Name, Queue.Email FROM QueueSobject WHERE Queue.Id=: 00GS00000016UFx AND SObjectType='Lead'];

 

to

 

List<QueueSobject> PardotQueue = [SELECT Id FROM QueueSobject WHERE Queue.Name = 'Pardot_Queue' AND SObjectType='Lead' LIMIT 1];