scripts:sample

Script samples

Copy a value from one field to another

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;
  });

Hide a field

Add the name of the field you want to hide to the placeholder in the script below.

form.onChange([], true)
  .setVisibility(['Field name'], state => false);

Write a value from a catalog column to a form field

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'];
  });

Make sure the end date isn’t before the start date

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;
  });

Make sure at least one field is filled out

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;
  });

Search for duplicate entries in the register

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;
  });

Deadline extension by priority

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};
  });

Automatically change task status after receiving a reply

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'};
  });

Autocomplete service level agreement resolution time

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.
' : return
64The deadline in hours.
; case '
MediumThe value from the Priority catalog column.
': return
32Срок в часах
; case '
HighThe value from the Priority catalog column.
': return
16The 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.
' : return
32The deadline in hours.
; case '
MediumThe value from the Priority catalog column.
': return
16Срок в часах
; case '
HighThe value from the Priority catalog column.
': return
8The 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.
' : return
16The deadline in hours.
; case '
MediumThe value from the Priority catalog column.
': return
8Срок в часах
; case '
HighThe value from the Priority catalog column.
': return
4The deadline in hours.
; } default: return undefined; } }

Hide a form field if an email doesn’t come not from a designated address

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";
  });

Filter a catalog

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 });

Was this article helpful?

Yes, thanks! No, I have a question