I am beginning the process of moving all of my Sampling
triggers to a utility class, and I am hitting a few snags. I have the following line of code to which I am getting the following error
Field expression not allowed for generic SObject
public void updateSamplingAE2(List<Sampling__c> sampling, Map<id,Sampling__c> oldMap) {
//doing stuff
if(Trigger.isUpdate && Trigger.oldMap.get(s.id).Product__c != s.Product__c){
//do stuff
}
}
So how do I go about treating Trigger.isUpdate and Trigger.oldMap when I am no longer using a trigger per say. I have never used a Utility class, and I want to start becoming more efficient rather than just pumping out triggers.
Best Answer
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.