2

Working with Visual Force pages recently I've grown to like the benefits of defining inner class objects to store complex data structure as opposed to multidimensional maps. For example I can easily store aggregated data into an inner object like this simple example:

public CustomClass{
    public class InnerObject{
        public String var1
        public Decimal var2

        public InnerObject(Custom_Object__c record){
            this.var1 = record.Custom1__c;
            this.var2 = record.Custom2__c - record.Custom3__c;
        }
    }

    public void testMethod(List<InnerObject> items){
        for(InnerObject item : items){
            System.debug('::TEST:: '+item.var1);
        }
    }
}

I've tested and found that I can use this in my trigger loop like so:

List<CustomClass.InnerObject> line_items = List<CustomClass.InnerObject>();
if(Trigger.isAfter && Trigger.isInsert){
    for(Custom_Object__c c : (List<Custom_Object__c>) Trigger.new){
        CustomClass.InnerObject line_item = new CustomClass.InnerObject(c);
        System.debug('::LINE ITEM:: '+line_item);

        line_items.add(line_item);
    }
}
CustomClass.testMethod(line_items); // This doesn't work

Sweet! This seems promising thus far but then when I store this in a collection to send to a utility method (as shown above) I keep getting the following error:

Error: Method does not exist or incorrect signature: CustomClass.testMethod(List<CustomClass.InnerObject>)

Is this not possible? Not sure why I can get part of it to work but not the next part, wondering if I'm missing something. Any insight is appreciated, thanks!

1 Answer 1

4

You need to either instantiate the class or make the method static:

CustomClass c = New CustomClass();
c.testMethod(line_items);

Or

public static void testMethod(List<InnerObject> items){
        for(InnerObject item : items){
            System.debug('::TEST:: '+item.var1);
        }
    }

then

CustomClass.testMethod(line_items);
3
  • Normally I would datatype Custom_Object__c.Custom_Field__c as SObjectField when passing to a utility method, but since this is not an actual object with fields, rather attributes how would I datatype something like InnerObject.var1 ? Aug 13, 2015 at 21:32
  • I guess it depends on the scope - Not that good with thinking so abstractly
    – Eric
    Aug 13, 2015 at 21:52
  • @Xtremefaith You can pass an Object, but as Eric mentions it really depends on scope. If your utility function can do the same thing on multiple types of objects, you can add an interface to both objects that defines functions for your utility class to use, then define that interface as the parameter to the function. Aug 14, 2015 at 0:28

You must log in to answer this question.

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