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
choppen5choppen5 

Apex class vs Trigger

I have a requirement to create a new custom child object for Opportunities.  Looks like I can create an APEX class to to do that and call it with a trigger on Opportunity... or just include in the trigger the code to create the new object.  

 

Is there a compelling reason to do it via apex class, or trigger would be the best?

Best Answer chosen by Admin (Salesforce Developers) 
HarmpieHarmpie
In general, APEX classes should contain re-usable code. So if this piece of logic is used in other situations as well, put it in a class. If not, you can add it to the trigger. Sometimes you may prefer adding non-re-used code to classes as well, purely to keep a trigger clean, in case of a lot of code (triggers are also limited to 500 lines).