6

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.

2 Answers 2

7

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
    }
}
2

You can pass in the maps when you call the methods. In other words, make the trigger maps arguments in the method call:

public void myMethod(List<sObject> newObjs, Map<id,sObject> oldMap){}
5
  • I added the oldMap argument into my method but am still getting the error Field expression not allowed for generic SObject, question updated
    – EricSSH
    Commented Oct 31, 2014 at 20:48
  • you'll have to reference the map now, you can't use Trigger.New, Trigger.newMap etc. outside of the actual trigger. You should do things like checking for an update in your trigger prior to making your method call in the utility class, or else you would also need to pass in that boolean to your method.
    – greenstork
    Commented Oct 31, 2014 at 20:50
  • Stated otherwise, Trigger.old and Trigger.new are of the generic type Sobject[] instead of a specific type, and oldMap and newMap are of type Map<Id, Sobject> instead of a specific type. You can use them generically, or you can cast them to a concrete Sobject, or use method parameters.
    – sfdcfox
    Commented Oct 31, 2014 at 21:11
  • 1
    @greenstork you certainly can access them. They just lose their concrete status and become generic.
    – sfdcfox
    Commented Oct 31, 2014 at 21:15
  • ah, I seem to remember that now that you say it, thanks for chiming in
    – greenstork
    Commented Oct 31, 2014 at 21:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .