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
Jereriah ManningJereriah Manning 

Find/Replace (multiple times and objects) in a String (essentially a text field)

Is there a way to search a text field and create a List or Map with entries each time a specific string of characters is in the text field? I'm looking for a way to go through a text (not Rich) field and make merge field replacements, in much the same way the standard email templates work in SF.

Unlike a standard email, my text field can have fields from up to 4 objects + Sending User fields; an email template on steroids.

For instance:

{!Contact.FirstName} {!Contact.LastName}
{!Account.BillingStreet}
{!Account.BillingCity} etc...

Dear {!Contact.FirstName},

Account Name: {!Account.Name}

Possible {!Lead.Name} in here for good measure. This is my body. More fields possible down below. {!Custom_object__c.Field} too!

Sent by: {!User.FirstName} {!User.LastName}

I want a List/Map for each object in the above text field with the field merge field name in it.

List<String> contactFields as
{!Contact.FirstName}
{!Contact.LastName}
etc... 
List<String> accountFields
List<String> LeadFields
List<String> customerObjectFields
List<String> sendingUserFields

OR

Map<String, String> contactFields ('{!Contact.FirstName}', 'FirstName')
etc...
Map<String, String> accountFields ('{!Account.Name}', 'Name')

Once I have the Lists, I can do some trimming/looping to create SOQL statements and return the field values for the merge fields. In essence, I'm trying to loop through a single field.

I'll worry about doing the actual merge once I get over this hurdle as it will take another pass through the text field making replacements. I actually feel better about this one though—a while loop checking for ‘{!’ In the text field.

Thanks in advance!
~J

@Conga_Jereriah
Best Answer chosen by Jereriah Manning
Dev.AshishDev.Ashish
Jereriah,
You can use regex to extract text string  between "{!" and "}" like below.

String str = '{!Contact.FirstName} {!Contact.LastName} {!Account.BillingStreet} {!Account.BillingCity} etc... Dear {!Contact.FirstName}, Account Name: {!Account.Name} Possible {!Lead.Name} in here for good measure. This is my body. More fields possible down below. {!Custom_object__c.Field} too! Sent by: {!User.FirstName} {!User.LastName}';

Pattern TAG_REGEX = Pattern.compile('\\{!(.+?)\\}');

Matcher matcher = TAG_REGEX.matcher(str);

while (matcher.find()) {
    System.debug(matcher.group(1));
}
instead of printing you can add return values to a list in while loop.

It will print below strings.
Contact.FirstName
Contact.LastName
Account.BillingStreet
 - - - - -
 - - - - -
 - - - - - -

All Answers

Dev.AshishDev.Ashish
Jereriah,
You can use regex to extract text string  between "{!" and "}" like below.

String str = '{!Contact.FirstName} {!Contact.LastName} {!Account.BillingStreet} {!Account.BillingCity} etc... Dear {!Contact.FirstName}, Account Name: {!Account.Name} Possible {!Lead.Name} in here for good measure. This is my body. More fields possible down below. {!Custom_object__c.Field} too! Sent by: {!User.FirstName} {!User.LastName}';

Pattern TAG_REGEX = Pattern.compile('\\{!(.+?)\\}');

Matcher matcher = TAG_REGEX.matcher(str);

while (matcher.find()) {
    System.debug(matcher.group(1));
}
instead of printing you can add return values to a list in while loop.

It will print below strings.
Contact.FirstName
Contact.LastName
Account.BillingStreet
 - - - - -
 - - - - -
 - - - - - -

This was selected as the best answer
Jereriah ManningJereriah Manning
Thank you. I'll play with that and see what I can do.

Cheers,
~J