You need to sign in to do that
Don't have an account?
AncaComaneciu
Dynamically set class variable value
Is it possible to dynamically set class variable value?
I have a dynamyc form that should send the data to an external service (SOAP). The fields that are necessary depend on specific settings and can change. Can we set the Object attributes dynamically similar to how it's possible with sObject?
I would like to be able yo do someting like this:
public class dynForm {
public String att1;
public String att2;
//additional attributes
}
dynForm myObject = new dynForm();
myObject.put('att1', 'value1');
I have a dynamyc form that should send the data to an external service (SOAP). The fields that are necessary depend on specific settings and can change. Can we set the Object attributes dynamically similar to how it's possible with sObject?
I would like to be able yo do someting like this:
public class dynForm {
public String att1;
public String att2;
//additional attributes
}
dynForm myObject = new dynForm();
myObject.put('att1', 'value1');
Everything is possible. But we need to understand how to define that thing.
Here what I understood is based on the object value you want to set dynamic attributes. There are multiple ways:
Hope this helps.
Thanks,
Gaurav
Skype: gaurav62990
Yes, it is possible to dynamically set the values.
Here is the pseudocode.
public class MyClass {
public Integer attr1;
public String attr2;
public Decimal attr3;
public String attr4;
}
Creating the object and setting up the attribute.
MyClass c = new MyClass();
c.attr1=1;
c.attr3=3.2;
c.attr2='hello';
//Serialize object
String s = JSON.serialize(c);
//Casting
Map<String,Object> obj = (Map<String,Object>) JSON.deserializeUntyped(s);
// You can define the custom setting or custom metadta which stroes the attribute name as key and value to be update into attribute in value field.
//value to be bound to attribute cast it to object type
Object oval = (Object) 'test';
// use the Put method to set up the attribute value.
obj.put('attr4',oval);
system.debug(obj.keyset());
for(string key:obj.keyset()) {
system.debug('Key is'+ key);
system.debug(obj.get(key));
}
// Serialize the Map back to String.
string jsonstring = JSON.serialize(obj);
MyClass objFinal = new MyClass();
// Type cast the JSON string to object.
objFinal = (MyClass)System.JSON.deserialize(jsonstring, MyClass.class);
Regards,
Praveen.
Yes, it is possible to dynamically set the values.
Here is the pseudocode.
public class MyClass {
public Integer attr1;
public String attr2;
public Decimal attr3;
public String attr4;
}
Creating the object and setting up the attribute.
MyClass c = new MyClass();
c.attr1=1;
c.attr3=3.2;
c.attr2='hello';
//Serialize object
String s = JSON.serialize(c);
//Casting
Map<String,Object> obj = (Map<String,Object>) JSON.deserializeUntyped(s);
// You can define the custom setting or custom metadta which stroes the attribute name as key and value to be update into attribute in value field.
//value to be bound to attribute cast it to object type
Object oval = (Object) 'test';
// use the Put method to set up the attribute value.
obj.put('attr4',oval);
system.debug(obj.keyset());
for(string key:obj.keyset()) {
system.debug('Key is'+ key);
system.debug(obj.get(key));
}
// Serialize the Map back to String.
string jsonstring = JSON.serialize(obj);
MyClass objFinal = new MyClass();
// Type cast the JSON string to object.
objFinal = (MyClass)System.JSON.deserialize(jsonstring, MyClass.class);
Regards,
Praveen.