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
StaciStaci 

move parts of trigger into an apex class

I'm realizing I need to move some of the stuff in this trigger into an apex class.  This is a snippet of the trigger.  I have a picklist called Macros.  When a user choses a macro and clicks save on a case, the corresponding comment will be added to the case comment.  How can I accomplish this? 
trigger CW_DSS_CaseTrigger on Case (after update) {
  if(CWcheckRecursive.runOnce())
    {
  
   List<caseComment> caseCommentList = new List<caseComment>();
    for (Case c: Trigger.new)
    {
        
        caseComment cc = new caseComment();
        if(c.Macros__c == 'Application Support information'){
                cc.parentid = c.ID;
                cc.commentbody = 'For application (DSSi and Relay Server) support and troubleshooting requests, please provide the following information.'+'\n' +
                                    'Minimum DSS Ticket Information requirements:'+'\n' +
                                    'Site Name:' +'\n' +
                                    'Site Contact: (For possible Site IT related issues)' +'\n' +
                                    'DSSi Software Version:' +'\n' +
                                    'Problem/Inquiry:' +'\n' +
                                    'Troubleshooting Steps Taken:' +'\n' +
                                    'This information is required with all DSS support requests. Failure to provide this requested information may delay your request.';
                cc.ispublished = true;
                casecommentlist.add(cc);
                System.debug('********************************************************************'+ cc.commentbody);
               
        }

//insert caseCommentList;
            If(caseCommentList.Size() > 0){
            insert caseCommentList;
}}}

 
Best Answer chosen by Staci
ANUTEJANUTEJ (Salesforce Developers) 
Hi Staci,

You can check it the below way and modify it accordingly:
 
trigger CW_DSS_CaseTrigger on Case (after update) {
if(trigger.isafter && trigger.isupdate)
{
   CW_DSS_CaseTriggerHelper.CaseMethod1(Trigger.New,Trigger.oldMap);
}
  }




public class CW_DSS_CaseTriggerHelper{
           
           public static void CaseMethod1(List<Case> caseList, Map<Id,Case> oldmapValue){
           
if(CWcheckRecursive.runOnce())
    {
  
   List<caseComment> caseCommentList = new List<caseComment>();
    for (Case c: caseList)
    {
        
        caseComment cc = new caseComment();
        if(c.Macros__c == 'Application Support information'){
                cc.parentid = c.ID;
                cc.commentbody = 'For application (DSSi and Relay Server) support and troubleshooting requests, please provide the following information.'+'\n' +
                                    'Minimum DSS Ticket Information requirements:'+'\n' +
                                    'Site Name:' +'\n' +
                                    'Site Contact: (For possible Site IT related issues)' +'\n' +
                                    'DSSi Software Version:' +'\n' +
                                    'Problem/Inquiry:' +'\n' +
                                    'Troubleshooting Steps Taken:' +'\n' +
                                    'This information is required with all DSS support requests. Failure to provide this requested information may delay your request.';
                cc.ispublished = true;
                casecommentlist.add(cc);
                System.debug('********************************************************************'+ cc.commentbody);
               
        }

//insert caseCommentList;
            If(caseCommentList.Size() > 0){
            insert caseCommentList;
}}
           
           }
           
           
           }

Additionally, I would suggest you to check the best practices in the below links: 

>> https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_bestpract.htm

>> https://developer.salesforce.com/forums/?id=906F0000000DBl8IAG

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.