+ Start a Discussion
Anthony HolmesAnthony Holmes 

Trigger Based on Custom Score

Hi there,

I want to make a trigger that fires when a custom field called "Behaviour Score" hits 100 and above. The trigger will change the current lead owner to a new lead owner that is a queue to be distributed between agents. Can anyone help me? 
Best Answer chosen by Anthony Holmes
VamsiVamsi
Ohh ok in that I need to add a check for Booking start  as well. please make use of below code....
 
Trigger BehaviourScore on Lead (before insert,before update)
{
   for(Lead L : Trigger.new)
  {
     if(Trigger.isInsert && L.Behaviour_score__c >= 100 && L.No.of Employees__c <=249 && L.Booking_start__c == null)
     {
        L.ownerID = 'specify a queue id'; // If you don't want to hard code queue id then retrieve the queue id from group object with type = queue and then assign that variable to L.ownerID IMP: retrieve the queue details out side of the for loop
     }else IF (Trigger.isUpdate && Trigger.oldmap.get(L.id).Behaviour_score__c!=Trigger.newmap.get(L.id).Behaviour_score__c && L.Behaviour_score__c >= 100 && Trigger.oldmap.get(L.id).No.of Employees__c!=Trigger.newmap.get(L.id).No.of Employees__c &&  L.No.of Employees__c <=249 && Trigger.oldmap.get(L.id).Booking_start__c !=Trigger.newmap.get(L.id).Booking_start__c  && L.Booking_start__c == null)
   {
    L.ownerID = 'specify a queue id'; 
    }
  }
}

This trigger will be executed only for below  

1. while inserting new Lead records with all 3 conditions to True 
L.Behaviour_score__c >= 100 && L.No.of Employees__c <=249 && L.Booking_start__c == null

2. While updating exisiting Lead record with all 3 conditions to True 

Behaviour_score__c value is updated and not equals to previous value and Behaviour_score__c >=100

AND 

No.of Employees__c value is updated and not equals to previous value and No.of Employees__c <=249 

AND 

Booking_start__c  value is updated and not equals to previous value and Booking_start__c  == null

Please validate the trigger thoroughly before moving it to live ...!!!

Have a great day ..!!!

 

All Answers

VamsiVamsi
Trigger BehaviourScore on Lead (before insert,before update)
{
   for(Lead L : Trigger.new)
  {
     if(Trigger.isInsert && L.Behaviour_score__c >= 100)
     {
        L.ownerID = 'specify a queue id'; // If you don't want to hard code queue id then retrieve the queue id from group object with type = queue and then assign that variable to L.ownerID IMP: retrieve the queue details out side of the for loop
     }else IF (Trigger.isUpdate && Trigger.oldmap.get(l.id).Behaviour_score__c!=Trigger.newmap.get(L.id).Behaviour_score__c && L.Behaviour_score__c >= 100)
   {
       L.ownerID = 'specify a queue id'; 
   }
  }
}
Hope the above code helps .......!!!!!!!!!!

Also please make to update Behaviour_score__c with correct API name 

Please mark as best answer if the above helps .......!!!!

 
Anthony HolmesAnthony Holmes
That works brilliantly thank you very much. I'm new to coding apex and I was tearing my hair trying to do this. One more thing I'm struggling with is adding more fields in order for it to trigger. I need it to fire based on 3 different criteria, Behaviour Score, No. of Employees and a custom field called 'Booking Start'. I've tried adding more IF statements on the same line as the behaviour score but it won't work. 
VamsiVamsi
Hi Anthony,

How would you like to have the criteria between 3 fields.

Example 
1. When 3 fields are true 
2. When anyone of the 3 fields are true 
3. 1 field is true and anyone among other 2 is true.

Please let me know, when you would like to trigger. So that can I can form the critera statement 
 
Anthony HolmesAnthony Holmes
Hi Vamsi,

Thank you very much for your help on this.

I want it to trigger when all 3 fields are true so eg Behaviour Score >=100, No. of Employees <=249 and Booking Start =null.

Thanks again
VamsiVamsi
So you want these 3 to be true for both newely Inserting records and the one which are updating ?
Anthony HolmesAnthony Holmes
Yes that's correct
VamsiVamsi
Trigger BehaviourScore on Lead (before insert,before update)
{
   for(Lead L : Trigger.new)
  {
     if(Trigger.isInsert && L.Behaviour_score__c >= 100 && L.No.of Employees__c <=249 && L.Booking_start__c == null)
     {
        L.ownerID = 'specify a queue id'; // If you don't want to hard code queue id then retrieve the queue id from group object with type = queue and then assign that variable to L.ownerID IMP: retrieve the queue details out side of the for loop
     }else IF (Trigger.isUpdate && Trigger.oldmap.get(L.id).Behaviour_score__c!=Trigger.newmap.get(L.id).Behaviour_score__c && L.Behaviour_score__c >= 100 && Trigger.oldmap.get(L.id).No.of Employees__c!=Trigger.newmap.get(L.id).No.of Employees__c &&  L.No.of Employees__c <=249 && L.Booking_start__c == null)
   {
    L.ownerID = 'specify a queue id'; 
    }
  }
}

Please make sure that API names of Booking_start__c and No.of Employees__c are correct on Lead. Also on Lead updates does Booking start contains data other than null ?
Anthony HolmesAnthony Holmes
Hi Vamsi, that's brilliant thank you so much. Booking start will contain data but the leads I want this trigger to filter into the queue won't contain any data. 
VamsiVamsi
Ohh ok in that I need to add a check for Booking start  as well. please make use of below code....
 
Trigger BehaviourScore on Lead (before insert,before update)
{
   for(Lead L : Trigger.new)
  {
     if(Trigger.isInsert && L.Behaviour_score__c >= 100 && L.No.of Employees__c <=249 && L.Booking_start__c == null)
     {
        L.ownerID = 'specify a queue id'; // If you don't want to hard code queue id then retrieve the queue id from group object with type = queue and then assign that variable to L.ownerID IMP: retrieve the queue details out side of the for loop
     }else IF (Trigger.isUpdate && Trigger.oldmap.get(L.id).Behaviour_score__c!=Trigger.newmap.get(L.id).Behaviour_score__c && L.Behaviour_score__c >= 100 && Trigger.oldmap.get(L.id).No.of Employees__c!=Trigger.newmap.get(L.id).No.of Employees__c &&  L.No.of Employees__c <=249 && Trigger.oldmap.get(L.id).Booking_start__c !=Trigger.newmap.get(L.id).Booking_start__c  && L.Booking_start__c == null)
   {
    L.ownerID = 'specify a queue id'; 
    }
  }
}

This trigger will be executed only for below  

1. while inserting new Lead records with all 3 conditions to True 
L.Behaviour_score__c >= 100 && L.No.of Employees__c <=249 && L.Booking_start__c == null

2. While updating exisiting Lead record with all 3 conditions to True 

Behaviour_score__c value is updated and not equals to previous value and Behaviour_score__c >=100

AND 

No.of Employees__c value is updated and not equals to previous value and No.of Employees__c <=249 

AND 

Booking_start__c  value is updated and not equals to previous value and Booking_start__c  == null

Please validate the trigger thoroughly before moving it to live ...!!!

Have a great day ..!!!

 
This was selected as the best answer
Anthony HolmesAnthony Holmes
Amazing, thanks so much for your help
VamsiVamsi
Always welcome ..!!! Virus-free. www.avast.com