Activity Forums Salesforce® Discussions What is Utility Method in Salesforce Apex?

  • Deepak

    Member
    February 20, 2020 at 6:41 am

    Hi Aditya,

    Utility classes are just like helper classes that consist of reusable(when required many times )  methods.
    From triggers, we can call methods in public classes. This helps to reduce code in the trigger and make it more organized.
    If we have a trigger on the opportunity which does date format and assign it to a field value, it can be written as follows with the help of utility class

    trigger updateOpp on Opportunity (before insert){
     for(Opportunity  opp : Trigger.new){
     opp.formattedDate__c = TriggerHelper.formatDate(opp.someDate__c);
    }
    }
    
    //Utility Class
    Public Class TriggerHelper{
    Public Static string formatDate(Date inputDate){
       return inputDate.format();
    }
    }

     

    • This reply was modified 4 years, 3 months ago by  Deepak.
    • This reply was modified 4 years, 3 months ago by  Forcetalks.
  • Ratnesh

    Member
    February 20, 2020 at 10:03 am

    Utility classes are helper classes that consist of reusable methods.

    From triggers, we can call methods in such public classes.

    • This reply was modified 4 years, 3 months ago by  Forcetalks.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos