+ Start a Discussion
WhyserWhyser 

Reflection in APEX for custom Classes

Is it possible to do any reflective calls on custom classes?

 

I have something like

 

public class CustomTaskObject { public Boolean isSelected {get;set;} public String Id {get;set;} public String Subject {get;set;} public String AccountName {get;set;} public WhatName {get;set;} public WhatStatus {get;set} public Double Timezone {get;set} ... }

 

Basically I populate a list of this CustomTaskObject, and do a very special sort done by Timezone (10 hours ahead of the current user's Timezone, desc), and then by "WhatStatus" priority (which is all done in code as there is no way to do this in SOQL) and throw it into a VisualForce pageBlockTable.

 

Users want to have the functionality to sort by other columns, such as AccountName, or WhatStatus, and I would like to have a way to make it so that I can call

 

object.getField('WhatStatus')

 

to simplify the sorting routine. Otherwise I would have to write a sort procedure to handle each field that is available for sorting.

 

Is reflection possible?

 

I have seen in other threads where people are dealing with sObjects which do have reflection, but I'm guessing that this is not possible with custom classes?

 

Thanks!

rnavaretternavarette

I'd be interested in this as well, but maybe a little more precise, like describe works on sObjects.  This feature would be really helpful working with some of our webservices to get at the data returned in a more generic way.

 

thanks,

ray

tggagnetggagne

I'm wondering the same thing for the same reason.  How can a utility method, like for sorting, discover what the properties of a custom class are?

Craig IsaksonCraig Isakson

Well, this isn't the best example but I have needed to do something similar for custom classes.  I essentially created my own getField method which took a string and got the value a map of the object fields and values:

 

public class YourCustomObject{
    public String someField{get;set;}

    public YourCustomObject(){
        this.someField = 'someText';
    }

    public getField(String fieldName){
        Map<String, String> fieldMap = new Map<String, String>();
        fieldMap.put('someField', this.someField);
        
        return (fieldMap.get(fieldName) == null)?'':fieldMap.get(fieldName);
    }
}

 And then you could just instantiate the class and call the get field method:

 

YourCustomObject c = new YourCustomObject();

System.assertEquals('someText', c.getField('someField'));