0

I have some strings variables:

test1 test2 test3 ...

Each of them with some text inside

I have in my html email template :

test1 test2 test3 ..

I would like to know how to iterate with :

body = body.replace('test1',test1);  

Not manually like that but directly in a loop.

I was thinking about something like

 for (Integer i =0; i<10; ++i) {  


               body = body.replace(test[i],test[i]); 
           }

But it is not working.

0

Put the variables in the list:

List<String> test = new List<String>{test1, test2, test3, ...};

Then, when you are replacing variables in body, you need to replace String, not a variable, as in the question:

for (Integer i = 0; i < test.size(); i++) {
    body = body.replace('test' + String.valueOf(i+1), test[i]);
}

For example:

String test1 = '1';
String test2 = '22';
String test3 = '333';
List<String> test = new List<String>{test1, test2, test3};

String body = 'test1 got to the test2 and then test3 happen';

for (Integer i = 0; i < test.size(); i++) {
    body = body.replace('test' + String.valueOf(i+1), test[i]);
}

system.debug(body); //1 got to the 22 and then 333 happen
2
  • Thank you for the answer. I have still a small problem on my list of string (see edited version)
    – Tiburce
    Jul 3 '19 at 12:31
  • Regarding your update: try to mix the variables. For example, bound inputs and set them as string separated by ',' then split the String by comma and pass it into list. Jul 3 '19 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.