Skip to main content
Version: v5.0

Script

The Script type is a subclass of NamedUserItem that references a user-created script. Like NamedUserItems, you can identify scripts by their unique _id value, or their _userType value, which may be shared by multiple NamedUserItems.

To manage Script types in the Item Service, use the IafScripts JavaScript library or the Scripts REST API methods in the Item Service REST API.

Posting a script to the Item Service as a Script type#

If you have a JavaScript function, such as the following, you can post it as a Script type in the Item Service.


let UserGroupScripts = {  async getUserGroups(input, libraries, ctx) {    let { PlatformApi } = libraries    let proj = await PlatformApi.IafProj.getCurrent(ctx)    let res = await PlatformApi.IafProj.getUserGroups(proj, ctx)    return res  }} export default UserGroupScripts

For more information on writing script content, see Script content considerations.

Create a Script class object and pass the function as a string for the _version._userData value. The Script type is a subtype of NamedUserItem so you can use the same properties and making sure to set the _itemClass value to "Script":


let scripts = [{  _name: "get_user_group_script",  _shortName: "get_ug_script",  _description: "Script to manage user groups for a given purpose",  _userType: "user_group_scripts",  _version: {    _userData: "let UserGroupScripts = { async getUserGroups(input, libraries, ctx) { let { PlatformApi } = libraries; let proj = await PlatformApi.IafProj.getCurrent(ctx); console.log(\"project: \" + proj); let res = await PlatformApi.IafProj.getUserGroups(proj, ctx); console.log(\"In UserGroupScripts, res: \" + res); return res } } export default UserGroupScripts"  },  _namespaces: [    {{nsfilter}}  ],  _itemClass: "Script"  }];

To post the Script to the Item Service, use the createScript function from the IafScripts library:

const createdScripts = await IafScripts.createScript(scripts);

Importing scripts#

You can import scripts on the client side or on the server side. In both scenarios, use the IafScriptEngine library. For more information on script execution with the script engine, see Script executor service.

Client-side script import#

To import the script on the client-side, first import the following modules:

import * as PlatformApi from '@dtplatform/platform-api'import { IafScriptEngine } from '@dtplatform/iaf-script-engine'import * as UiUtils from '@dtplatform/ui-utils'

To import a stored script, use IafScriptEngine.dynamicImport and query by the script's identifier, such as its _id or userType property. You need to then extract the module and get its runnable scripts:


  await IafScriptEngine.dynamicImport(    { query: { _userType: 'user_group_scripts' }}, ctx).then((module) => {      let UserGroupModule = module.default      let scripts = UserGroupModule.getRunnableScripts()    }  )

Run the script with all the parameters passed in:

    let pApi = PlatformApi    let uiUtils = UiUtils    if (!UserGroupModule) {      return    }    let scriptToRun = 'UserGroupModule.' + this.state.selectedScript +       '(pApi, uiUtils, ' + JSON.stringify(ctx) + ')'    let promise = eval(scriptToRun)    promise.then(data => {      //your operations with response data    })

Server-side script import#

To call a script from the server, make sure node is aware of the ESM format rather than CJS format:


node --experimental-loader ./https-loader.mjs userGroups.mjs

Use IafScriptEngine.dynamicImport to load the script you want to use. Once it's loaded, you can start using the functions in the script:


await IafScriptEngine.dynamicImport({     query: {       _userType: 'user_group_scripts'     }  }, ctx)  .then((module) => {    const UserGroupScripts = module.default    const userGroups = await UserGroupScripts.getUserGroups(PlatformApi, ctx)  })

Running server-side scripts using an orchestrator#

To run a server-side script from an orchestrator, use the "default_script_target" target component and reference the script file name in _params.tasks._actualparams._userType and the script function in _params.tasks._actualparams._scriptName. Add the orchestrator with the IafScriptEngine.addDatasource function.


let datasourceResult = await IafScriptEngine.addDatasource(  {    _name: "orchestrator_name",    _description: "Description of your orchestrator",    _namespaces: proj._namespaces,    _userType: "your-custom-usertype",    _params: {      tasks: [        _orchcomp: "default_script_target", //target component to read script and execute          _name: "Import excel sheets",        _sequenceno: 1,        _actualparams: {          _userType: "UserGroupScripts", //the script file name          _scriptName: "getUserGroups" //the script function        }      ]    }  }, ctx)

Script considerations for Script Execution Service#

If you want to execute your script in the Script Execution Service, for example, for a script that an Orchestrator or Object Model API uses, the JavaScript syntax differs slightly from the syntax of a regular script's content. For more information, see Script content considerations.