You need to sign in to do that
Don't have an account?
Akash Choudhary 17
Write an Apex Class to call a Trigger
Hi,
This is my Trigger but I want to make class instead of trigger and then call trigger with it.
trigger caseClose on Case (before insert) {
for (Case myCase : Trigger.new){
List<Case> LatestCase =[SELECT Id
FROM Case
WHERE CreatedDate = Today
AND Contact.Id =:myCase.ContactId];
if(LatestCase.size()>=2){
myCase.Status = 'Closed';
}
}
}
This is my Trigger but I want to make class instead of trigger and then call trigger with it.
trigger caseClose on Case (before insert) {
for (Case myCase : Trigger.new){
List<Case> LatestCase =[SELECT Id
FROM Case
WHERE CreatedDate = Today
AND Contact.Id =:myCase.ContactId];
if(LatestCase.size()>=2){
myCase.Status = 'Closed';
}
}
}
You cannot explictly call a trigger from a class. Triggers are automatically fired(implicitly) when there is a DML operation on that object.
I am guessing that you need the trigger logic in the class and the trigger should call the class?
Yes, You can call the trigger from apex class as well...but you need to make sure that apex class should run in real time and perfom some DML operation ..either you can you schedule the apex or user should perfom a DML operation using the class.may be custom button click..
--
Thanks,
Prashant