You need to sign in to do that
Don't have an account?
Jonathan Spink
How do I (dynamically) refer to a field in a loop given the fieldname as an input string?
So I'm looping through the records from a List and want to check the value of a field against the value from the previous record. But I want to be able to pass the name of the field to the method. Something like:
public static void (List<Contact> cList, string fieldname) {
integer i = 1;
for ( Contact c : cList) {
if (c[i].fieldname == c[i-1].fieldname) {
...
} else {
...
}
public static void (List<Contact> cList, string fieldname) {
integer i = 1;
for ( Contact c : cList) {
if (c[i].fieldname == c[i-1].fieldname) {
...
} else {
...
}
Yes we can pass the field name dynamically . Once you have passed the field name dynamically, capture the field name in a string and use get method to fetch the value.
Hope this helps, If so kindly mark this as best answer.
Thanks,
Karthik
All Answers
I think so using a generic for loop would solve your requirement (to compare current records field name with previous records field name) .
I have tested the below mock code and it is working as expected. Hope it helps.
In my example i took the field name as account and i am able to fetch current record and previous record values in for loop.
Thanks,
Karthik
Yes we can pass the field name dynamically . Once you have passed the field name dynamically, capture the field name in a string and use get method to fetch the value.
Hope this helps, If so kindly mark this as best answer.
Thanks,
Karthik