mcpField
F
flow-designer

flow-designer

github.com/serac-labs开发7764 次安装9 周前更新
代码仓库

关于 flow-designer

面向 ServiceNow 开发者的 Flow Designer 模式技能,涵盖触发器、分支、子流程、错误处理和脚本操作。还说明了通过发现到的 Flow Designer 工具以编程方式创建和管理流程的规则。

主要功能

  1. 01配置记录、计划和服务目录触发器
  2. 02使用条件分支与子流程复用逻辑
  3. 03设置 TRY/CATCH 错误处理
  4. 04编写脚本操作和 REST 操作
  5. 05查看执行历史并调试流程

适用场景

  1. 01构建 SLA 升级和审批路由流程
  2. 02定期同步外部系统与记录
  3. 03为新开发设计可复用自动化

MCP 相关

建议通过 tool_search 搜索并使用 Flow Designer 工具管理流程生命周期;该工具处理所需的 GraphQL 变更。

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/serac-labs/serac --skill flow-designer

安装后在 Claude Code 中输入/flow-designer确认已加载。

详细介绍

ServiceNow 的 Flow Designer 模式

Flow Designer 是 ServiceNow 中现代化的自动化引擎,新开发项目中已由它取代旧版 Workflow。

使用 Flow Designer 工具

要通过编程方式创建和管理流程,首先通过 tool_search({query: "flow designer"}) 发现 Flow Designer 工具。该工具会处理整个流程生命周期中的所有 GraphQL 变更操作。

重要 — IF/ELSE/ELSEIF 放置规则:

  • IF 分支内部的操作:parent_ui_id = IF 的 uiUniqueIdentifier
  • ELSE/ELSEIF 块:必须与 IF 位于同一层级,不能嵌套在 IF 内部
    • parent_ui_id = IF 块使用的同一父级
    • connected_to = IF 的 logicId(创建 IF 时返回的 sysId)
  • 放置错误会导致保存流程时出现“Unsupported flowLogic type”错误

重要 — TRY/CATCH 放置规则:

添加 TRY 块时,该工具会自动创建对应的 CATCH 块。TRY 和 CATCH 都是容器,操作应放在它们内部,而不是与它们并列。

  • TRY 内部的操作(可能失败的步骤):
    • parent_ui_id = TRY 的 uiUniqueIdentifier
    • order:TRY 内的 1、2、3……
  • CATCH 内部的操作(错误处理):
    • parent_ui_id = CATCH 的 uiUniqueIdentifier(TRY 创建响应中返回的 catch_ui_id)
    • order:CATCH 内的 1、2、3……
  • 创建 TRY/CATCH 对时返回的 next_order 适用于该对之后的下一个同级项,不适用于其中的子项。将它用于 try/catch 之后的操作,而不是其中的操作。

不要通过后台脚本、GlideRecord 或对 sys_hub_* 表发起 REST 调用来创建流程。务必通过 tool_search({query: "flow designer"}) 发现并使用 Flow Designer 工具,它能正确处理 GraphQL 变更操作。后台脚本方式会悄无声息地生成损坏的流程。

Flow Designer 组件

组件用途可复用
Flow主要自动化流程否
Subflow可复用的流程逻辑是
Action单项操作(脚本、REST 等)是
Spoke相关操作的集合是

流程触发器

基于记录的触发器
Trigger: Created
Table: incident
Condition: Priority = 1

Trigger: Updated
Table: incident
Condition: State changes to Resolved

Trigger: Created or Updated
Table: change_request
Condition: Risk = High
计划触发器
Trigger: Daily
Time: 02:00 AM
Timezone: America/New_York

Trigger: Weekly
Day: Monday
Time: 08:00 AM
服务目录触发器
Trigger: Service Catalog
Catalog Item: Request New Laptop

Flow 最佳实践

1. 使用 Subflow 实现复用
Main Flow: Incident P1 Handler
├── Trigger: Incident Created (Priority = 1)
├── Action: Log Event
├── Subflow: Notify On-Call Team     ← Reusable!
├── Subflow: Create Major Incident   ← Reusable!
└── Action: Update Incident
2. 错误处理
Flow: Process Integration
├── Try
│   ├── Action: Call REST API
│   ├── Action: Parse Response
│   └── Action: Update Record
├── Catch (all errors)
│   ├── Action: Log Error Details
│   ├── Action: Create Error Task
│   └── Action: Send Alert
└── Always
    └── Action: Cleanup Temp Data
3. 流程变量
// Input Variables (from trigger)
var incidentSysId = fd_data.trigger.current.sys_id
var priority = fd_data.trigger.current.priority

// Scratch Variables (within flow)
fd_data.scratch.approval_required = priority == "1"
fd_data.scratch.notification_sent = false

// Output Variables (to calling flow/subflow)
fd_data.output.success = true
fd_data.output.message = "Processed successfully"
4. 条件和分支
If: Priority = Critical
  Then:
    - Notify VP
    - Create Major Incident
    - Page On-Call
  Else If: Priority = High
    - Notify Manager
    - Escalate in 4 hours
  Else:
    - Standard Processing

自定义 Action(脚本)

基本脚本 Action
;(function execute(inputs, outputs) {
  // Inputs defined in Action Designer
  var incidentId = inputs.incident_sys_id
  var newState = inputs.target_state

  // Process
  var gr = new GlideRecord("incident")
  if (gr.get(incidentId)) {
    gr.setValue("state", newState)
    gr.update()

    // Set outputs
    outputs.success = true
    outputs.incident_number = gr.getValue("number")
  } else {
    outputs.success = false
    outputs.error_message = "Incident not found"
  }
})(inputs, outputs)
带错误处理的脚本 Action
;(function execute(inputs, outputs) {
  try {
    var gr = new GlideRecord(inputs.table_name)
    gr.addEncodedQuery(inputs.query)
    gr.query()

    var records = []
    while (gr.next()) {
      records.push({
        sys_id: gr.getUniqueValue(),
        display_value: gr.getDisplayValue(),
      })
    }

    outputs.records = JSON.stringify(records)
    outputs.count = records.length
    outputs.success = true
  } catch (e) {
    outputs.success = false
    outputs.error_message = e.message
    // Flow Designer will catch this and route to error handler
    throw new Error("Query failed: " + e.message)
  }
})(inputs, outputs)

REST Action 示例

配置
Action: Call External API
Connection: My REST Connection Alias
HTTP Method: POST
Endpoint: /api/v1/tickets

Headers:
  Content-Type: application/json
  Authorization: Bearer ${connection.credential.token}

Request Body:
{
  "title": "${inputs.short_description}",
  "priority": "${inputs.priority}",
  "reporter": "${inputs.caller_email}"
}

Parse Response: JSON
响应处理
// In a Script step after REST call
var response = fd_data.action_outputs.rest_response

if (response.status_code == 201) {
  outputs.external_id = response.body.id
  outputs.success = true
} else {
  outputs.success = false
  outputs.error = response.body.error || "Unknown error"
}

Subflow 模式

通知 Subflow
Subflow: Send Notification
Inputs:
  - recipient_email (String)
  - subject (String)
  - body (String)
  - priority (String, default: "normal")

Actions:
  1. Look Up: User by email
  2. If: User found
     - Send Email notification
     - Output: success = true
  3. Else:
     - Log Warning
     - Output: success = false
审批 Subflow
Subflow: Request Approval
Inputs:
  - record_sys_id (Reference)
  - approver (Reference: sys_user)
  - approval_message (String)

Actions:
  1. Create: Approval record
  2. Wait: For approval state change
  3. If: Approved
     - Output: approved = true
  4. Else:
     - Output: approved = false
     - Output: rejection_reason = comments

Flow Designer 与 Workflow 对比

功能Flow DesignerWorkflow
界面现代化、可视化旧版
可复用性Subflow、Action有限
测试内置测试手动
版本控制支持有限
Integration Hub支持不支持
性能更好较慢
建议用于新开发仅维护现有项目

调试流程

流程上下文日志
// In Script Action
fd_log.info("Processing incident: " + inputs.incident_number)
fd_log.debug("Input data: " + JSON.stringify(inputs))
fd_log.warn("Retry attempt: " + inputs.retry_count)
fd_log.error("Failed to process: " + error.message)
流程执行历史记录
Navigate: Flow Designer > Executions
Filter by: Flow name, Status, Date range
View: Step-by-step execution details

常见模式

模式 1:SLA 升级流程
Trigger: SLA breached (Task SLA)
Actions:
  1. Get: Task details
  2. Get: Assignment group manager
  3. Send: Escalation email
  4. Update: Task priority
  5. Create: Escalation task
模式 2:审批路由
Trigger: Request Item created
Actions:
  1. If: Amount < $1000
     - Auto-approve
  2. Else If: Amount < $10000
     - Request: Manager approval
  3. Else:
     - Request: VP approval
     - Wait: 3 business days
     - If timeout: Escalate to CFO
模式 3:集成同步
Trigger: Scheduled (every 15 minutes)
Actions:
  1. Call: External API (get changes)
  2. For Each: Changed record
     a. Look Up: Matching local record
     b. If exists: Update
     c. Else: Create
  3. Log: Sync summary

性能提示

  1. 尽早使用条件 - 在执行开销较大的操作前进行筛选
  2. 限制循环 - 为 For Each 设置最大迭代次数
  3. 尽可能使用异步 - 不要因耗时操作而阻塞
  4. 缓存查询结果 - 将重复查询的结果存储在暂存变量中
  5. 批量操作 - 将类似的更新归组处理

评论

支持 Markdown