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 Answers 2

3

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.

0
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .