This script copies the value from “Source field” to “Text field”.
form.onChange(['Source field'], true) .setValue('Text field', state => { const [value] = state.changes; return value.text; });
In this example, the script adds up rental cost and deposit, then applies a 7% discount and displays this sum.
form.onChange(['Rental price', 'Deposit']) .setValues(['Discount', 'Total'], state => { const [price, deposit] = state.changes; const discount = price.value * 0.07; const total = price.value + deposit.value - discount; return [discount, total]; });
In this example, the 'Discount' field is hidden if the value in the 'Quantity' field is up to 99.
form.onChange(['Quantity'], true) .setVisibility(['Discount'], state => { const num = state.changes[0].value; return num > 99; });
In the example below, the Client field will be hidden from the second step of the workflow.
form.onChange([''], true) .setVisibility (['Client'], state => { return state.currentStep < 2; });
Here “Catalog” is the name of the form field with a catalog, “Column” is the name of a catalog column, and “Text” is the **Text** type form field where the value is stored.
form.onChange(['Catalog']) .setValue('Text', state => { const [item] = state.changes; return item.columns['Column']; });
form.onChange(['Start date', 'End date']) .validate('End date', state => { const [start, end] = state.changes; if (start.date && end.date && start.date >= end.date) return { errorMessage: 'The end date cannot be before the start date.' }; return null; });
form.onChange(['Email', 'Phone'], true) .validate('Email', state => { const [email, phone] = state.changes; if (!email.text && !phone.text) return { errorMessage: 'Add your email or phone number.' }; return null; });
Suppose there is a form with the fields “Year”, “Number”, and “Document”. Using this script, you can send a request to the register and check if the document has already been published.
form.onChange(['Year', 'Number']) .validateAsync('Number', async state => { const [year, num] = state.changes; if (year.text && num.text) { const duplicates = await form.fetchSelfRegister(f => f.fieldEquals('Year', year).fieldEquals('Number', num), [] ); const firstDuplicate = duplicates.tasks[0]; if (firstDuplicate) return {errorMessage: `Order <a href="#id${firstDuplicate.task_id}">has already been published.</a>`}; } return null; });
This script lets you change a task's due date based on its priority, or set a custom due date.
form.onChange(['High priority', 'Date shifting']) .setValue('Due date', state => { const [checkmark, customDue] = state.changes; if (customDue.date) return {date: customDue.date}; if (checkmark.checked) return {days_from_create: 1}; return {days_from_create: 3}; });
const orgId = 111111; form.onChange([], true) .setVisibility(['Open / Done'], state => false ); form.onChange([], true) .setStatus(state => { if (state.commenter.organization_id !== orgId) return {choice_name: 'In progress'}; }); form.onChange(['Responsible']) .setStatus(state => { const [currVal] = state.changes; const [prevVal] = state.prev; if (!prevVal && currVal) return {choice_name: 'In progress'}; });
You can specify resolution time and field names in the script using specific numbers,, variables, or formulas to calculate the resolution time. When the Priority and Client form fields are changed, the script will determine one of the designated due dates.
form.onChange(['PriorityThe name of a Catalog field in the service ticket form.', 'ClientThe name of a Catalog field in the service ticket form.']) .setValue('Due dateThe name of a Date field in the service ticket form.', state => { const [priority, client] = state.changes; if (!priority || !client) return undefined; const tariff = client.columns['Rate planThe Client catalog column of service rates.']; const priorityValue = priority.columns['PriorityThe name of a column in the Priority catalog.']; const hours = getSlaHoursByTariffAndPriority(tariff, priorityValue); if (hours === undefined) return undefined; return {hours_from_create: hours}; }); function getSlaHoursByTariffAndPriority(tariff, priority) { switch (tariff) { case 'BasicThe name of a rate plan from the Client catalog column.': switch (priority) { case 'LowThe value from the Priority catalog column.' : return64The deadline in hours.; case 'MediumThe value from the Priority catalog column.': return32Срок в часах; case 'HighThe value from the Priority catalog column.': return16The deadline in hours.; } case 'StandardThe name of a rate plan from the Client catalog column.': switch (priority) { case 'LowThe value from the Priority catalog column.' : return32The deadline in hours.; case 'MediumThe value from the Priority catalog column.': return16Срок в часах; case 'HighThe value from the Priority catalog column.': return8The deadline in hours.; } case 'PremiumThe name of a rate plan from the Client catalog column.': switch (priority) { case 'LowThe value from the Priority catalog column.' : return16The deadline in hours.; case 'MediumThe value from the Priority catalog column.': return8Срок в часах; case 'HighThe value from the Priority catalog column.': return4The deadline in hours.; } default: return undefined; } }
The script identifies the sender’s email address, and if a message is not from mail@example.com, the Category field will be hidden.
const emailPattern = /([a-z0-9-._]+)\@([a-z0-9-._]+)\.([a-z]{2,3})/ig; form.onChange(['Subject'], true) .setValue('Category', state => { const [currVal] = state.changes; const emails = (currVal.text || '').match(emailPattern); const first = emails ? emails[0] : ''; return first; }); form.onChange(['Email'], true) .setVisibility(['Category'], state => { const [choice] = state.changes; return choice && choice.text === "mail@example.com"; });
This script filters the list of cities by selected state. You might also use it to show the expenses related to a specific department chosen from a list of other departments.
let catalogItems = null; form.getCatalog("US states and citiesThe catalog that binds values from two other catalogs.").then(items => { catalogItems = items; }); form.onChange(["StateThe form field that defines values available for the City field."]).setFilter("CityThe form field where values change depending on the value selected in the State field.", state => { const [region] = state.changes; if (!catalogItems || !region || !region.columns) return null; const regionCol = region.columns["StateThe title of the column that defines values available in the City field."]; const filtered = catalogItems .filter(item => item.columns["StateThe title of the column that defines values available in the City field."] === regionCol) .map(item => item.columns["CityThe name of the column where potential City fields come from."]); return filtered.length > 0 ? { values: filtered } : null });