Array is returning null

tsoct
Tera Guru

The script include will return the stringify value shown below.

*** Script: JSON.stringify(obj): ["ABC","AF","EFD","CGED","AAAA","BBBB","CCCC","ZZZZZ",""]

 

I want  value above to appear in a Select Box type of catalog variable. The catalog client script below returns null as an answer. What could have gone wrong?

 

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

    var gb = new GlideAjax('ORGANIZATIONAjazUtils');
    gb.addParam('sysparm_name', 'getName');
    gb.getXML(getResponse);

    function getResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert("Answer: " + answer); //null is returned
        var array = answer.split(',');
        for (var i = 0; i < array.length; i++) {
            g_form.addOption('client_company_name', array[i].toString(), array[i].toString());
        }
    }

}

 

1 ACCEPTED SOLUTION

KevinBellardine
Kilo Sage

Hey @tsoct 

 

I've gone through this in my own PDI, and the only way I was able to replicate what you're seeing was if the user triggering the change didn't have the role for the script include. Have you checked the SI ACL?

View solution in original post

11 REPLIES 11

Brad Bowman
Kilo Patron
Kilo Patron

When you say the script include will return the value shown - obviously it's not as you are getting null.  That looks like an array, not JSON which would be more like

[{"field":"value","field2":"value"...

so in the script include, try returning something like

ArrName.join(',')

to return a string to the client

After changing to ArrName.join(','), script include value like below but catalog client script still giving null

 

Script: ABC,AFG,AFD,AFCD,AFCND

Hi @tsoct ,

 

Possible to share the script Include. Would be much easier to help.

 

Thanks,

Danish

Sure, here you go

getName: function() {
        
        var obj = [];
        try {
            var rest = new sn_ws.RESTMessageV2('ORGDetails', "Get Name");
            rest.setRequestHeader('Authorization', 'Bearer ' + this.token);
      
            var response = rest.execute();
            var responseBody = response.getBody();
            var httpStatus = response.getStatusCode();

            if (httpStatus.toString().startsWith("2")) {

                var parsedData = JSON.parse(responseBody);
                for (var loop = 0; loop < parsedData.value.length; loop++) {
                    obj.push(parsedData.value[loop].Org);
                }
                gs.log("Return: " + obj.join(','));
                return obj.join(',');

            } else {
                gs.error("Failed to retrieve Get Name: " + response.getErrorMessage());
            }

        } catch (ex) {
            var message = ex.getMessage();
            gs.error("Error: " + message);
        }
    },