Rest API Integration: Validation of Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2024 07:33 AM
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.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2024 08:17 PM
Hi,
Use GlideAjax in the client script to get the API response from script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2024 08:23 PM
Hii
Instead of creating object directly in client script i.e new RESTAPIService();
use GlideAjax();
i.e = new GlideAjax('RESTAPIService')