2

I saw some posts related to APEX triggers and recently one below,

Salesforce method to update doesn't get commited

in which the trigger have few lines,

ApexPAges.StandardController sc = new ApexPages.StandardController(projectMap.get(pro.id));

    ProjectTaskExtension projExtClass = new ProjectTaskExtension(sc);
    projExtClass.updateTasks(projectMap.get(pro.id));

Is that a best practice to call a controller inside of a trigger and use it to achieve something? Calling a controller would affect the performance of the trigger? Would it be a best practice doing so? Any suggestions.

Thanks,

4

In that example the user is only calling the controller in order to reuse some existing code in this method:

projExtClass.updateTasks()

In terms of trigger best practice, it's widely considered best practice to move code away from your triggers into trigger helper classes.

Personally, I use a streamlined version of this framework: http://krishhari.wordpress.com/2013/07/22/an-architecture-framework-to-handle-triggers-in-the-force-com-platform/

  • 1
    In general, people might consider pulling out the code from the controller first into it's own class to simplify re-use. Visualforce controllers often have stuff that won't work in triggers, e.g. page messages, and tend to be geared towards single records. Pulling out common business logic into it's own class and reference that from both the trigger and controller would be ideal. Of course any code re-use is better than none! – Ralph Callaway Jun 30 '14 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.