How to pass workspace page sys from client script to scripted rest api

JohnDF
Mega Sage

Hi everyone,

I'am working with the ui builder and try to pass the current page sysid from the workspace page to a scripted rest api. The point why I try to pass it from the client script part in ui builder to a scripted rest api is I want to start a subflow via the scripted rest api.

Here is the code how I get the current sysid and how I call the scripted rest api. But I dont know how to pass ithe value to the script:

/**
* @param {params} params
* @param {api} params.api
* @param {any} params.event
* @param {any} params.imports
* @param {ApiHelpers} params.helpers
*/
function handler({api, event, helpers, imports}) {
    helpers.snHttp('/api/770697/retiresoftwareasset', {method: 'GET'})
    .then(({response}) => {
      // do something with the table data
      var softwaremodelId = api.context.props.sysId;
      var softwaremodelId2 = api.context.props.treeElement;
    })
    .catch(({error}) => {
      const message = `Error: ${error.data.error.message}`;
      console.error(message);
      api.emit('NOW_UXF_PAGE#ADD_NOTIFICATIONS', {
        id: 'alert5',
        status: 'high',
        icon: 'info-circle-outline',
        content: message,
        action: { type: 'dismiss' }
    });
  });
}

Here is the image of the ui builder.

find_real_file.png

Maybe there is a smarter way to achieve my use case. I wanna push the button on the workspace page and want for this example retire a software model. So When I click the button a flow should be triggered with the current sysID to set the state to retire. This is just a simple example. I want to trigger flows with the current sysid as input when I click on buttons in the workspace.

 

Maybe any input? Thanks for your help in advance.

1 ACCEPTED SOLUTION

Marc Mouries
ServiceNow Employee
ServiceNow Employee

In your Scripted REST API, you need to specify a relative path that will contain one or several parameters enclosed between "{ }".

In your case, I'd rename your REST Scripted API

- API Name: software asset

- then create a resource with a relative path : "/retire{sys_id}"

Next in the Scripted REST API code, retrieve the sys_id with 

var input = request.pathParams.input;
varsys_id = request.pathParams.sys_id;

then pass the sys_id to the flow

If you get stuck I highly recommend the training: Scripted REST APIs

Hit the 👍 Helpful link ↓ if this brings you closer to achieving your goal

 

View solution in original post

6 REPLIES 6

Marc Mouries
ServiceNow Employee
ServiceNow Employee

The easiest is to create a scripted REST API that will call your subflow. 

Your scripted REST API will have,for example, this path

 /api/my_API/v1/retiresoftwareasset/{sys_id}

and the implementation of the scripted api, you can use the code snippet provided by flow designer

Then in your script, you build the URL such as this

URL = "/api/my_API/v1/retiresoftwareasset/" + your_sys_id

and then call 

helpers.snHttp(URL, {method: 'GET'})

 

Hi @Marc Mouries thanks for reply.

what need I to change? You see my code how I call the scripted rest api and how I get the SysID from the object in the ui builder. But my point is how to pass the SysID in the helpers.snHttp Method to the subflow back? That can work with the sysid in the subflow?

Thanks for your help.

Marc Mouries
ServiceNow Employee
ServiceNow Employee

In your code you first need to retrieve the sysId of the current record. I don't know your application so it's hard to be very specific. If the page has a property sysId, you can call:

var softwaremodelId = api.context.props.sysId;

 

If you need to call the workflow on the selected item on the tree, then add a page variable "softwareModelId" and sets its value in a page script triggered on the ITEM_CLICKED event of the tree component.

Once you have the correct value for the softwareModelId than pass it to the scripted rest api like i indicated above.

 

 

@Marc Mouries I mean I struggle more in the Scripted Rest API and subflow parts.

How can I configure the path like you mentioned with the sys_id.

Your scripted REST API will have,for example, this path

 /api/my_API/v1/retiresoftwareasset/{sys_id}


Here is m scripted rest api:
find_real_file.png

 

And here is the related resource with the subflow snipped:

find_real_file.png

 

How can I recieve the sysId and pass it to the subflow?

Here is the client script from the button which call the scripted rest api and hopefully pass the sysid too.

 

Thanks for your help.