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
Gunwinder Singh 4Gunwinder Singh 4 

issue with apex

I need help with invocable apex class that takes collection (string) and formats it replacing the second last comma to an and:

Raw string : Amy, Rose, Gary
Formatted: Amy, Rose and Gary.

testing on anonymous code block, i get the desired output however when using it on a Apex class it fails.
 
Anoymous code block : 

string inputString = 'Amy, Rose,Mary';

integer indexOfSecondLastComma = inputString.lastIndexOf(',',
                                 inputString.lastIndexOf(','));

//replace 
string outputString = 
inputString.substring(0,indexOfSecondLastComma) + ' and '+
inputString.substring(indexOfSecondLastComma+1);

system.debug(outputString);
 
public class FormatSendersInv{
    
    @InvocableMethod(label='Format Contact Names'
                     description='Returns the string for sending email for deceased contacts'
                     category='WC_ApexActions')

    public static LIST<string> getString(LIST<string> StrCol){
            LIST<string> outputStrings = new LIST<string>(); 
    	    string inputString = '';
        
        for (string s:StrCol){
            inputString = inputString+s;
        }
        
        integer indexOfSecondLastComma = inputString.lastIndexOf(',',
                                                                 inputString.lastIndexOf(','));
        
        //replace 
        string outputString = 
            inputString.substring(0,indexOfSecondLastComma) + ' and '+
            inputString.substring(indexOfSecondLastComma+1);
        	outputStrings.add(outputString);
        	system.debug(outputString);
    return outputStrings;
    }
}

I am new to apex, please suggest what I am doing wrong here.
AnkaiahAnkaiah (Salesforce Developers) 
Hi Gunwinder,

What is the error you were getting and which line?

Thanks!!
Gunwinder Singh 4Gunwinder Singh 4
Line: 20, Column: 1
System.StringException: Ending position out of bounds: -1
Anonymous code : 

list<string> str = new list<string>{'Amy','Sandy','Mary'};
    FormatSendersInv.getString(str);

 
Gunwinder Singh 4Gunwinder Singh 4
@Ankaiah, can you suggest ? 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Gunwinder,

try with below code.
public class FormatSendersInv{
    
    @InvocableMethod(label='Format Contact Names'
                     description='Returns the string for sending email for deceased contacts'
                     category='WC_ApexActions')

    public static LIST<string> getString(LIST<string> StrCol){
            LIST<string> outputStrings = new LIST<string>(); 
    	    string inputString = '';
        
        for (string s:StrCol){
		s.remove(null);
         inputString = string.join(s,',');

        }
        
        integer indexOfSecondLastComma = inputString.lastIndexOf(',',
                                                                 inputString.lastIndexOf(','));
        
        //replace 
        string outputString = 
            inputString.substring(0,indexOfSecondLastComma) + ' and '+
            inputString.substring(indexOfSecondLastComma+1);
        	outputStrings.add(outputString);
        	system.debug(outputString);
    return outputStrings;
    }
}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
Gunwinder Singh 4Gunwinder Singh 4
Thank you for respoding Ankaiah.

I am unable to save the class, it gets me : Method does not exist or incorrect signature: void join(String, String) from the type String at line 13