Getting 3rd party data and showing in catalog variable

imkhan
Tera Contributor

Hi everyone,

 

Can someone please help me with where the issue is while parsing the variable data? we need to show data in variable

Created a script include from which to get third-party data and show it in the catalog variable.

 

{"result":{"catalog_item":"3a25637b47701100ba13a5554ee490a0","variable_name":"locationdata","choices":[{"label":"mumbai","value":"3"},{"label":"delhi","value":"2"},{"label":"Passing","value":"1"}]}}

 

script include:

var CatalogDataFetcher = Class.create();
CatalogDataFetcher.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getThirdPartyData: function() {
        try {
            var restMessage = new sn_ws.RESTMessageV2('catlog varaible data get', 'get the catalog varaible data');
            var response = restMessage.execute();
            var httpStatus = response.getStatusCode();
            var responseBody = response.getBody();

            if (httpStatus == 200 ) {
                  // Parse the response (assuming JSON)
                var data = JSON.parse(responseBody);
                return data; // Return the data to the client script
            } else {
                gs.error('Failed to fetch data from API. Status: ' + httpStatus);
                return null;
            }
        } catch (ex) {
            gs.error('Error in FetchDataFromThirdParty: ' + ex.getMessage());
            return null;
        }
    },

    type: 'CatalogDataFetcher'
});
 
client script:
function onLoad() {

    var ga = new GlideAjax('CatalogDataFetcher');
    ga.addParam('sysparm_name', 'getThirdPartyData');
    ga.getXMLAnswer(ajaxResponse);

    // Function to process the response
    function ajaxResponse(response) {
 
           
            alert('API Response: ' + response);

            // Parse the JSON response 
            var data = JSON.parse(response);
      
            alert('Parsed Data: ' + JSON.stringify(data));

            // Check if data 
            if (data && data.length > 0) {
              
                alert('Data is valid and not empty');

                var options = '';
                for (var i = 0; i < data.length; i++) {
                    // Customize this based on your API response structure
                    options += '<option value="' + data[i].value + '">' + data[i].label + '</option>';
                }

              
                alert('Options: ' + options);

                // Populate the variable 
                g_form.setOptions('locationdata', options);
                
                alert('Dropdown populated successfully');
            }
        }
    }
------
Result:
API Response: [object Object]
 
 
1 ACCEPTED SOLUTION

You need to store that data as choices on the variable, or use a remote table if you need both the value and label continuously 

View solution in original post

13 REPLIES 13

Kieran Anson
Kilo Patron

Hi,

When you return data via GlideAjax, you need to return it as a string rather than a parsed object. 

return responseBody
or
return JSON.stringify(responseBody)

 

Hi Kieran,

Thanks, I can see now response is coming on client side, and now the options are showing undefined on alert.

 

 

imkhan_1-1739467439818.png

 

Does the following line produce a value with the expected data?

alert('Parsed Data: ' + JSON.stringify(data)); produce a value?

 

You would need to use JSON.parse(data) to turn that into an accessible object.

 

You also don't need to do the following, that is if you were writing html option elements, but the g_form API does this for you

                    options += '<option value="' + data[i].value + '">' + data[i].label + '</option>';
 
instead you would simply
g_form.addOption('field' , 'option value, 'option display value');

Yes, the alert alert ('Parsed Data: ' + JSON.stringify(data)) producing a value and can see the data is coming successfully, but after adding array the to add label and value which is not working, and data is not showing on alert (!data.result).

imkhan_0-1739604619205.png