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

Server scripts

Automation scripts allow you to implement custom business logic that is required for your needs. Server scripts are executed in the Corteza Corredor. Refer to integrator guide/extensions for more details.

Since they are executed on the server, you can’t manipulate the user interface. Use client scripts if this is needed.

Send an email to the contact

The example script sends an email to the contact it was invoked for.

Make sure that your SMTP configuration is working.

server-scripts/Contact/SendMail.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 }) {
    let emailContent
    let emailSubject

    // Determine the email content and subject.
    // You could also do other bits inhere

    if (!$record.values.Email) {
      // This will stop the script's execution
      return false
    }

    await Compose.sendMail(
      $record.values.Email,
      emailSubject,
      { html: emailContent }
    )
  }
}

Notify owner about the update

The example fetches the lead owner and sends them an email.

Make sure that your SMTP configuration is working.

server-scripts/Lead/NotifyChange.js
export default {
  label: "Script label",
  description: 'Script description',

  * triggers ({ after }) {
    // This script myst be invoked after the record is updated (implicitly)
    yield after('update')
      // for a record
      .for('compose:record')
      // if the record belongs to the Lead module
      .where('module', 'Lead')
      // 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 }, { Compose, System }) {
    let emailContent
    let emailSubject

    if (!$record.ownedBy) {
      // This will stop the script's execution
      return false
    }

    // Lets get the owner
    const owner = await System.findUserByID($record.ownedBy)

    // Determine the email content and subject.
    // You could also do other bits inhere

    await Compose.sendMail(
      owner.email,
      emailSubject,
      { html: emailContent }
    )
  }
}

Calculate the lead cost

The example calculates the lead cost when it is created or updated.

server-scripts/Lead/UpdateCost.js
export default {
  label: "Script label",
  description: 'Script description',

  * triggers ({ before }) {
    // This script myst be invoked before the record is created or updated (implicitly)
    yield before('create', 'update')
      // for a record
      .for('compose:record')
      // if the record belongs to the Lead module
      .where('module', 'Lead')
      // 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 }, { Compose }) {
    if (!$record.values.LeadSource) {
      // This will use the original record, since no changes are required.
      // You could also use return false to stop the execution
      return $record
    }

    switch ($record.values.LeadSource) {
      case 'source-a':
        $record.values.LeadCost = 10
        break

      case 'source-b':
        $record.values.LeadCost = 20
        break

      default:
        $record.values.LeadCost = 30
        break
    }

    // Returning $record in a before script will automatically update the record.
    // IMPORTANT: This is not the same for after scripts -- they need to be explicitly updated.
    return $record
  }
}