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

Server scripts

Generic implicit script

Use this generic template to cover 90% of use-cases.

Generic implicit script:
export default {
  label: "label goes here",
  description: "description goes here",

  // Use the ones you need, delete the rest
  triggers ({ before, after, on, at }) {
    return before('event goes here')
      .where('constraint goes here')
      // Add/remove constraints here
  },

  // remove async if you aren't doing any async operations
  // use object destructuring for args and ctx
  async exec(args, ctx) {
    // Code goes here
  },
}

Before saving a record

Example use-cases:
  • calculate complex fields

  • value validation

  • create a changelog entry

It would be best if you used field expressions for most field validations and value calculations.

DevNote: provide some references.

Before saving a record:
export default {
  label: "label goes here",
  description: "description goes here",

  triggers ({ before }) {
    return before('create', 'update')
      .for('compose:record')
      .where('module', 'module goes here')
      .where('namespace', 'namespace goes here')
  },

  // If you don't need the Compose helper, remove it
  async exec ({ $record }, { Compose }) {
    // Code goes here

    return $record
  },
}

After saving a record

Example use-cases:
  • notify the owner about the change

  • send the change to an external integration

  • create a changelog entry

If you want to change the value, you should use Before saving a record instead.

After saving a record:
export default {
  label: "label goes here",
  description: "description goes here",

  triggers ({ after }) {
    return after('create', 'update')
      .for('compose:record')
      .where('module', 'module goes here')
      .where('namespace', 'namespace goes here')
  },

  // If you don't need the Compose helper, remove it
  async exec ({ $record }, { Compose }) {
    // Code goes here
    // Note: the return value is ignored
  },
}

Generic on click action

Example use-cases:
  • OAuth flows

  • send notifications

  • render documents

Generic on click action:
export default {
  label: "label goes here",
  description: "description goes here",

  triggers ({ on }) {
    return on('manual')
      // vv Don't remove the next line vv
      .uiProp('app', 'compose')
  },

  // If you don't need the Compose helper, remove it
  async exec (args, { Compose }) {
    // Code goes here
  },
}

Process record on click

Example use-cases:
  • email the record contact

  • email the record owner

  • generate report for the specific record

Process record on click:
export default {
  label: "label goes here",
  description: "description goes here",

  triggers ({ on }) {
    return on('manual')
      .for('compose:record')
      .where('module', 'module goes here')
      .where('namespace', 'namespace goes here')
      // vv Don't remove the next line vv
      .uiProp('app', 'compose')
  },

  // If you don't need the Compose helper, remove it
  async exec ({ $record }, { Compose }) {
    // Code goes here
    // Note: unless false or an error, the return value is ignored
  },
}

Respond to an HTTP request

Example use-cases:
  • creating records from an external form

  • document approval/signature tracking

  • credit card payment capturing

You will need to add the base-64 package.

Respond to an HTTP request:
// Don't forget to add this package
import base64 from 'base-64'

export default {
  label: "label goes here",
  description: "description goes here",

  security: {
    runAs: 'user goes here',
  },

  triggers ({ on }) {
    return on('request')
      .where('request.path', '/path')
      .where('request.method', 'POST')
      .for('system:sink')
  },

  async exec ({ $request, $response }) {
    // The body is base64 encoded.
    // If we're working with json content, don't forget the JSON.parse
    const body = JSON.parse(base64.decode($request.rawBody))

    // Do something with the data...
    // ...

    // Prepare the response
    $response.status = 200
    $response.header = { 'Content-Type': ['application/json'] }
    $response.body = JSON.stringify({ result: 'example' })

    // and send back everything
    return $response
  }
}