Scripting when dot-walking two levels

keesh
Mega Contributor

Hi All,

I'm working on a catalog item that includes 2 variables: Computer (references [cmdb_ci_computer] table) & Stockroom (references [alm_stockroom] table). The requirement is to automatically populate the Stockroom variable based on the CI selected in the Computer variable. This requires 2 levels of dot-walking from the CI.Asset.Stockroom and I believe the best approach is to create a Script Includes + Catalog Client Script. I created both scripts below, however the functionality isn't working. Any guidance on what's missing would be much appreciated.

Computer CI Record:

find_real_file.png

Hardware Asset Record:

find_real_file.png

// Script Includes
var ComputerUtils = Class.create();
ComputerUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    getCIDetails: function(){
      var id = this.getParameter('sysparm_computer');
      var gr = new GlideRecord('cmdb_ci_computer');
      gr.get(id);
      return gr.asset.stockroom.getDisplayValue(); 
    },

    type: 'ComputerUtils'
});
 
// Catalog Client Script
function onChange(controloldValuenewValueisLoadingisTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var ga = new GlideAjax('ComputerUtils');
    ga.addParam('sysparm_name''getCIDetails');
    ga.addParam('sysparm_computer',g_form.getValue('computer'));
    ga.getXML(callBackMethod);
}

function callBackMethod(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if(answer != ''){
        g_form.setValue('stockroom'answer); 
    }
}
1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

Couple things:

1. this should solve your issue - change "return gr.asset.stockroom.getDisplayValue();" to "return gr.asset.stockroom.toString();".  You need a sys_id as your variable is a reference field, correct?

2. Just to make things easier, change "ga.getXML(callBackMethod);" in the Client Script to "ga.getXMLAnswer(callBackMethod);".  That way your "response" parameter contains the result without having to pick the answer out of the XML tree.

 

Just try #1 first to confirm and #2 if you want.

View solution in original post

8 REPLIES 8

Voona Rohila
Kilo Patron
Kilo Patron

 

 

Try keeping logs and verify values at all places.

 

var ComputerUtils = Class.create();
ComputerUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getCIDetails: function(){
      var id = this.getParameter('sysparm_computer');
      gs.info("CI IS"+id);
      var gr = new GlideRecord('cmdb_ci_computer');
      gr.get(id);
      gs.info("Stockroom"+gr.asset.stockroom.getDisplayValue());
      return gr.asset.stockroom.getDisplayValue(); 

    },
    type: 'ComputerUtils'

});

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

        return;

    }

     g_form.addInfoMessage(g_form.getValue('computer'));


    var ga = new GlideAjax('ComputerUtils');

    ga.addParam('sysparm_name', 'getCIDetails');

    ga.addParam('sysparm_computer',g_form.getValue('computer'));

    ga.getXML(callBackMethod);

}



function callBackMethod(response) {
   g_form.addInfoMessage("Hello");

    var answer = response.responseXML.documentElement.getAttribute("answer");

    if(answer != ''){

        g_form.setValue('stockroom', answer); 

    }

}

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

keesh
Mega Contributor

Still no luck with determining the root cause of the issue. The info icon is being displayed, however the Stockroom name isn't visible and shows 'Record not found'.

find_real_file.png

Jim Coyne
Kilo Patron

Couple things:

1. this should solve your issue - change "return gr.asset.stockroom.getDisplayValue();" to "return gr.asset.stockroom.toString();".  You need a sys_id as your variable is a reference field, correct?

2. Just to make things easier, change "ga.getXML(callBackMethod);" in the Client Script to "ga.getXMLAnswer(callBackMethod);".  That way your "response" parameter contains the result without having to pick the answer out of the XML tree.

 

Just try #1 first to confirm and #2 if you want.

keesh
Mega Contributor

Hi @Jim Coyne - I update my script include to the below code and it worked perfectly. Thank you for your help on this 🙂

var ComputerUtils = Class.create();
ComputerUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    getCIDetails: function(){
      var gr = new GlideRecord('cmdb_ci_computer');
      gr.get(this.getParameter('sysparm_computer'));
      return gr.asset.stockroom.toString();
      //return gr.asset.stockroom + '';
    },

    type: 'ComputerUtils'
});