Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Fetch data from an external API and display on a field when I click on the UI Action button

Ifeanyi Kingsle
Tera Contributor

 

 

Hello everyone, I need some assistance with this. I want to display data on a field when I click on the UI Action button. I have created an Action to fetch data from an external API. can someone help me get this done better.

 

screenshots:

Screenshot 2023-06-22 at 01.21.20.pngScreenshot 2023-06-22 at 01.24.55.png

 

Here is the UI Action script copied from the Action and modified a bit:

(function() {
    try {
        var inputs = {};
        inputs['date'] = current.getValue('date'); // Date 

        // Start Asynchronously: Uncomment to run in background. Code snippet will not have access to outputs.
        // sn_fd.FlowAPI.getRunner().action('x_842414_stock_exc.get_stock_info').inBackground().withInputs(inputs).run();

        // Execute Synchronously: Run in foreground. Code snippet has access to outputs.
        var result = sn_fd.FlowAPI.getRunner().action('x_842414_stock_exc.get_stock_info').inForeground().withInputs(inputs).run();
        var outputs = result.getOutputs();

        // Get Outputs:
        // Note: outputs can only be retrieved when executing synchronously.
        var stock_summary = outputs['stock_summary']; // Object
        gs.info(stock_summary + 'line 17');
        // console.log(stock_summary + 'line 18');
        current.setValue('stock_summary', stock_summary);
    } catch (ex) {
        var message = ex.getMessage();
        gs.error(message);
    }
})();

 

I would appreciate the assistance,

Thank you.

 

1 REPLY 1

Quinten
Tera Guru

Hello @Ifeanyi Kingsle 

I do understand the requirement, but this won't work. The code you copied is server code and should not be used from a client UI action. if the purpose is to fetch it from the client, then I would recommend you using a display business rule on the table, call the script and store the result in the scratchpad.

 

Then the only thing you need in the UI action is a small code that fetches the data from the scratchpad object and populates it into the field.

 

Hope this helps you any further.