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

 

Hi,

I have resolved it after two times parsed because the console log was still showing it as a string after the first parse, and then the second time it worked.

 

thank you for your reply!

Glad that helped - please mark my solution as correct and helpful 

Hi, okay, sure, I just have another query. We are using a multirow variable set, and we have a variable, so how do we show the variable label after selecting the choice from the select box instead of the value? We are getting data from a third party that is not stored.

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