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

Trung
Kilo Guru

hi @keesh 

var grAsset = current.asset.getRefRecord();
if(grAsset.isValidRecord()) { // << only perform operations on it if it's a valid record
    return grAsset.getDisplayValue("stockroom"); 
}
return "";

please replace my code for line "return gr.asset.stockroom.getDisplayValue();"

 

keesh
Mega Contributor

Hi - Thanks for the response. Unfortunately that script didn't work.

burbigo1
Kilo Contributor

Hey, I really can't see what can be missing.

SHAREit MX Player

keesh
Mega Contributor

Any input from the community would be appreciated.