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 

You need any clarification from me? Need your support. Thanks
 
 

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