You are reading the documentation for an outdated Corteza release. 2024.9 is the latest stable Corteza release.

Client scripts

Automation scripts allow you to implement custom business logic that is required for your needs. Client scripts are executed in the client’s browser. Refer to integrator guide/extensions for more details.

Prompt notification

This example prompts the user to enter a value and then displays it as a notification.

client-scripts/compose/crm/Contact/CollectValue.js
export default {
  label: "Script label",
  description: 'Script description',

  * triggers ({ on }) {
    // This script myst be invoked manually (explicitly)
    yield on('manual')
      // for a record
      .for('compose:record')
      // if the record belongs to the Quote module
      .where('module', 'Contact')
      // and the module belongs to the crm namespace -- this is the slug
      .where('namespace', 'crm')
      // visible in the compose application
      .uiProp('app', 'compose')
  },

  // Refer to the integrator guide for details on these two parameters
  async exec ({ $record }, { Compose, ComposeUI }) {

    const value = window.prompt('Please insert a value')
    if (!value) {
      ComposeUI.warning('No value provided')
      return false
    }

    // Do something with the inserted value
    // ...

    ComposeUI.success(`Hi! You've entered ${value}!`)
  }
}

Prefill values

This example prefills some record values in case they are not provided.

This can also be done with the module field default value setting.

client-scripts/compose/crm/Contact/Prefill.js
export default {
  label: "Script label",
  description: 'Script description',

  * triggers ({ before }) {
    // This script myst be invoked manually (explicitly)
    yield before('formSubmit')
      // for a record
      .for('compose:record')
      // if the record belongs to the Request module
      .where('module', 'Request')
      // and the module belongs to the crm namespace -- this is the slug
      .where('namespace', 'crm')
  },

  // Refer to the integrator guide for details on these two parameters
  async exec ({ $record, $module }, { Compose }) {
    // Lets get the defaults from a Default module.
    // This allows some more flexibility
    const defaults = await Compose.findFirstRecord('Defaults')

    for (const k in $record.values) {
      if (!$record.values[k]) {
        $record.values[k] = defaults.values[k]
      }
    }

    // IMPORTANT: client-scripts work with references, so you don't need to
    // explicitly return the $record -- this is already applied when we
    // assigned a new value above.
    return $record
  }
}