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.

How to parse response and populate as choice on form

Deepthi13
Tera Expert

HI tea, I am trying script include and client script  to parse the response from rest message and populate the values as choices on form. but i am getting java script console error and not working as expected please suggest.

 salesOrgs.forEach(function(org) {
            choices.push({ label: org.SalesOrganization, value: org.SalesOrganization });
        });
        return JSON.stringify(choices); // Return choices as a JSON string
    },
 
2 REPLIES 2

Sid_Takali
Kilo Patron

Hi @Deepthi13 Try below code

Script Include:

var GettingSAPResponse = Class.create();
GettingSAPResponse.prototype = {
    initialize: function() {},
 
    getSalesOrganizations: function() {
        var request = new sn_ws.RESTMessageV2("SAP_Integration");
        request.setHttpMethod('get');
        var response = request.execute();
        gs.log("@@@ " + response.getBody());
       
        var httpResponseStatus = response.getStatusCode();
        if (httpResponseStatus != 200) {
            gs.error('Failed to get data from SAP: HTTP Status ' + httpResponseStatus);
            return [];
        }
        var responseBody = response.getBody();
        var responseObject = {};
        try {
            responseObject = JSON.parse(responseBody);
        } catch (e) {
            gs.error('Failed to parse response: ' + e.message);
            return [];
        }
        if (responseObject.error) {
            gs.error('Error in SAP response: ' + responseObject.error);
            return [];
        }
 
        var salesOrgs = responseObject.SalesOrganization || [];
        var choices = [];
        salesOrgs.forEach(function(org) {
            choices.push({ label: org.name, value: org.id }); 
        });

        return choices;
    },
 
    type: 'GettingSAPResponse'
};

 

Client Script: 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var catalogChoices = new GettingSAPResponse();
    var ga = new GlideAjax('GettingSAPResponse');
    ga.addParam('sysparm_name', 'getSalesOrganizations');
    
    ga.getXMLAnswer(function(response) {
        var salesOrgs = JSON.parse(response);
       
        var fieldName = 'salesorganization'; 
        var field = g_form.getControl(fieldName);
        if (field) {
            g_form.clearOptions(fieldName);
            salesOrgs.forEach(function(choice) {
                g_form.addOption(fieldName, choice.value, choice.label);
            });
        }
    });
}

 

Deepthi13
Tera Expert
var OMNIUtils_Client = Class.create();
OMNIUtils_Client.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getSales: function() {
        gs.log("@@@1 ");
        var request = new sn_ws.RESTMessageV2('OMNIapi', 'SalesAreaAssignments');
        var response = request.execute();
        var js = response.getBody();
        var acc = [];
        // Parse the JSON response
        var salesData = JSON.parse(js);
        for (var i = 0; i < salesData.length; i++) {
            // Format the response as "value,label"
            acc.push(salesData[i].SalesOrg + "," + salesData[i].SalesOrgName);
        }
        return acc.join(';');
    },

    getDistributionChannel: function() {
        gs.log("@@@2 ");
        var request = new sn_ws.RESTMessageV2('OMNIapi', 'SalesAreaAssignments');
        var response = request.execute();
        var js = response.getBody();
        var acc = [];
        // Parse the JSON response
        var distributionData = JSON.parse(js);
        for (var i = 0; i < distributionData.length; i++) {
            // Format the response as "value,label"
            acc.push(distributionData[i].DChannel + "," + distributionData[i].DChannelName);
        }
        return acc.join(';');
    },
       type: 'OMNIUtils_Client'
});