The Zurich release has arrived! Interested in new features and functionalities? Click here for more

getXMLAnswer returning null response

amielalcala
Kilo Sage

Hi everyone,

 

I need help on the scripting for a catalog. My script include is already working and it returns the value that I need.

 

var CIEnvironmentChecker = Class.create();
CIEnvironmentChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    initialize: function() {},

    checkCIEnvironment: function(parentCI) {
        gs.log("CIEnvironmentChecker Script Include Invoked. Parent CI: " + parentCI); 
        var validEnvironments = [];
        var ciRelationship = new GlideRecord('cmdb_rel_ci');
        ciRelationship.addQuery('parent.sys_id', parentCI); 
        ciRelationship.query();

        while (ciRelationship.next()) {
            var environment = ciRelationship.child.environment; 
            if (environment && validEnvironments.indexOf(environment.toString()) === -1) {
                validEnvironments.push(environment.toString());
            }
        }

        // Return the array as a JSON string
        return JSON.stringify(validEnvironments);
    },

    type: 'CIEnvironmentChecker'
});

 

 

But when I try to call the script include via GlideAjax and getXMLAnswer, the Response log returns null which I'm guessing it doesn't receive anything from the script include. 

 

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

  var ga = new GlideAjax('CIEnvironmentChecker');
  ga.addParam('sysparm_name', 'checkCIEnvironment');
  ga.addParam('sysparm_parentCI', newValue); 

  console.log("GlideAjax created, sysparm_parentCI: " + newValue);

	alert(JSON.stringify(ga));

  ga.getXMLAnswer(function(response) {
    alert("Response: " + response);

	var result = response.responseXML.documentElement.getAttribute("answer");
        var environmentsArray = JSON.parse(result);

    console.log("Valid Environments: ", environmentsArray);  

    if (!response) {
      console.error("No response from Script Include.");
      return;
    }

    var result = response.responseText;

    if (result) {
      console.log("Result: " + result);
      var environments = JSON.parse(result);
      
      g_form.clearOptions('v_environment');
      
      if (environments.length === 0) {
        g_form.addOption('v_environment', '', '-- No valid environments --');
      } else {
        environments.forEach(function(environment) {
          g_form.addOption('v_environment', environment, environment);
        });
      }
    } else {
      console.error("No valid result returned from the Script Include.");
    }
  });
}

 

How do I fix this null issue?

 

Thank you.

3 REPLIES 3

OlaN
Giga Sage
Giga Sage

Hi,

I believe the issue here is that the value you send in via the client script, is not processed since you only use the backend value of the parameter.

I.e. the variable parentCI is undefined when you call the script include.

 

To use the value provided by the client script add this line in your script include

checkCIEnvironment: function(parentCI) {
  if (!parentCI){
    parentCI = this.getParameter('sysparm_parentCI');
  }

 

amielalcala
Kilo Sage

Hi @OlaN,

 

Thanks for the prompt response!

 

This didn't work unfortunately but I did try to run the script include in background scripts and it returned the sys_id of the parentCI which means it's working.

 

Now when I try to call the script include in client script, it is now returning null. I tried using JSON.stringify and JSON.parse but it didn't work. What else am I missing here?

When you call the script include using backgrounds script, you're calling it from serverside.

That means your variable parentCI is defined.

When doing the same thing from a client script, which is frontend, then the variable doesn't get defined, because it's passed in another way. Therefore the script include works, but only for one scenario, not both.