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
Arthur Almeida 12Arthur Almeida 12 

Field with @InvocableVariable(required=false) does not is ignored by einstein bot

I have an apex class like this:
01    public class Example {
02        
03        public class HandleInput {
04            @InvocableVariable(required=true)
05            public String id;
06        }
07        
08        public class HandleOutput {
09            @InvocableVariable(required=false)
10            public String name;
11            
12          @InvocableVariable(required=true)
13
14            public double value;
15        }
16        
17        @InvocableMethod
18        public static List<HandleOutput> function(List<HandleInput> inputParameters){
19            String id = inputParameters.get(0).id;
20            
21            ResponseIntegration response = new SomeIntegration().call(id); 
22            
23            List<HandleOutPut> outPuts = new List<HandleOutput>();
24            HandleOutput outPut = new HandleOutput();
25            //outPut.name = response.getName();
26            outPut.confidence = response.getValue();
27            
28            outPuts.add(outPut);    
29            return outPuts;
30        }
31    }

In Einstein Bot, when I put action apex, calling this class, the output shows me name and value
User-added image


Well, from the moment I commented line 25, and annotated the field as required=false (line 09), the Einstein bot must ignore the name field, and must KEEP the bot's name variable with the value it had before of this apex call, correct?

but this is not happening... the bot is not ignoring this field, it is setting NULL for the name field... I would like the field to keep the value it had before
 
Arthur Almeida 12Arthur Almeida 12
"required = Specifies whether the variable is required. If not specified, the default is false. The value is ignored for output variables." https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableVariable.htm
Shri RajShri Raj

From the code you've provided, it seems that when the name field is commented out on line 25, the value of the name field is not being set to anything. If the name field is annotated as required=false (line 09), it means that the field is optional and can be left empty.
In this case, the Einstein Bot is likely setting the name field to null because it is not receiving a value for that field from the Apex class. To keep the value of the name field as it was before the Apex call, you can set the name field to its previous value before the Apex call inside the method

 

public class Example {
    public class HandleInput {
        @InvocableVariable(required=true)
        public String id;
    }
        
    public class HandleOutput {
        @InvocableVariable(required=false)
        public String name;
        @InvocableVariable(required=true)
        public double value;
    }
        
    @InvocableMethod
    public static List<HandleOutput> function(List<HandleInput> inputParameters){
        String id = inputParameters.get(0).id;
        ResponseIntegration response = new SomeIntegration().call(id); 
        List<HandleOutPut> outPuts = new List<HandleOutput>();
        HandleOutput outPut = new HandleOutput();
        //keep the value of name as it was before the Apex call
        outPut.name = BotContext.current().getVariable('name').getValue();
        outPut.value = response.getValue();
        outPuts.add(outPut);    
        return outPuts;
    }
}

This will set the name field to the value it had before the Apex call, by getting the value of the name variable from the BotContext.
Please note that this is a simplified example and you will need to adjust it to match your specific use case.
Also, BotContext is used to get values from the current context of the bot flow, so you have to make sure this is being used in the correct context