Getting [object Object] from script include

Colleen1
Tera Contributor

I am calling a script include from my onChange Catalog Client Script and it is returning [object Object].  I need to bring back 3 items of information from the script include to populate my catalog form.  I tried following the logic in this post

https://www.servicenow.com/community/developer-forum/how-to-retrieve-multiple-values-from-a-script-i...  but still I am getting the data to return. When I write the values to the log, information is populating the values but not sending it back?  Any help would be greatly appreciated. 

 

OnChange script:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
    var cve_no =  g_form.getValue('primary_cve_or_cwe_number');
    alert( "cve_no " + cve_no );

    var ga = new GlideAjax('FindVulInfo');
    ga.addParam('sysparm_name','getVulData');
    ga.addParam('sys_parm_cve',cve_no);
    ga.getXML(getData);

function getData(response){
        // parse the answer to an object
    var answer = JSON.parse(response.responseXML.documentElement.getAttribute('answer'));
    // check if answer has been set correctcly
    alert( " answer " + answer);
    if (typeof answer != 'undefined') {
        if(answer.isValid == -1){
            alert( " cat clie score " + score);
            g_form.setValue('primary_on_cisa_exploited_list_yes_no',answer.exploit);  
            g_form.setValue('primary_cve_or_cwe_score',answer.score);
            g_form.setValue('primary_criticality_level',answer.criticality);
        }
       
    }
}
   
}
 
Script Include:
var FindVulInfo = Class.create();
FindVulInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getVulData: function(sys_id){
         var CVESysId = this.getParameter('sys_parm_cve') ? this.getParameter('sys_parm_cve') : sys_id; // check what type of parameter was used, by GlideAjax on as an argument.
        gs.log( " scr inc " + CVESysId);
        var cve = {}; // object that will be filled in
        var grCVE = new GlideRecord('sn_vul_nvd_entry');
        grCVE.get(CVESysId);
        gs.log( " scr inc after " + CVESysId + "exploit" + grCVE.exploit);
       
        var critical_value = '';
        // add multiple information to single object
        cve.exploit = grCVE.getDisplayValue('exploit');
        cve.score = grCVE.getDisplayValue('v3_base_score');
       
        var criticality = grCVE.getDisplayValue('normalized_severity');
               
        gs.log( " criticality " + criticality + " exploit " + cve.exploit + " score " + cve.score );
      
        if (criticality == '1 - Critical')
        {
             critical_value = 'Critical';
        }
        else if (criticality == '2 - High') {
             critical_value = 'High';
        }
        else if (criticality == '3 - Medium') {
            critical_value = 'Medium';
        }
        else if (criticality == '4 - Low') {
            critical_value = 'Low';
        }
        else if (criticality == '5 - None') {
            critical_value = 'None';
        }
        gs.log( " after critical_value " + critical_value + " exploit " + cve.exploit + " score " + cve.score );
 
        //cve.criticality = critical_value.toString();
        gs.log( " cve " + cve );
                             
        // return JSON string of the object
        return JSON.stringify(cve);
               
    },

    type: 'FindVulInfo'
});
2 ACCEPTED SOLUTIONS

Dibyaratnam
Tera Sage

Where exactly are you getting this object object, are the field values getting set with the code you have written? 

1 - Once do alert( " answer " + JSON.stringify(answer));

2- alert( " cat clie score " + score); --- this will not work, answer.score will work.

3- Also, in Script Include,make this change. for this var criticality = grCVE.getDisplayValue('normalized_severity'); => cve.criticality = grCVE.getDisplayValue('normalized_severity');

View solution in original post

Anil Lande
Kilo Patron

Hi,

Update your  function "getData" in client script like below:

function getData(response){
      
     var answer = response.responseXML.documentElement.getAttribute('answer');
    // check if answer has been set correctcly
    alert( " answer : " + answer);
  // parse the answer to an object
    answer = JSON.parse(answer);
    
    if (typeof answer != 'undefined') {
        if(answer.isValid == -1){
            alert( " cat clie score " + score);
            g_form.setValue('primary_on_cisa_exploited_list_yes_no',answer.exploit);  
            g_form.setValue('primary_cve_or_cwe_score',answer.score);
            g_form.setValue('primary_criticality_level',answer.criticality);
        }
       
    }
}

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

3 REPLIES 3

Dibyaratnam
Tera Sage

Where exactly are you getting this object object, are the field values getting set with the code you have written? 

1 - Once do alert( " answer " + JSON.stringify(answer));

2- alert( " cat clie score " + score); --- this will not work, answer.score will work.

3- Also, in Script Include,make this change. for this var criticality = grCVE.getDisplayValue('normalized_severity'); => cve.criticality = grCVE.getDisplayValue('normalized_severity');

Anil Lande
Kilo Patron

Hi,

Update your  function "getData" in client script like below:

function getData(response){
      
     var answer = response.responseXML.documentElement.getAttribute('answer');
    // check if answer has been set correctcly
    alert( " answer : " + answer);
  // parse the answer to an object
    answer = JSON.parse(answer);
    
    if (typeof answer != 'undefined') {
        if(answer.isValid == -1){
            alert( " cat clie score " + score);
            g_form.setValue('primary_on_cisa_exploited_list_yes_no',answer.exploit);  
            g_form.setValue('primary_cve_or_cwe_score',answer.score);
            g_form.setValue('primary_criticality_level',answer.criticality);
        }
       
    }
}

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Colleen1
Tera Contributor

These both helped me and it works now!!! Thank you both!!!