+ Start a Discussion
EdCodeEdCode 

What is an "Utility" method?

Looking at SFDC's Trailhead "Get started with apex triggers", I read the following:

"You can call public utility methods from a trigger."

What is an utility method?

Thank you.
Best Answer chosen by EdCode
Tintu_BabuTintu_Babu
Hi,

Utility classes are helper classes that consisits of reusable methods. 
From triggers we can call  methods in such public classes. This helps to reduce code with in trigger and make it more structured.
If we have a trigger on oppotunity which does date format and assign it to a field value, it can be written as follows with the help of utilty 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();
}
}

All Answers

Neetu_BansalNeetu_Bansal
Hello,

Utility method is like you have any other apex class, where you have written some code and you can use those methods in Trigger like below:
User-added image
Here, EmailManager is another apex class and sendEmail() is one of its method.

If it helps, mark it as best answer.

Thanks,
Neetu
Tintu_BabuTintu_Babu
Hi,

Utility classes are helper classes that consisits of reusable methods. 
From triggers we can call  methods in such public classes. This helps to reduce code with in trigger and make it more structured.
If we have a trigger on oppotunity which does date format and assign it to a field value, it can be written as follows with the help of utilty 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 was selected as the best answer
EdCodeEdCode
Thank you very much to both of you.
Sai.MaharajpetSai.Maharajpet

I am facing an error while executing the utility method from the trailhead(Get Started with Apex Triggers) as Neetu Mentioned above 
please help me to resolve it


"17:06:22:197 USER_DEBUG [20]|DEBUG|#### parentIdsSet = {} "

rigger ExampleTrigger on Contact (after insert,after delete) {
    if(trigger.isInsert){
        Integer recordcount = Trigger.New.size();
        EmailManager.sendMail('Saisagar003@gmail.com','Test Email for Contact Data ',recordcount + 'Contacts were inserted'); 
    }
    else if(trigger.isDelete){    
    }
}
public class EmailManager {
    // Public method
    public static void sendMail(String address, String subject, String body) {
        // Create an email message object
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {address};
        mail.setToAddresses(toAddresses);
        mail.setSubject(subject);
        mail.setPlainTextBody(body);
        // Pass this email message to the built-in sendEmail method 
        // of the Messaging class
        Messaging.SendEmailResult[] results = Messaging.sendEmail(
                                 new Messaging.SingleEmailMessage[] { mail });
        
        // Call a helper method to inspect the returned results
        inspectResults(results);
    }
    
    // Helper method
    private static Boolean inspectResults(Messaging.SendEmailResult[] results) {
        Boolean sendResult = true;
        
        // sendEmail returns an array of result objects.
        // Iterate through the list to inspect results. 
        // In this class, the methods send only one email, 
        // so we should have only one result.
        for (Messaging.SendEmailResult res : results) {
            if (res.isSuccess()) {
                System.debug('Email sent successfully');
            }
            else {
                sendResult = false;
                System.debug('The following errors occurred: ' + res.getErrors());                 
            }
        }
        
        return sendResult;
    }
}