+ Start a Discussion
raji devi 1raji devi 1 

Trigger for update field depends on picklist values in solution object

Hi All,
Thanks in advance.
I have written a trigger on solution object Sample code below. Mysenariou islike this : we are saving the records in solution object defaulty the status field (picklist) value is Draft. When it changed to'Duplicate'then   solution Detail (Rich Text Area) also update to 'Duplicate' ?

    trigger solutiontest on Solution(after update){    
    
     set<id> solutionids= new set<id>();
      for(solution s : trigger.new){
          solutionids.add(s.id);
      
      }  
            
        list<solution> slist =[select id,SolutionNote, status from solution where id in : solutionids];
            for(Solution s : slist){
            if(s.status == 'Duplicate'){
                s.SolutionNote= s.status;
               slist.add(s); 
            }    
        
        }
        
        update slist;
          
    
    }

Thanks and Regards,
Raji
Best Answer chosen by raji devi 1
Naresh YadavNaresh Yadav
Hi raji devi

Here is a basic concet that if you are update the field of same object on which you writing trigger then use Before Update instead of After Update.

Use the below code.
 
trigger solutiontest on Solution(before update){    
    
    for(Solution s : trigger.new){
        if(s.status == 'Duplicate'){
            s.SolutionNote= s.status;
        }    
    }

}

Let me know if it helps you out.
Peace.

All Answers

Naresh YadavNaresh Yadav
Hi raji devi

Here is a basic concet that if you are update the field of same object on which you writing trigger then use Before Update instead of After Update.

Use the below code.
 
trigger solutiontest on Solution(before update){    
    
    for(Solution s : trigger.new){
        if(s.status == 'Duplicate'){
            s.SolutionNote= s.status;
        }    
    }

}

Let me know if it helps you out.
Peace.
This was selected as the best answer
Rohit K SethiRohit K Sethi
hi ,

I thing you should need to write the trigger on before update so that you do not need to again update so "slist" that solution list.
 
trigger solutiontest on Solution(before update){    
    
      for(solution s : trigger.new){
            if(s.status != trigger.oldMap.get(s.id).status){
              s.SolutionNote = s.status;
            }      
      }  
}

Try this code if you any issue plz let me know.

Thanks.
raji devi 1raji devi 1
Hi ,
Thanks alot , Naresh and Rohit 
Regards,
Raji