Rest API Integration: Validation of Script

fenny patel
Tera Contributor

Hello Experts,
I'm attempting Rest API integration using script include and client script. Could someone please verify if the script is correct? Both details are provided below. Ideally, upon updating the UPC number, it should trigger a popup with the SKU number from the payload (e.g., "itemId":"42814293") and create log entries. However, nothing is currently happening. Any assistance would be greatly appreciated.
fennypatel_1-1709824492998.png

 

fennypatel_2-1709824611735.png

fennypatel_0-1709824210161.png

Client script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
  //onchange UPC number
    // Get the value of UPC field
    var upc = g_form.getValue('upc');

    // Call the script include method to get item info
    var restService = new RESTAPIService();
    var itemInfo = restService.getItemInfoByUPC(upc);

    // Parse the JSON response and set SKU field value
    var itemInfoJSON = JSON.parse(itemInfo);
    var itemId = itemInfoJSON[0].itemId;
    g_form.setValue('sku', itemId);
}

 

Script Include

var RESTAPIService = Class.create();
RESTAPIService.prototype = {
    initialize: function() {},

    // Function to make REST API call and return the response
    getItemInfoByUPC: function(upc) {
        var endpoint = "(Endpoint)" + upc;
        var response = this._sendRequest(endpoint);
		gs.info("0305 RESPONSE1: "+response);
        return response;
    },

    // Private function to send REST API request
    _sendRequest: function() {
        var request = new sn_ws.RESTMessageV2('Oracle JCD', 'GET');
        //request.setEndpoint(endpoint);
        //request.setHttpMethod("GET");
        var response = request.execute();
		gs.info("0305 RESPONSE2: "+response);
        return response.getBody();
    },

    type: 'RESTAPIService'
};

Thank you,
Fenny
 

2 REPLIES 2

Shruti
Mega Sage
Mega Sage

Hi,

Use GlideAjax in the client script to get the API response from script include

Parth_Poriya
Tera Expert

Hii 

 

Instead of creating object directly in client script    i.e new RESTAPIService();

use GlideAjax();

i.e = new GlideAjax('RESTAPIService')