Corredor automation: How to update record onInterval?

I m playin around with corredor server.

I have a workflow with an interval trigger which is calling a server script.
I am able to push values to the script and i m able to return values.

But i m not able to use compose to update the values directly in my server script.

Server Script:

//import axios from ‘axios’
//import { apiClients } from ‘@cortezaproject/corteza-js’

export default {
label: “Workflow Test”,
description: “Hey”,

triggers ({ on }) {
/**
* Due to how the Corredor scripting system is designed right now,
* triggers still need to be defined (even if the script is
* executed explicitly from the workflow).
*/
return on(‘manual’)
.for(‘system’)
},

security: {
runAs: ‘bot’
},

async exec (args, { Compose, System }) {
// unpack arguments
const { webID, workflow, webseite } = args
// webRecord = await Compose.findRecordByID(webID, ‘Server’)
// await Compose.saveRecord(webseite)

 return webseite

}
}

Debuged compose looks like:
ComposeHelper {
ComposeAPI: Compose {
headers: { ‘Content-Type’: ‘application/json’ },
baseURL: ‘http://corteza/compose’,
accessTokenFn: [Function: accessTokenFn]
},
‘$namespace’: undefined,
‘$module’: undefined,
‘$record’: undefined
}

Compose has no context which is ok, cause there is no record context.
So I d like to set namespace, module and use compose or read a record by id but i m not getting this to work using onInterval to trigger the script.

Any working example for this?
How would i use compose to get a record by ID from within the above server script?

If the script doesn’t have a context you can either define it to the Compose helper, or you can pass the values to the helper methods.

To manually define the context you could:

const namespace = await Compose.findNamespaceBySlug('the_namespace')
const module = await Compose.findModuleByHandle('the_module', namespace)

Compose.$namespace = namespace
Compose.$module = module

If you prefer the second option, you could do something like this:

const namespace = await Compose.resolveNamespace('the_namespace')
const membersModule = await Compose.findModuleByHandle('the_module', namespace)
const rsp = await Compose.findRecords('', membersModule)

On the subject of intervals; automation scripts can also be executed in intervals – I see that the docs are a bit lacking there; see an example for the trigger below:

export default {
  label: 'Some interval',
  security: {
    runAs: 'my.user@email.tld'
  },

  triggers (t) {
    return [
      t
        .on('interval')
        .every('0 2 * * *')
    ]
  },

  // ...
}

The syntax for the interval is exactly the same as the one for workflows; you can use this tool to help out.

Thx tjerman,

to be fair - i shortend my example, i already tried it that way. :wink: I found the old scripts on github and though i could do it that way but it does not work.

If i try to

const await Compose.findNamespaceBySlug(‘the_namespace’)

or

const namespace = await Compose.resolveNamespace(‘the_namespace’)

i get an error (with my own namespaces but also with the existing ones: crm,
):

workflow 263314013868261378 step 4 execution failed: Cannot read property ‘set’ of undefined

Does the user you’ve defined under the runAs have sufficient permissions to access the resources in question? The easiest way to see if that’s the case is to try it with a super administrator user.

Yes - i created a new user which is member of super administrator.
I just verified this.

I’ve found and addressed the issue. Should be available in 2021.9.x and 2022.3.x version branches soon and in one of the dev or patch releases a bit later

2 Likes