4

In my organisation , they ve written a class and in description it says " This is Utility Class " . Actually what is the purpose of utility class ? Why are we using that ?

  • 1
    if u go thru the class methods, variables, properties a few times, and search for where all its been used, you will definitely get a good idea on it purpose. even after that if you can't figure it out, you can share the class code to see if someone can help out – Vamsi Krishna May 13 '15 at 5:40
  • 2
    This is part of your apex architecture, and the specific reasons for this class is something you'd better ask your architects or senior developers. In general: ususally relates to separation of concerns and maintainability of your code. – Samuel De Rycke May 13 '15 at 12:53
9

The purpose of a utility class is to serve as a single one-stop-shop for reusable pieces of code. Basically any variable, method, inner class and whatever it might be that is used multiple times across multiple other classes within the project, can reside into a single (or multiple) utility class(es), which helps writing more elegant and structured code, as well as makes the code easier to maintain.

|improve this answer|||||
5

Utility classes are your friend. In addition to @Bachovski use cases, my Util methods provide centralized solutions for caching reference data to save on SOQL calls - especially when you have a complex environment wherein triggers fire on multiple objects per transaction. So, for example, sometimes you have code that needs to execute logic that varies based on running user's Profile name.

The Util class method getProfile(someName) will return the Profile object for a given name but cache all the Profiles on the first invocation of Util.getprofile(..) as the Profiles don't change in a given transaction but multiple triggers/handler classes may need the profile name for logic switching.

You'll find that as you move from org to org, that you'll copy over your Util methods and build on them -- within months/few years - you'll have a robust set of commonly-executed functionality that will save on time and make your calling logic cleaner and more consistent.

|improve this answer|||||
0

Utility class contains functions or methods which might be used frequently in a lot of class in the SFDC instance. eg. we want to get the label or API Name of any custom objects or to get the name of the record type from the id.

We write a generic method in the utility class which does the job for you and you can reuse the code of that class in any other class.

|improve this answer|||||

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.