0

I am writing a sample class to split a fields into several fields depending on the separator character '\\|!\\|'.

My problem is when I ran the flow, it's not possible to me to retrieve the values returned from the Apex Action.

It's maybe a problem of the method declaration with a List of List and/or the size of the List returned in the output that don't match the size of the List entered?

public class ImplementCrossCellSolution3 {

    @InvocableMethod(label='Parse Comments into Fields Version 3')
    public static List<List<OutputVariables>> parseComments(List<InputVariables> recordComments){

        String comments = recordComments[0].recordComments;

        HubSpotCommentsInfo HubInfo = new HubSpotCommentsInfo();
        List<List<OutputVariables>> outputHubSpotCommentsInfo = new List<List<OutputVariables>>(); //===============
        
        List<OutputVariables> outputresults = new List<OutputVariables>();
        OutputVariables outputresult = new OutputVariables();
 
        List<String> parts = comments.split('\\|!\\|');

                Integer index = 0;
                for(String part : parts) {
                    part = part.trim();

                    switch on index {
                        when 0 {
                            HubInfo.fields1 = part;
                            outputresult.fields1 = part;
                            system.debug(HubInfo.fields1);
                            system.debug(outputresult.fields1);
                            }
                        when 1 {
                            HubInfo.fields2 = part;
                            outputresult.fields2 = part;
                            system.debug(HubInfo.fields2);
                            system.debug(outputresult.fields2);
                            }
                        when 2 {
                            HubInfo.fields3 = part;
                            outputresult.fields3 = part;
                            //system.debug(HubInfo.fields3);
                            //system.debug(outputresult.fields3);
                            }
                        when 3 {
                            HubInfo.fields4 = part;
                            outputresult.fields4 = part;
                            //system.debug(HubInfo.fields4);
                            //system.debug(outputresult.fields4);
                            }
                    }
                    index++;
                    outputresults.add(outputresult);
                    }
                //return outputresults;
                outputHubSpotCommentsInfo.add(outputresults); // ===================
                return outputHubSpotCommentsInfo; // ====================

    }
    
    public class InputVariables {  //=====================
        @InvocableVariable 
        public String recordComments;
        
    }

    public class OutputVariables {
        @InvocableVariable
        public String fields1;
        @InvocableVariable
        public String fields2; 
        @InvocableVariable
        public String fields3;
        @InvocableVariable
        public String fields4; 
    }
}
2
  • To be clear, there's potentially more than four parts, which is why you're trying to return a list of output variables?
    – sfdcfox
    Aug 22, 2022 at 12:30
  • Yes you have right, It's possible to have more than 4 parts. Following an example of an input value for the Variable 'recordComments' : |!| Testing-sell |!|, |!| 25-50 |!|, |!| Sales rep |!|, |!| ocabdel|!|, testlead|!|, |!| elgrd@gmail.com |!|, |!| gmail |!|, |!| ;|!|, |!| Mkt |!| In total 17 parts
    – ocabdel
    Aug 22, 2022 at 12:35

1 Answer 1

0

The input size and output size must match. That means your code must iterate over all of the InputVariables, like this:

public class ImplementCrossCellSolution3 {

    @InvocableMethod(label='Parse Comments into Fields Version 3')
    public static List<List<OutputVariables>> parseComments(List<InputVariables> recordComments){
        List<List<OutputVariables>> outputHubSpotCommentsInfo = new List<List<OutputVariables>>();
        for(InputVariables recordComment: recordComments) {
            List<OutputVariables> output = new List<OutputVariables>();
            String comments = recordComment.recordComments;
            List<String> parts = comments.split('\\|!\\|');
            OutputVariables outputresult = new OutputVariables();
            for(Integer index = 0; index < parts.size(); index++) {
                switch on Math.mod(index, 4) {
                    when 0 {
                        outputresult.fields1 = parts[index].trim();
                    }
                    when 1 {
                        outputresult.fields2 = parts[index].trim();
                    }
                    when 2 {
                        outputresult.fields3 = parts[index].trim();
                    }
                    when 3 {
                        outputresult.fields4 = parts[index].trim();
                        output.add(outputresult);
                        outputresult = new OutputVariables();
                    }
                }
            }
            if(Math.mod(parts.size(), 4) != 0) {
                outputresults.add(outputresult);
            }
            outputHubSpotCommentsInfo.add(outputresults);
        }
        return outputHubSpotCommentsInfo;
    }
    
    public class InputVariables {  //=====================
        @InvocableVariable 
        public String recordComments;
        
    }

    public class OutputVariables {
        @InvocableVariable
        public String fields1;
        @InvocableVariable
        public String fields2; 
        @InvocableVariable
        public String fields3;
        @InvocableVariable
        public String fields4; 
    }
}
7
  • I just tested your code but it seems to return only 1 value for the output : Outputs [OutputVariables : { "fields1" : "HR" }] At less, I need to have the 4 fields for the outputs if it's possible :-) Thanks a lot!
    – ocabdel
    Aug 22, 2022 at 13:05
  • @ocabdel I just tested this code in my org, it seems to work fine. Your string from the comment parsed as: 07:22:19:039 USER_DEBUG [6]|DEBUG|((OutputVariables:[fields1=, fields2=Testing-sell, fields3=,, fields4=25-50], OutputVariables:[fields1=,, fields2=Sales rep, fields3=,, fields4=ocabdel], OutputVariables:[fields1=, testlead, fields2=,, fields3=elgrd@gmail.com, fields4=,], OutputVariables:[fields1=gmail, fields2=,, fields3=;, fields4=,], OutputVariables:[fields1=Mkt, fields2=null, fields3=null, fields4=null]))
    – sfdcfox
    Aug 22, 2022 at 14:09
  • Ok thank you. May be I used your code incorrectly. You changed something in your code? Thanks
    – ocabdel
    Aug 22, 2022 at 15:13
  • Hi again, also I have another problem, I can't store the values fields spitted into an output variable in the flow! I think, It need to take a variable of type String of collection or something else? Thank you!
    – ocabdel
    Aug 22, 2022 at 15:32
  • Hello, I found the way to store the output values by fixing the declaration of the @InvocableMethod by this : public static List<OutputVariables> parseComments(List<InputVariables> recordComments){ .... } Instead of : public static List<List<OutputVariables>> parseComments(List<InputVariables> recordComments){ ... } Thanks!
    – ocabdel
    Aug 23, 2022 at 12:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .