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
Varun Sharma 150Varun 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);
}
 
Best Answer chosen by Varun Sharma 150
Varun Sharma 150Varun Sharma 150
Hi
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

Rishab Wali 3Rishab Wali 3
Hi Varun ,

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
Varun Sharma 150Varun Sharma 150
Hi
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;
        }
    }
This was selected as the best answer