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
Akash Choudhary 17Akash 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';
        }
    }

}
Preeta B YPreeta B Y
To invoke a trigger from apex - write an Apex class and perform a DML operation in it. In this case-  create a new case, this will automatically fire the trigger.
Narender Singh(Nads)Narender Singh(Nads)
Hi Akash,
You cannot explictly call a trigger from a class. Triggers are automatically fired(implicitly) when there is a DML operation on that object.
Prashant Pandey07Prashant Pandey07
Hi Akash,

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