3

Looking for a way to dynamically set a class structures fields by list or map of the string version. I know we can do this with Objects. Here is kinda of what I am wanting to do. This is a simple model and it works when working with SObjects

public class myClass{
  public string name;
  public Date  currentDate;
  public string email;
}


public myClass loadValues{
   Map<string, string> structMappedValues  = new Map<string,string>();
   structMappedValues.put('name', 'new name');
   structMappedValues.put('currentDate', '2015-01-01');

   myClass mc = new myClass();
   for(String field : structMappedValues){
      mc.put(field, structMappedValues.get(field));
   }
   return mc;
}
1
  • You can do this with JSON.deserialize -- one statement can take a nested json object and deserialize into a corresponding set of related (inner) classes
    – cropredy
    Jul 21, 2015 at 22:20

2 Answers 2

3

Apex does not support field level reflection so there is no convenient API to populate named fields of a class.

However, for specific cases you can make use of the Apex's support of JSON serialization/deserialization. You can serialize your map and deserialize the JSON string into an instance of myClass:

myClass mc = (myClass) JSON.deserialize(JSON.serialize(structMappedValues), myClass.class);

providing the field names match and the data formats such as the date string is in the format that the deserialization expects.

0
1

Thanks to Keith C. I have my solution for the overall aspect of what I was doing. Here is the almost completed class. I leave some for you to do but this covers a large portion. Hope it helps others out there.

This class allows for a field Mapping reference between a SObject and a class structure and then loads the class structure based on data from the SObject, can easily be done the other way also with some modification.

public class myClass{
  public string name;
  public Date  currentDate;
  public string email;
}

public Set<String> getAttributes() // found example here on stackExchange
{
     myClass m = new myClass();
     // Serialize out Ojbect into JSON
     String s = JSON.serialize(m);
     // Deserialize it back into a key/value map
     Map<String,Object> obj = (Map<String,Object) JSON.desializeUntyped(m);
     // Return a Set of those fields present on our object
     return obj.keyset().clone();
}

// build an sObject to myClass field name map
// Acutal not shown sorry 
{  
   Map<String,String> fieldMap  = new Map<String,String>();
   fieldMap.put(sObjectFieldName, structureFieldName);
   // this puts sObject fields and maps them to myClass Structure fields
}

public myClass loadValues{

   // Get Data from SObject based on fieldMap
   // Actual Not shown sorry
   List<SObject> sObjects = [SELECT id, name FROM SObject];
   for(SObject s : SObjects){
        Map<string, string> structMappedValues  = new Map<string,string>();

        myClass mc = new myClass();
        for(String field : fieldMap  ){               
           structMappedValues.put(fieldMap.get(field), (string)s.get(field))
        }
        //Thanks Keith C - convert mapped field and data to class struct
        mc = (myClass) JSON.deserialize(JSON.serialize(structMappedValues),myClass.class);
   }
   return mc;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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