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
Darunphop Winiyakul 4Darunphop Winiyakul 4 

How to add value to each field which have field name like 'A1' , 'A2' , 'A3' using for loop

Hi,

Could you guys help me about this?
I'm newbie for programming.
I want to add value from my List to my custom fields which have sequential number as suffix of each field by using for loop.

Similar this..
Example
            My custom field API name : A_1__c,A_2__c,A_3__c

            List<User> userList = new List<User>();
            Integer counterXX = 1;
            for(User rUser : userList){
                xxx.A_counterXX__c = rUser.Id;
                counterXX++;
            }
Can I use this logic or what it should be? Please give me some advices.

Thank you.
Best Answer chosen by Darunphop Winiyakul 4
Lucian Mihai CiobanuLucian Mihai Ciobanu
You can use the put method on the sObject. Inside the for loop you can do something like this:
xxx.put('A_' + counterXX + '__c', rUser.Id);
The first parameter of the method is the field API name in a string format. The second one is the value you want to assign to that field.

All Answers

Lucian Mihai CiobanuLucian Mihai Ciobanu
You can use the put method on the sObject. Inside the for loop you can do something like this:
xxx.put('A_' + counterXX + '__c', rUser.Id);
The first parameter of the method is the field API name in a string format. The second one is the value you want to assign to that field.
This was selected as the best answer
Darunphop Winiyakul 4Darunphop Winiyakul 4
Thank you so much for your advice Lucian.
This solution solved my problem.