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
Dipa87Dipa87 

Need help in string formatting function.

I need help to write a method which will do some kind of string formatting.

 

I have list of ids of an object. It may be any object.Like this:

List listContactIds = [select id from contact limit 5];

 

And I have a certain string format like this:

String format = ‘{name} belongs to {Account.Name}’;

 

I need to call a method. Suppose the method name is formatString.

formatString(listContacts, format);

 

The method should be able to return list of formatted strings.Like this Rahul belongs to Google

We can take the exact field names enclosed in {} from the 'format' string

How to acheive this?

Best Answer chosen by Admin (Salesforce Developers) 
magicforce9magicforce9

Hi,

 

Ive just tried the below code and it prined some thing similar to what you are looking for. You can change or improve it.

 

//By Using sObject List you can store records of any object
List<sObject> acc = [Select id, name from Account];

//Insted of using a single string as format, I've used a list of sting where the first and last value is
// field names and the middle one is the text you want to use in formatting. String[] fieldformat = new String[]{'name', 'belongs to', 'id'};

//This is a method that does the formatting for you and returns you a list of formatted strings List<String> formatString (List<sObject> objects, List<String> format) { List<String> formattedStrings = new List<String>(); for(sObject obj : objects) { String Str = String.valueOf(obj.get(format[0])) + ' ' + format[1] +
' ' + String.valueOf(obj.get(format[2])); formattedStrings.add(Str); } return formattedStrings; } List<String> outPut = formatString(acc, fieldformat); System.debug(outPut[0]);

 

All Answers

magicforce9magicforce9

Hi,

 

Ive just tried the below code and it prined some thing similar to what you are looking for. You can change or improve it.

 

//By Using sObject List you can store records of any object
List<sObject> acc = [Select id, name from Account];

//Insted of using a single string as format, I've used a list of sting where the first and last value is
// field names and the middle one is the text you want to use in formatting. String[] fieldformat = new String[]{'name', 'belongs to', 'id'};

//This is a method that does the formatting for you and returns you a list of formatted strings List<String> formatString (List<sObject> objects, List<String> format) { List<String> formattedStrings = new List<String>(); for(sObject obj : objects) { String Str = String.valueOf(obj.get(format[0])) + ' ' + format[1] +
' ' + String.valueOf(obj.get(format[2])); formattedStrings.add(Str); } return formattedStrings; } List<String> outPut = formatString(acc, fieldformat); System.debug(outPut[0]);

 

This was selected as the best answer
Dipa87Dipa87

Thanks it helped.

 

Tweeked the code based on my requirement.This is the working code which will handle relationship fields also:

 

//By Using sObject List you can store records of any object
List<sObject> acc = [Select name,Account.Name,Account.Id from Contact limit 10];

//Insted of using a single string as format, I've used a list of sting where the first and last value is
// field names and the middle one is the text you want to use in formatting.
String[] fieldformat = new String[]{'Name', 'belongs to', 'Account.Name'};

//This is a method that does the formatting for you and returns you a list of formatted strings
List<String> formatString (List<sObject> objects,  List<String> format)
{
    List<String> formattedStrings = new List<String>();
    for(sObject obj : objects)
    {
        String Str = ((format[0].contains('.')) ? getString(obj,format[0]) : String.valueOf(obj.get(format[0]))) + ' ' + format[1] + ' ' + ((format[2].contains('.')) ? getString(obj,format[2]): String.valueOf(obj.get(format[2])));
        formattedStrings.add(Str);
    }
     return formattedStrings;
}
List<String> outPut = formatString(acc, fieldformat);

String getString(Sobject obj, String str){
    String objName = str.substring(0,str.indexOf('.'));
    String fieldName = str.substring(str.indexOf('.')+1,str.length());
    return String.valueOf(obj.getSObject(objName).get(fieldName));
}
System.debug(outPut);