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
Andrey BolkonskyAndrey Bolkonsky 

Trouble Converting Method that Returns String into Invocable Method

This is my second post trying to figure this out. I have a method of the return type string with one integer Parameter. It is my first Apex class so I want to make it invocable so that if something goes wrong with it I can just deactivate the flow. 

However I can't wrap my head around the invocable method annotation, specifically turning everything in my method into some kind of list. Can anyone explain how I might do this?
 
//@InvocableMethod(label='Write Number' description='Writes the Number' category='Opportunity')
public static String writeNumber(Integer Amt) {
                Integer Amount=Amt;
                Integer Len;
                String spelledAmount;
                
                Map<Integer, String> numberName=new Map<Integer, String> { }; 
                
            String amountString=String.valueOf(Amount);
            Len=amountString.Length();
                
          List<Integer> arrayNumber=new List<Integer>();  
            String[] chars=amountString.split('');
            for(String c:chars) {
                    arrayNumber.add(Integer.valueOf(c));        
            }

           Switch on Len { }

//Body of method

return spelledAmount;

}



 
Best Answer chosen by Andrey Bolkonsky
Vishwajeet kumarVishwajeet kumar
Hello,
Method processing logic has to be changed to bulk.

Try something like this : 

@InvocableMethod(label='Write Number' description='Writes the Number' category='Opportunity')
public static List<String> writeNumber(List<Integer> Amts) {
  List<String> spelledAmounList = new List<String>();

for(integer Amt : amts){                      //Process input in loop
Integer Amount=Amt;
Integer Len;
String spelledAmount;
Map<Integer, String> numberName=new Map<Integer, String> { };
String amountString=String.valueOf(Amount);
Len=amountString.Length();
List<Integer> arrayNumber=new List<Integer>();
String[] chars=amountString.split('');
for(String c:chars) {
arrayNumber.add(Integer.valueOf(c));

spelledAmounList.add(spelledAmount);      //Add individual items to  return List
}

Switch on Len { } //Body of method
}

return spelledAmounList ;
}


Not sure what logic goes into loop, it can be optimized for bulk processing.

Thanks

All Answers

Vishwajeet kumarVishwajeet kumar
Hello,
Method processing logic has to be changed to bulk.

Try something like this : 

@InvocableMethod(label='Write Number' description='Writes the Number' category='Opportunity')
public static List<String> writeNumber(List<Integer> Amts) {
  List<String> spelledAmounList = new List<String>();

for(integer Amt : amts){                      //Process input in loop
Integer Amount=Amt;
Integer Len;
String spelledAmount;
Map<Integer, String> numberName=new Map<Integer, String> { };
String amountString=String.valueOf(Amount);
Len=amountString.Length();
List<Integer> arrayNumber=new List<Integer>();
String[] chars=amountString.split('');
for(String c:chars) {
arrayNumber.add(Integer.valueOf(c));

spelledAmounList.add(spelledAmount);      //Add individual items to  return List
}

Switch on Len { } //Body of method
}

return spelledAmounList ;
}


Not sure what logic goes into loop, it can be optimized for bulk processing.

Thanks
This was selected as the best answer
Andrey BolkonskyAndrey Bolkonsky
Vishwajeet,

This worked! Thanks so much for your help!