0

I want to pass field values from trigger to an APEX class.

I have created the trigger but I don't know how to call the class from trigger and how to receive these values in the class.

trigger trgTestCustomer on TestCustomer__c (after insert) {

for(TestCustomer__c t: Trigger.new){

         string newName;
         newName = t.Name;
     } 

}

Any snippet will be helpful. Can a List object of all the values be passed to class?

2

The context variables Trigger.new and Trigger.old are typed as List<sobject>, where sobject is the object your trigger fires on. Trigger.newMap and Trigger.oldMap are typed as Map<Id, sobject>.

All of these values can be passed to a class like any other parameters, and it's very common to do so as part of a quality trigger framework. For example:

TestCustomerTriggerHandler.afterInsert(Trigger.new);

I recommend reviewing Trigger Context Variables and Trigger Context Variables Considerations from the Apex Developer Guide.

2
  • But how to call the class with Trigger.new from trigger?
    – RKh
    Feb 27 '18 at 11:59
  • There is an example of how to call a class with Trigger.new in the body of my answer.
    – David Reed
    Feb 27 '18 at 12:04
0
trigger trgTestCustomer on TestCustomer__c (after insert) {
   TriggerHandler.method1(Trigger.new)
    for(TestCustomer__c t: Trigger.new){
     string newName;
     newName = t.Name;
 } 
}
public class TriggerHandler{
    public static void method1(List<TestCustomer__c> lstCustomers){
       //your code comes here
    }
}`

Hope this should help on how to pass context variables from trigger to apex class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.