Script Action

The <action-script>, introduced in v5, can be used to create complex actions using scripting languages.

  name="create.invoice" (1)
  model="" (2)
    language="js" (3)
    transactional="true" (4)
  var req = $request; (5)
  var res = $response; (6)
  var so = req.context;
  var invoice = new Invoice(); = so.confirmDate;
  // prepare invoice lines from sale order
  //TODO: invoice.invoiceLines = listOf(...);

  // if you want to save invoice
  //invoice.saleOrder = em.find(Order.class,;
  //return $em.persist(invoice);

  res.setValue('invoice', invoice);
  res.setReadonly('customer', true);
  // and so on...
1 the name of the action (required)
2 the name of the context model
3 scripting language to use (required, currently js and groovy only)
4 whether the code is transactional
5 the ActionRequest is available as $request
6 the ActionResponse is available as $response

The action-script is nothing but the controller method dynamically created using a scripting language. The $request and $response variables are nothing but the ActionRequest and ActionResponse parameters of controller method.

Following variables are available in script execution context:

Name Description


the ActionRequest


the ActionReponse


the EntityManager if script is transactional


instance of MetaJsonRecordRepository to work with custom models

The action-script can be used for custom models too. Here is an example:

<action-script name="create.hello" model="com.axelor.meta.db.MetaJsonRecord">
  <script language="js" transactional="true">
    var hello = $json.create('hello'); (1) = "Hello!!!";           (2)

    var world = $json.all('world').by('name', '=', 'World!!!').fetchOne(); (3)
    if (world == null) {
        world = $json.create('world'); = "World!!!";
        world = $; (4)
        // now we can't update world, as it's converted to real instance
    } = world;  (5)

    // return as response values
    $response.values = hello;

1 create a new empty context for MetaJsonRecord for the hello model
2 the context allows seamless access to custom field values
3 find a world model record by field name
4 record(s) intended for relational field values must be saved manually
5 set relational value (m2o)