TopDesk中Freemarker响应体条件判断失效问题求助
问题解决:TopDesk动作序列中判断Step1响应是否存在并执行Step2
问题背景
在TopDesk配置动作序列,需求如下:
- Step1返回JSON数组响应(或无返回),示例响应:
[{"self":"https://xxxxx.atlassian.net/rest/api/3/user?accountId=60dd8a8a6d066300689dbc42","accountId":"60dd8a8a6d066300689dbc42","accountType":"atlassian","emailAddress":"h.h@t.com","avatarUrls":{"48x48":"https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/60dd8a8a6d066300689dbc42/2fbb32d1-6a7b-4d72-8e5d-5053f47fc2e9/48","24x24":"https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/60dd8a8a6d066300689dbc42/2fbb32d1-6a7b-4d72-8e5d-5053f47fc2e9/24","16x16":"https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/60dd8a8a6d066300689dbc42/2fbb32d1-6a7b-4d72-8e5d-5053f47fc2e9/16","32x32":"https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/60dd8a8a6d066300689dbc42/2fbb32d1-6a7b-4d72-8e5d-5053f47fc2e9/32"},"displayName":"XXXXX YYYYYYY","active":true,"timeZone":"Europe/Berlin","locale":"en_US"}]
- Step2仅在Step1有响应时执行,但尝试多种FreeMarker表达式(如
?contains、?size、_responses.CheckIfAUserExists??)均报错,错误信息:
Step 2 'StopandReturnifuserexists' [2024-05-15T22:40:40.363967Z] Starting FreeMarker template processing [2024-05-15T22:40:40.364963Z] Error while processing FreeMarker template: For "." left-hand operand: Expected a hash, but this has evaluated to a sequence (ArrayList wrapped into f.t.DefaultListAdapter): ==> _responses.CheckIfAUserExists.body [in template "action sequence" at line 1, column 75]
错误原因
报错核心是:Step1的响应体是JSON数组,在FreeMarker中被解析为序列(Sequence),而非哈希(Hash)。之前的表达式要么错误地对序列使用哈希的.操作,要么未正确组合“响应存在”和“序列非空”的判断逻辑。
解决方案
使用以下FreeMarker表达式,先判断Step1的响应对象是否存在,再判断响应体序列是否非空:
方案1:通过序列大小判断
<#if _responses.CheckIfAUserExists?? && _responses.CheckIfAUserExists.body?size > 0> <!-- 这里写Step2要执行的逻辑 --> <#else> <!-- Step1无响应或响应为空时的逻辑 --> </#if>
方案2:使用?has_content判断
?has_content对序列类型来说,只要序列长度大于0就返回true,写法更简洁:
<#if _responses.CheckIfAUserExists?? && _responses.CheckIfAUserExists.body?has_content> <!-- Step2执行逻辑 --> </#if>
关键说明
_responses.CheckIfAUserExists??用于处理Step1完全无返回的场景(此时该响应对象不存在)- 若后续需要访问响应体中的具体用户属性,需先确认序列非空,再通过索引访问(如
_responses.CheckIfAUserExists.body[0].accountId)
内容的提问来源于stack exchange,提问作者Holger Homann
相关产品推荐
相关产品推荐

