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
Daniel Peaper 8Daniel Peaper 8 

Is it not necessary to pass parameters between a trigger and it's helper class?

I came across some code recently and I was surprised that no parameters were included in the call to the helper class method. Instead the helper class method just used all of the trigger methods directly. So you end up with code like this:-

if(Trigger.isBefore && Trigger.isInsert) {

AccountTriggerHelper.beforeInsert();

}

Then in the helper class:-

public with sharing class AccountTriggerHelper {

public static void beforeInsert() {

System.Debug('Trigger new is: ' + Trigger.new);

}
}

And this works perfectly but I can't find any reference to coding like this in the documentation or on Trailhead.

 

Arun Kumar 1141Arun Kumar 1141

Hi Daniel,

The code you mentioned is using the trigger context variables directly within the helper class method without passing any parameters. This approach is possible because the trigger context variables (such as `Trigger.new`, `Trigger.old`, etc.) are static variables and can be accessed from any class in the same context.

So, It is not necessary to pass parameters in helper class.

Hope this will help.
Thanks!