+ Start a Discussion
Sushma PriyaSushma Priya 

Doubt on Invoking Trigger from a class and vice versa

Hi,

 

I am new to Apex and have following doubts, can you please answer?

 

  1. Can we invoke a Trigger from a class, if so an example please.
  2. Can we invoke a Class from a Trigger, if so an example please.

 

Thanks,

Sush.

alok29novalok29nov

Hi ,

 

We can invoke a class from a trigger not vice-versa. I am giving you an example directly from apex guide.

 

------------code snippet---------

trigger helloWorldAccountTrigger on Account (before insert) {
Account[] accs = Trigger.new;
MyHelloWorld.addHelloWorld(accs);
}

 

public class MyHelloWorld {
public static void addHelloWorld(Account[] accs){
for (Account a:accs){
if (a.Hello__c != 'World') {
a.Hello__c = 'World';
}
}
}
}

------------code snippet---------------

 

If my solution worked for you, accept as solution.

 

 

 

Regards,

Alok