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
Zach DelacroixZach Delacroix 

Sample Trigger Handler help

Hey Guys!

I'd like some help about Trigger Handler. I want to start with the basics and I'd like to have assistance with step by step explanation on how this really works. (Based on your expert experience)

I've read the link below which was provided to me in my previous question but it is difficult for me to understand it for now. (Still new to apex)
https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices

For example, I've created this one below as a start. The code simply create record when I call it in my Trigger but I don't know if it's correct.

CLASS
 
public with sharing class ClsTriggerHandlerTest {

    public static void createTestRecords(List<TestObj__c> testObjs){

        List<TestObj__c> SaveRecord = new List<TestObj__c>();
        
        for(TestObj__c testObj : testObjs){
            
            testObj.Name = 'Created from ClsTriggerHandler';
            SaveRecord.add(testObj );
        }

        insert SaveRecord;
    }
}

the above Class doesn't seem to return an Error so I assume it's correct (But I'm sure there's something else I need to add)

Here's how I try to use the class in my trigger but it is returning the error:
"Method does not exist or incorrect signature: ClsTriggerHandler.theResult(TestObj__c)"

TRIGGER
trigger TriggerTest on TestObj__c (before insert, before update) {

        for(TestObj__c TestObj: trigger.new){
              ClsTriggerHandler.theResult(TestObj);
    }
}


I'm a visual learner so please provide me the very basic and very simple explanation :)

Thanks a lot as always :)

-Zach
 
Best Answer chosen by Zach Delacroix
sandeep sankhlasandeep sankhla
Hi Zach,

ClsTriggerHandler.createTestRecords(trigger.new);

You need to simply remove for loop and use this below stanemmst ..we should pass list of TestObj__c  records to our controller method. for further process.....

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 

All Answers

sandeep sankhlasandeep sankhla
Hi Zach,

ClsTriggerHandler.createTestRecords(trigger.new);

You need to simply remove for loop and use this below stanemmst ..we should pass list of TestObj__c  records to our controller method. for further process.....

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
This was selected as the best answer
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Zach,

Please pass list<TestObj__c > directly to  ClsTriggerHandler.createTestRecords .

trigger TriggerTest on TestObj__c (before insert, before update) {
             ClsTriggerHandler.createTestRecords(trigger.new);
 }
Zach DelacroixZach Delacroix
Thank you for the Answer Sandeep and Ashish!

I'm not getting the Error anymore.. However, I'm not sure if I'm doing it right because it doesn't seem to do anything. Is the logic correct?

Here's my code.

CLASS
 
public class AccountTriggerHandler {

    public static void CreateContact(List<Account> Acc){
        List<Contact> SaveContact = new List<Contact>();
        
        for(Account Accounts: Acc){
            Contact con = new Contact();
            
            con.FirstName = 'Arvin';
            con.LastName = 'FromAccountTriggerHandler';
            SaveContact.add(con);
        }
        insert SaveContact;
    }
}

Trigger
 
trigger TrgCreateContact on Account (before insert) {

    AccountTriggerHandler.CreateContact(Trigger.new);
    
}

The sample above supposed to create Contact when an Account is Created.


One of my confusions is that, in the Above sample, if it's just in a trigger, I can just simply do the below code.
    
     Trigger TestTrigger on Account (before insert){
              
                   List<Contact> ContactToSave = new List<Contact>();
                   for (Account acc: Trigger.new){
                        Contact Cons = new Contact();
                        Cons.LastName = 'Test';
                        ContactToSave.add(Cons);                     
                   }
                   Insert ContactToSave;
​      }

How do I put these logic if I wan't to put them in Class so I can have a triggerHandler?

Thanks! 
Zach DelacroixZach Delacroix
Got this figured out.. Thanks!