+ Start a Discussion
Shaijan ThomasShaijan Thomas 

Class variable are not able to access dynamically1

Hi All,

I am trying to access the class varible dynamically for one of my requirment. Please see the code.

public class CaseUpdateTestingClass {
    public string AssetSN = '1AED45';
    public string CDIDId='3498474';
    public string ContactFirstName='SalesForce';
}
CaseUpdateTestingClass es = new CaseUpdateTestingClass();
string ss = 'AssetSN;CDIDId;ContactFirstName'; // Here I do dynamic assignment
for ( string a : ss.split(';'))
system.debug('*********** : '+es.get(a));

Thanks
Shaijan Thomas
Sandeep Mishra 7Sandeep Mishra 7
Hi Shaijan,
In your code AssetSN, CDIDId, ContactFirstName are member variable. We should always refer them with the object you have created. Consider following code:
public class CaseUpdateTestingClass {
    public string AssetSN = '1AED45';
    public string CDIDId='3498474';
    public string ContactFirstName='SalesForce';
}

//Call out here .. Remmeber that we are calling the member with there Object's dot notation.
CaseUpdateTestingClass es = new CaseUpdateTestingClass();
string ss =es.AssetSN+';'+es.CDIDId+';'+es.ContactFirstName ; // Here I do dynamic assignment

This worked with me. I hope this helps.

Regards,
Sandeep Mishra
Keyur  ModiKeyur Modi
Hi,
Try this bunch of code.
 
public class CaseUpdateTestingClass {
    public string AssetSN = '1AED45';
    public string CDIDId='3498474';
    public string ContactFirstName='SalesForce';
}
CaseUpdateTestingClass es = new CaseUpdateTestingClass();
string ss =es.AssetSN+';'+es.CDIDId+';'+es.ContactFirstName ; // Here I do dynamic assignment
for ( string a : ss.split(';'))
system.debug('*********** : '+es.get(a));


OR else make your all three variable as static variable and try to do the same 

public class CaseUpdateTestingClass {
    public static string AssetSN = '1AED45';
    public static string CDIDId='3498474';
    public static string ContactFirstName='SalesForce';
}
string ss =CaseUpdateTestingClass.AssetSN+';'+CaseUpdateTestingClass.CDIDId+';'+CaseUpdateTestingClass.ContactFirstName ; // Here I do dynamic assignment
for ( string a : ss.split(';'))
system.debug('*********** : '+es.get(a));

please let me know if this will help.

Thanks,
Keyur Modi
Shaijan ThomasShaijan Thomas
Thanks for you replay. I need to dynamically get the values from the class

string ss = 'AssetSN;CDIDId;ContactFirstName'; This is an example, 

Suppose I have 100 value and store in custom setting or label, i can access it easily

string ss = 'v1, v2......v100';

Thanks
Shaijan Thomas
Gouranga SadhukhanGouranga Sadhukhan
Hi Shaijan ,

try this way.
public class CaseUpdateTestingClass {
    public string AssetSN = '1AED45';
    public string CDIDId='3498474';
    public string ContactFirstName='SalesForce';
}
string fieldvalue='';
CaseUpdateTestingClass es = new CaseUpdateTestingClass();
   String serialized = JSON.serialize(es );
  Map<String, Object> parameters = new Map<String, Object>();
   parameters = (Map<String, Object>) JSON.deserializeUntyped(serialized);
      
      fieldvalue=parameters.get( 'AssetSN ');