You need to sign in to do that
Don't have an account?
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.
I am new to apex, please suggest what I am doing wrong here.
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.
Apex Code Development
What is the error you were getting and which line?
Thanks!!
System.StringException: Ending position out of bounds: -1
Anonymous code : list<string> str = new list<string>{'Amy','Sandy','Mary'}; FormatSendersInv.getString(str);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!!
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