Catalog client script to retrieve values from JSON

tsoct
Tera Guru

There are two cases in which the json return may or may not contain the address_lines.

//With Address Lines
{
  "national_ids":[
    {
      "id":"10406",
      "scheme":"APTBEF",
      "institution_name":"SOCIETE TUNISIENNE DE BANQUE",
      "branch_information":"BOUSALEM",
      "address":{
        "address_lines":[
          "RUE DES JARDINS",
          "16"
        ],
        "country_subdivision":"JENDOUBA GOVERNORATE",
        "post_code":"8170",
      },
      "office_type":"DB"
    }
  ]
}

//Without Address Lines
{
  "national_ids":[
    {
      "id":"3000",
      "scheme":"BSK",
      "institution_name":"SPAREBANKEN SOER",
      "address":{
        "country_code":"NO",
        "country_name":"NORWAY"
      },
      "office_type":"DB"
    }
  ]
}

 

Below is my catalog client script that is giving me an error; how can i retrieve address and postcode? which section is wrong?

  var gaFields = new GlideAjax('GetSwiftData');
    gaFields.addParam('sysparm_name', 'getDetails');
    gaFields.addParam('sysparm_countrycode', countrycode);
    gaFields.addParam('sysparm_bankId', bankId);
    gaFields.getXML(showSWIFTData3);

    function showSWIFTData3(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == 'Invalid Branch parameters') {
            //do nothing
        } else {
            var obj = JSON.parse(answer);
            alert(obj);
            g_form.setValue('postcode', obj.postcode);
            var fulladdress = "";
            if (obj.address.address_lines) {
				alert('With address line');
                for (var i = 0; i < obj.address.address_lines.length; i++) {
                    fulladdress += obj.address.address_lines[i] + ", ";
                    g_form.setValue('branch_address', fulladdress);
                }
            } else {
				alert ('no address line');
                for (var i = 0; i < obj.address.address.length; i++) {
                    fulladdress += obj.address.addres[i] + ", ";
                    g_form.setValue('branch_address', fulladdress);

                }

            }
        }

    }

 

1 ACCEPTED SOLUTION

Ajay61
Kilo Sage

Hi @tsoct , Try using below updated code.

var gaFields = new GlideAjax('GetSwiftData');
    gaFields.addParam('sysparm_name', 'getDetails');
    gaFields.addParam('sysparm_countrycode', countrycode);
    gaFields.addParam('sysparm_bankId', bankId);
    gaFields.getXML(showSWIFTData3);

    function showSWIFTData3(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == 'Invalid Branch parameters') {
            //do nothing
        } else {
            var obj = JSON.parse(answer);
            alert(obj);
            var postCode = obj.national_ids[0].id;
            var fulladdress= obj.national_ids[0].address.address_lines;
            g_form.setValue('postcode', postCode);
            g_form.setValue('branch_address', fulladdress);
        }

    }


Hope this works for you.

View solution in original post

3 REPLIES 3

Ajay61
Kilo Sage

Hi @tsoct , Try using below updated code.

var gaFields = new GlideAjax('GetSwiftData');
    gaFields.addParam('sysparm_name', 'getDetails');
    gaFields.addParam('sysparm_countrycode', countrycode);
    gaFields.addParam('sysparm_bankId', bankId);
    gaFields.getXML(showSWIFTData3);

    function showSWIFTData3(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == 'Invalid Branch parameters') {
            //do nothing
        } else {
            var obj = JSON.parse(answer);
            alert(obj);
            var postCode = obj.national_ids[0].id;
            var fulladdress= obj.national_ids[0].address.address_lines;
            g_form.setValue('postcode', postCode);
            g_form.setValue('branch_address', fulladdress);
        }

    }


Hope this works for you.

Thanks @Ajay61 ,

But I am still getting javascript error and the catalog client script allso alerting:

 

New client-scripts are run in strict mode, with direct DOM access disabled. Access to jQuery, prototype and the window object are likewise disabled. To disable this on a per-script basis, configure this form and add the "Isolate script" field. To disable this feature for all new globally-scoped client-side scripts set the system property "glide.script.block.client.globals" to false.

 

 

Hello @tsoct ,
You can configure the form layout and update the form with 'Isolate script' field and mark it as false(uncheck).
For more information, refer below article
https://www.servicenow.com/community/developer-blog/isolate-script-in-london/ba-p/2275821