如何从Invocable.Action.Result列表提取IndividualScore存入Integer列表?
从Invocable Action结果中提取IndividualScore存入Integer列表
我调用Invocable Action得到了List<Invocable.Action.Result>类型的结果列表:
List<Invocable.Action.Result> results = action.invoke();
结果的结构大致如下:
(Result":[action=Action":[invocations=("{"QuestionDevName=testA","ResponeValue=WBFH"}"),invokeFromLightningComponent=false,name=Tser,namespace=null,type=runDecisionMatrix],errors=(),invocationParameters=(already output),outputParameters="{"IndividualScore=20.0}",success=true])
我已经声明了List<Integer>类型的列表:
List<Integer> testInteger = new List<Integer>();
希望把结果中的IndividualScore值提取出来存入testInteger列表,尝试了下面的写法但有问题,求正确实现方式:
testInteger = results.get(Result.outputParameters.IndividualScore);
正确实现方式
要提取目标值,需遵循Invocable.Action.Result的API规范处理,示例代码如下:
List<Integer> testInteger = new List<Integer>(); if(results != null && !results.isEmpty()){ for(Invocable.Action.Result singleResult : results){ // 先校验调用是否成功 if(singleResult.isSuccess()){ Map<String, Object> outputParams = singleResult.getOutputParameters(); // 检查目标键是否存在 if(outputParams.containsKey('IndividualScore')){ Object scoreValue = outputParams.get('IndividualScore'); // 处理数值类型转换,兼容Double/Integer两种情况 if(scoreValue instanceof Double){ testInteger.add(((Double)scoreValue).intValue()); } else if(scoreValue instanceof Integer){ testInteger.add((Integer)scoreValue); } } } else { // 可选:处理调用失败的场景 System.debug('Invocable调用失败,错误信息:' + singleResult.getErrors()); } } }
原写法的问题
- 语法错误:
results是列表,get()方法需要传入索引值,而非直接访问属性; - API使用错误:
Invocable.Action.Result的outputParameters需通过getOutputParameters()方法获取,无法直接通过.访问; - 类型不匹配:
IndividualScore是Double类型,需显式转换为Integer才能存入List<Integer>。
内容的提问来源于stack exchange,提问作者Abhijeet
相关产品推荐
相关产品推荐

