Wednesday, December 30, 2015

Trigger to update Rating field value based on Account Industry field

I'm trying to set Rating field value in Account object to 'Warm' whenever the Industry filed value on Account is Finance or Chemicals or HealthCare.

Possible Solutions:
  1.Using Workflows
  2.Using Triggers

In this post I'm trying to do this with the help of trigger only.Below is code snippet to implement this functionality with the help of trigger.

Source Code:
 trigger updateRatingTrigger on Account (before insert,before update)  
 {  
  if(Trigger.isBefore && (Trigger.isinsert || Trigger.isUpdate))  
  {  
    Set<String> setIndusry = new Set<String>{'Finance','Chemicals','HealthCare'};  
    for(Account acc: Trigger.new)  
    {  
    if(setIndusry.contains(acc.Industry) && acc.Rating!='Warm')  
     acc.Rating= 'Warm';  
    }  
  }  
 }  

Enjoy....!

No comments:

Post a Comment