操作工作流程 是用户定义的自动工作流程。每个操作流程都包含一个操作流程触发器和一个或多个操作,该触发器可启动工作流程。
自定义代码步骤使您可以执行自定义 JavaScript,以执行复杂的数据转换、格式设置和高级逻辑操作,而这些是预定义的拖放操作和步骤无法实现的。例如,您可以使用自定义代码步骤获取信息以屏蔽电邮地址的敏感部分,验证电邮或订单 ID 的格式,甚至计算跟进提醒的未来日期。
添加和配置自定义代码步骤
如果您需要在操作流程中执行代码,可以添加自定义代码步骤。请参阅 支持的数据类型 ,了解自定义代码步骤的输入和输出支持哪些数据类型。
- 打开操作流程。
- 在操作生成器中现有步骤下方,单击 添加步骤 图标 (
)。 - 在步骤侧栏中,在 工作流程控制和实用程序下,单击 自定义代码。
- 在步骤侧栏中,单击 添加输入 , 为自定义代码添加输入。
- 在步骤侧栏中,单击 添加输出 以 添加至少一个输出。
- 在步骤侧栏中的 代码下, 输入您的自定义 JavaScript 代码。
- 单击保存。
要确保自定义代码按预期工作, 请测试您的操作流程。如果您遇到问题,请参阅 自定义代码步骤故障排除。
将输入添加到自定义代码步骤
将输出添加到自定义代码步骤
添加自定义代码
自定义代码步骤中的 JavaScript 必须导出一个函数,该函数接受 输入 对象并返回 输出 对象。每次您将自定义代码步骤添加到操作流程时,都会使用此框架进行预先填充,您可以对其进行修改、扩展,也可以删除并重新开始。
尽管自定义代码步骤旨在尽可能实现用户友好,但管理员在实施这些步骤时可能仍需要工程支持。
- 步骤代码最多可以有 10,000 个字符。
- 您的自定义代码必须为该步骤定义的每个 输出 返回一个值。
- 您在操作流程中使用的代码必须快速执行,并使用合理的内存量。
- 存在以下 JavaScript 限制:
- 该函数必须是同步的。这意味着
async,await,和promise不支持。 - 网络请求,例如
fetch,XMLHttpRequest、 和 jQuery AJAX 不受支持。 - 不可以
import或require外部库或模块。
- 该函数必须是同步的。这意味着
- 打开一个操作流程。
- 在操作生成器中,单击现有自定义代码步骤,或单击 添加步骤 图标 (
)以 添加自定义代码步骤。 - 在 代码下,根据提供的范例展开、删除范例并编写您自己的逻辑,或使用 人工智能提示 为您生成自定义代码。您的代码必须导出一个接受 输入 对象并返回 输出 对象的函数。例如:
// In the Inputs section for this step, add an input named status. // In the Outputs section for this step, add a text output named message. module.exports = (inputs) => { // Access inputs using inputs.<name> const msg = 'Your text was ' + inputs.status + '.' // This return must include all outputs. return { message: msg } }注意:在您编写代码时,编辑器会提供有效 JavaScript 操作的实用建议。拼写错误和无效代码会标有红色的下划线。将鼠标悬停在带下划线的部分上可查看错误消息。 - 单击保存。
自定义代码输入和输出支持的数据类型
自定义代码步骤接受并返回以下数据类型,每个数据类型在您的代码中显示为等效的 JavaScript 类型。
| 输入类型 | JavaScript 数据类型 | 示例 |
|---|---|---|
| 文本 | 字符串 | “您好!” |
| 数字 | 号码 | 123, -5, 0 |
| 小数 | 号码 | 12.67, 1.0, 0.0 |
| True/false | 布尔值 | true、false |
| 日期* | 字符串 | “2025-10-27” |
| 日期和时间* | 字符串 |
UTC 日期和时间 “2025-10-27T10:30:00Z” 带有时区偏移量的日期和时间: “2025-06-22T05:11:28+10:00” UTC 日期和时间(毫秒): “2025-03-08T06:01:21.415Z” 带有时区偏移量和毫秒的日期和时间:"2025-07-23T13:33:17.031-3:00" |
| 文本数组 | 字符串数组 | [“您好!”, “您好”] |
| 数字数组 | 数字数组 | [12, 34, 56, 0] |
| 小数数组 | 数字数组 | [2.1, 6.3, 6.01, 0.1] |
| True/False 数组 | 布尔值数组 | [true, true, false] |
| 日期数组 | 字符串数组 | ["1997-08-29", "2004-07-25", "2011-04-21"] |
| 日期时间数组 | 字符串数组 | ["1977-05-25T12:45Z", "1980-05-21T15:00Z", "1983-05-25T19:30Z"] |
- 对象
- 对象数组
- 混合数组
- 数组 数组
用于为操作流程步骤生成自定义 JavaScript 代码的人工智能提示模板
以下提示可与人工智能工具(例如 ChatGPT)结合使用,为您的自定义代码步骤生成代码。提示包括关于受支持的数据类型和所需的代码结构的指南。
提示 1
Write a piece of JavaScript code. The code must export a function that takes an inputs object and returns an outputs object, for example:
module.exports = (inputs) => {
let msg = "Your text was " + inputs.inputText + "."
return {
outputMessage: msg
}
}
Supported data types:
Strings, numbers, booleans, dates (YY-MM-DD) and datetimes (YY-MM-DDTHH:mm:SSZ) are supported. Arrays of those are supported too.
Objects, arrays of arrays, and arrays of objects are not supported. When used as inputs or outputs they must be stringified and passed as strings.
Check for missing or invalid data, and ensure it is handled gracefully by returning a valid output:
for strings, output the empty string ""
for numbers, output -1
for booleans, output false
for arrays, output the empty array []
for objects, output the empty object {}
Comment the code:
Include a comment at the top which briefly describes what the code does.
Include comments directing the reader to create the inputs and outputs, for example: // Make sure to create the following inputs: field_tag (Text).
In comments, refer to variable types with the following friendly names:
string: use "Date" for dates, "Date and time" for datetimes, else use "Text"
number: use "Number” or "Decimal"
boolean: use "True/False"
array: use "Number array", "Text array" etc
The code should... <DESCRIBE WHAT YOU WANT YOUR CODE TO DO HERE>
提示 2
Write a piece of JavaScript code. The code must export a function that takes an inputs object and returns an outputs object, for example:
module.exports = (inputs) => {
let msg = "Your text was " + inputs.inputText + "."
return {
outputMessage: msg
}
}
Supported input and output data types:
Strings, numbers, booleans, dates (YY-MM-DD) and datetimes (YY-MM-DDTHH:mm:SSZ) are supported. Arrays of those are supported too.
Objects, arrays of arrays, and arrays of objects are not supported. When used as inputs or outputs they must be stringified and passed as strings.
Comment the code:
Include a comment at the top which briefly describes what the code does.
Include comments directing the reader to create the inputs and outputs, for example: // Make sure to create the following inputs: field_tag (Text).
In comments, refer to variable types with the following friendly names:
string: use "Date" for dates, "Date and time" for datetimes, else use "Text"
number: use "Number" or "Decimal"
boolean: use "True/False"
array: use "Number array", "Text array" etc
The code should... <DESCRIBE WHAT YOU WANT YOUR CODE TO DO HERE>
自定义代码步骤示例
将基本工单详情设置为可读评论的格式
module.exports = (inputs) => {
// This code takes text inputs 'status' and 'priority', and a number input 'ticket_id', and outputs a friendly message combining these values.
// Make sure to create the following inputs:
// status (Text)
// priority (Text)
// ticket_id (Number)
const message = `Ticket ${inputs.ticket_id} has status ${inputs.status} and priority ${inputs.priority}.`
// Make sure to create the following outputs:
// message (Text)
return {
message,
}
}
从现在开始计算接下来的一小时
本例中,当前时间为 2025-10-27T09:17:00Z。因此,以下代码将返回 2025-10-27T10:00:00Z。
module.exports = (inputs) => {
// This code calculates the next full hour from the current UTC time
// No inputs required
// Make sure to create the following outputs:
// nextHour (Date and time)
// Create a Date object for the current time
const nextHour = new Date()
// Round up to the next hour by:
// 1. Setting minutes, seconds, and milliseconds to 0
nextHour.setUTCMinutes(0, 0, 0)
// 2. Adding 1 hour
nextHour.setUTCHours(nextHour.getUTCHours() + 1)
return {
nextHour: nextHour.toISOString(),
}
}
验证电邮地址的格式
module.exports = (inputs) => {
// This code takes a text input 'emailAddress',
// uses regex to do a basic check whether it is a valid email format,
// and outputs a boolean 'emailIsValid'.
// Make sure to create the following inputs:
// emailAddress (Text)
// Simple email regex pattern
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
const emailIsValid = emailRegex.test(inputs.email)
// Make sure to create the following outputs:
// emailIsValid (True/False)
return {
emailIsValid
}
}
屏蔽电邮地址
此示例用星号字符替换了电邮地址的一部分。例如,“您能否确认您的电邮地址是 d******n@zendesk.com?”
// This code takes an email address input and returns a masked version where the local part is partially replaced with '*', preserving the first and last characters.
// Make sure to create the following input:
// emailToMask (Text)
// Make sure to create the following output:
// maskedEmail (Text)
module.exports = (inputs) => {
const [emailName, emailDomain] = inputs.emailToMask.split("@")
let maskedEmailName = emailName
if (emailName.length > 2) {
maskedEmailName = emailName[0]
maskedEmailName += "*".repeat(emailName.length-2)
maskedEmailName += emailName[emailName.length-1]
}
return {
maskedEmail: `${maskedEmailName}@${emailDomain}`
}
}
将内部标识符映射到人类可读的名称
像这样的映射有助于将内部 ID(例如下拉工单字段的标签)转换为更加用户友好的名称(例如下拉菜单中显示的值)。
- 将Slack频道 ID 翻译为频道名称
- 将 JIRA 项目 ID 转换为项目名称
- 映射 Zendesk 工单状态到相应 JIRA 问题状态
const optionMap = {
option_01_ivory: 'ivory',
option_02_cream: 'cream',
option_03_white: 'white',
option_04_bone: 'bone',
}
module.exports = (inputs) => {
// This code takes a text input 'optionTag',
// and outputs the corresponding 'optionName' based on a predefined mapping.
// If the input is missing or does not match any tag, it returns an empty string.
// Make sure to create the following inputs:
// optionTag (Text)
const optionName = optionMap[inputs.optionTag] || ''
// Make sure to create the following outputs:
// optionName (Text)
return {
optionName,
}
}
检查列表中是否有以特定字符开头的项目
本例检查列表中是否有以特定字符开头的项目。例如,如果您正在搜索以“customer”开头的项目,并且您的工单标签列表包含 [“metric_bug”、“customer_24601”、“escalation”],则结果将为 true 因为“customer_24601”以“customer”开头。
module.exports = (inputs) => {
// This code takes a text array 'tags' and a text string 'searchTerm',
// and outputs a boolean 'matchFound' indicating whether any tag begins with the searchTerm.
// Make sure to create the following inputs:
// tags (Text array)
// searchTerm (Text)
const matchFound = inputs.tags.some(tag => tag.startsWith(inputs.searchTerm))
// Make sure to create the following outputs:
// matchFound (True/False)
return {
matchFound
}
比较两个值列表
此示例比较两个列表,并移除第一个列表中第二个列表中存在的所有值。
例如,给定一个工单抄送列表和一个被禁用用户列表,在抄送列表中找到的任何被禁用用户都将从输出列表中排除(cleanCCs)。此方法还可用于筛选掉应从工单或用户中移除的标签。
module.exports = (inputs) => {
// This code takes two number arrays: ticketCCs and disallowedUsers,
// and outputs a number array cleanCCs which includes only those items from ticketCCs that are NOT in disallowedUsers.
// Make sure to create the following inputs:
// ticketCCs (Number array)
// disallowedUsers (Number array)
const cleanCCs = inputs.ticketCCs.filter(cc => !inputs.disallowedUsers.includes(cc))
// Make sure to create the following outputs:
// cleanCCs (Number array)
return {
cleanCCs,
}
}
在文本中查找订单 ID
此示例从一串文本(例如工单评论)中提取订单 ID。您可以调整正则表达式(/GO-\d{8,9}/),以匹配特定的订单 ID 格式。
// Regex for order number: "GO-" followed by 8 or 9 digits
const ORDER_NUMBER_REGEX = /GO-\d{8,9}/
// Searches comment for order number and returns it if found.
const findOrderNumber = comment => comment.match(ORDER_NUMBER_REGEX)[0]
module.exports = (inputs) => {
// This code uses regex to look for an orderNumber with pattern "GO-" followed by 8 or 9 digits
// Make sure to create the following inputs:
// commentBody (Text)
const orderNumber = findOrderNumber(inputs.commentBody)
// Make sure to create the following outputs:
// orderNumber (Text)
// matchFound (True/False)
return {
orderNumber,
matchFound: Boolean(orderNumber),
}
}
生成随机数
本例生成一个在您指定的范围内的随机数。您可以更改该值 52 以设置所需的随机数上限。
module.exports = (inputs) => {
// This code outputs a random integer number between 1 and 52 inclusive.
// No inputs required.
// Make sure to create the following outputs:
// randomNumber (Number)
const randomNumber = Math.floor(Math.random() * 52) + 1
return {
randomNumber
}
}
生成 1 到 7 之间的三个随机数
module.exports = (inputs) => {
// This code outputs three unique random integer numbers between 1 and 7 inclusive.
// No inputs required.
// Make sure to create the following outputs:
// randomNumber1 (Number)
// randomNumber2 (Number)
// randomNumber3 (Number)
const numbers = []
while (numbers.length < 3) {
const candidate = Math.floor(Math.random() * 7) + 1
if (!numbers.includes(candidate)) {
numbers.push(candidate)
}
}
return {
randomNumber1: numbers[0],
randomNumber2: numbers[1],
randomNumber3: numbers[2]
}
}
module.exports = (inputs) => {
// This code outputs an array of three unique random numbers between 1 and 7 inclusive.
// No inputs required.
// Make sure to create the following outputs:
// randomNumbers (Number array)
const numbers = []
while (numbers.length < 3) {
const candidate = Math.floor(Math.random() * 7) + 1
if (!numbers.includes(candidate)) {
numbers.push(candidate)
}
}
return {
randomNumbers: numbers
}
}
翻译免责声明:本文章使用自动翻译软件翻译,以便您了解基本内容。 我们已采取合理措施提供准确翻译,但不保证翻译准确性
如对翻译准确性有任何疑问,请以文章的英语版本为准。