function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AncaComaneciuAncaComaneciu 

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');
GauravGargGauravGarg
Hi Anca,

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:
  1. Use if else conditions.
  2. Use custom setting to dynamically populate data based on object value
  3. Use labels instead of attributes. 

Hope this helps. 

Thanks,

Gaurav
Skype: gaurav62990

Keshava Praveen 15Keshava Praveen 15
Hi, 

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.
 
Keshava Praveen 15Keshava Praveen 15
Hi, 

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.