You need to sign in to do that
Don't have an account?
Varun Sharma 150
Dynamic Property Selection in APEX
Hello Friends
I have a question where I have a map with Key as Name of properties of Object with values and I want to iterate over collection and assign value to relevant property i.e.
I have a custom object with 4 properties, First, Second, Third and Forth
Now I also have a Map<string, string> Values which has data as
<First, 10>
<Second,20>
<Third, 30>
<Forth, 40>
Now I want a way to iterate over all the Keys in MAP and assign the value of corrosponding property of the object from value from MAP.
for(string keyValue : Map.GetKeys())
{
Object.Proeprty_Name = Map.Get(keyValue);
}
I have a question where I have a map with Key as Name of properties of Object with values and I want to iterate over collection and assign value to relevant property i.e.
I have a custom object with 4 properties, First, Second, Third and Forth
Now I also have a Map<string, string> Values which has data as
<First, 10>
<Second,20>
<Third, 30>
<Forth, 40>
Now I want a way to iterate over all the Keys in MAP and assign the value of corrosponding property of the object from value from MAP.
for(string keyValue : Map.GetKeys())
{
Object.Proeprty_Name = Map.Get(keyValue);
}
I used following method to set values
public static void UpdateObjectProperty(sObject objBaseObject, string BeforePropertyName, string AfterPropertyName, object PropertyValue, ROIHelper.METRIC_TIME TimeOfMetric)
{
try
{
if(objBaseObject!=null)
{
if(TimeOfMetric == ROIHelper.METRIC_TIME.BEFORE)
{
if(BeforePropertyName!=null && BeforePropertyName.length() >0)
{
objBaseObject.put(BeforePropertyName, PropertyValue);
}
}
if(TimeOfMetric == ROIHelper.METRIC_TIME.AFTER)
{
if(AfterPropertyName!=null && AfterPropertyName.length() >0)
{
objBaseObject.put(AfterPropertyName, PropertyValue);
}
}
}
}
catch(exception exp)
{
ROIHelper.ExceptionHandler(exp);
throw exp;
}
}
All Answers
You can use the sObject method put(fieldname,value) to set the values on the basis of the map values.
Example -- Account acc = new Account();
for(string keyValue : Map.Keyset())
{
acc.put('keyValue',Map.get(keyValue));
}
Hope this helps.
Thanks & Regards
Rishab Wali
I used following method to set values
public static void UpdateObjectProperty(sObject objBaseObject, string BeforePropertyName, string AfterPropertyName, object PropertyValue, ROIHelper.METRIC_TIME TimeOfMetric)
{
try
{
if(objBaseObject!=null)
{
if(TimeOfMetric == ROIHelper.METRIC_TIME.BEFORE)
{
if(BeforePropertyName!=null && BeforePropertyName.length() >0)
{
objBaseObject.put(BeforePropertyName, PropertyValue);
}
}
if(TimeOfMetric == ROIHelper.METRIC_TIME.AFTER)
{
if(AfterPropertyName!=null && AfterPropertyName.length() >0)
{
objBaseObject.put(AfterPropertyName, PropertyValue);
}
}
}
}
catch(exception exp)
{
ROIHelper.ExceptionHandler(exp);
throw exp;
}
}