0

We have wrapper class:

public class Meta {
    @AuraEnabled public Integer total;
    @AuraEnabled public Integer count;
    @AuraEnabled public Integer offset;
    @AuraEnabled public String orderBy;
    @AuraEnabled public String orderDirection;

} 

and I want to loop through all fields dynamically like:

for(String key: Meta.xxxkeySet()){
    String value = Meta[key or something];
}

I can add method like returnField(String fieldName) to the wrapper class, but I don't like that I would need to hardcode field names there and more importantly there are different variable types (in this example only Integer, String)(maybe use Object type here?). Did you have similar issue?

2

Probably Your looking for this .

public class MyClass {
public Integer attr1;
public String attr2;
public Decimal attr3;
}
MyClass c = new MyClass();
c.attr1=1;
c.attr3=3.2;
c.attr2='hello';

String s = JSON.serialize(c);
Map<String,Object> obj =  (Map<String,Object>) JSON.deserializeUntyped(s);
system.debug(obj.keyset());
for(string key:obj.keyset())
    system.debug(obj.get(key));

Convert instance of the object to jSon so that you get all field names. and Convert back to Map. So that you access it directly with keyset of Map dynamically .

|improve this answer|||||
  • @KarelL Your Welcome.... – NITHESH K Nov 22 '17 at 10:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.