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 ?
[SalesForce] purpose of utility class
Related Solutions
The error message "Field expression not allowed for generic SObject" means that you're trying to use "dot" syntax where it isn't allowed or can't be performed.
In this case, it's because the code is referencing a generic sObject (which might not have the expected field on it) and the compiler can't verify the existence of .Product__c
on it. You can either cast the generic sObject to its proper type or reference a collection of a concrete type, Sampling__c
in this case and the compiler will be satisfied.
Now that you've got a method parameter named oldMap which is being passed in with the concrete type, you can reference it in your code. In addition, you've also still got access to the Trigger
static references when this code is being called from within the trigger's context.
public void updateSamplingAE2(List<Sampling__c> sampling, Map<Id, Sampling__c> oldMap) {
// not sure what the s reference is, left it as is
// changed the reference from Trigger.oldMap to the method param oldMap
if (Trigger.isUpdate && oldMap.get(s.id).Product__c != s.Product__c) {
//do stuff
}
}
I think this question is way too broad for one answer to cover. I suggest you look at some libraries out there to see how they use inner classes.
There are so many use cases. It would be really difficult to cover them all. But I will just cover three here:
- Generalization (DRY)
- Selection Wrappers
- Deserialization
Generalization (DRY)
I know for my DML
library, I use a few, all of which have their behavior defined by interfaces. But that is neither here nor there.
Maybe a concrete example will help you better understand. Take a look at the GenericResult
inner class in my DMLResults
class.
public class GenericResult
{
final Id recordId;
final List<Database.Error> errors;
public GenericResult(Id recordId, List<Database.Error> errors)
{
this.recordId = recordId;
this.errors = (errors != null) ? errors : new List<Database.Error>();
}
public Id getId() { return recordId; }
public List<Database.Error> getErrors() { return errors; }
}
This class helps me out with DRY programming by letting me use one class to represent all of the *Result
system classes (see also: Do the *Result objects in Database have a common ancestor?). Because of this implementation, my PartialResult
logic can use GenericResult
instances once instead of repeating the logic once each for SaveResult
, UpsertResult
, DeleteResult
, etc.
Selection Wrappers
Often wrapper classes are often used for record selection on a Visualforce Page
. This can be as simple as:
public class MyObjectWrapper
{
public Boolean isSelected { get; set; }
public MyObject__c record { get; private set; }
public MyObjectWrapper(MyObject__c record)
{
this.record = record;
this.isSelected = false;
}
}
Then in your markup, you would see something like:
<apex:repeat value="{!wrappers}" var="wrapper">
<apex:inputCheckbox value="{!wrapper.isSelected}">
<!--other output-->
</apex:repeat>
Deserialization
Deserialization is much simpler using an inner class, though you can use top-level classes as well.
public class TopLevel
{
public class SomeStructure
{
public String someProperty;
}
}
Then you can deserialize using this class like:
SomeStructure data = JSON.deserialize(
'{"someProperty": "<value>"}',
TopLevel.SomeStructure.class
);
Best Answer
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.