You need to sign in to do that
Don't have an account?
Zach 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
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
I'm a visual learner so please provide me the very basic and very simple explanation :)
Thanks a lot as always :)
-Zach
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
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
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
Please pass list<TestObj__c > directly to ClsTriggerHandler.createTestRecords .
trigger TriggerTest on TestObj__c (before insert, before update) {
ClsTriggerHandler.createTestRecords(trigger.new);
}
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
Trigger
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!