Set Catalog Item User Reference Variable based upon cmdb_rel_ci

purdue
Kilo Sage

Hello,

Any assistance is appreciated.

 

I have a cat item which has the following

1. Reference Variable pointing to cmdb_ci_business_app table.   2. Once I make a selection  from 1 then a lookup selectbox filters values from cmdb_rel_ci see below.

Screenshot 2024-11-13 at 2.05.42 PM.png

3. There is a reference variable to the user table which they would populate with the child.owned_by when 2 is selected.  I tried below but it is not working.

Screenshot 2024-11-13 at 2.14.46 PM.png

1 ACCEPTED SOLUTION

Here is the corrected Script Include, with some temporary log lines, so if you are still not getting the expected results you can share which system logs you are seeing (filter on Message starts with SI)

var Cloud_Owned_by = Class.create();
Cloud_Owned_by.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    updateOwned: function () {
        var results = 'none';        
        var buildingid = this.getParameter('sysparm_application_service');
        gs.info('SI running: buildingid = ' + buildingid);
        var loc = new GlideRecord('cmdb_ci');
        loc.addQuery('sys_id', buildingid);  
        loc.query();
        if (loc.next()) {  
            gs.info('SI record found: ' + loc.name);
            results = {
                "owned_by": loc.getValue('owned_by'),
                "managed_by": loc.getValue('managed_by')
            };
            gs.info('SI results: ' + JSON.stringify(results));
            return JSON.stringify(results);
        } else {
            return results;
        }
    },
    type: 'Cloud_Owned_by'
});

View solution in original post

7 REPLIES 7

Brad Bowman
Kilo Patron
Kilo Patron

To be clear, the reference qualifier for 3. will limit the records that can be selected, not populate the value of the variable.  In the qualifier, you need a field on the user table to go with the variable.owned_by, and then you need an AND character (^) before the active, so maybe you're looking for something more like:

javascriptt: 'sys_id=' + current.variables.select_your_application_service_environment.owned_by + '^active=true^internal_integration_user=false';

of course with just one t in javascript.  If you want to actually populate the variable with this value, clear the reference qualifier and add an onChange Catalog Client Script when 2. changes.  This script would use GlideAjax to call a Script Include, passing in the newValue.  The Script Include will query the cmdb_ci table for the sys_id passed in from the client, returning the value of the owned_by for the client script to populate in the reference variable. Your Lookup Value Field on 2 should be Child instead of Parent.

Hello,

Yes I would like to be set from the child when selected. I also need to set Managed By variable so I would assume we can set both in the same call.  Any assistance is appreciated.  Thanks Brad.

Client Script

Screenshot 2024-11-13 at 3.46.38 PM.png

Screenshot 2024-11-13 at 3.49.13 PM.png

Sure - add a comma to the end of line 8, then a similar format for loc.managed_by.  Consider changing line 13 to return 'false'; or something unique so when troubleshooting the response from the server you'll know that the script ran. 

On the GlideRecord, if you are passing in the application_service parameter which contains the sys_id of the (child) CI, you'll want it to look like this:

var loc = new GlideRecord('cmdb_ci');
loc.addQuery('sys_id', buildingid);
loc.query();
if (loc.next()) {
    var results = {
        "owned_by": loc.getValue('owned_by'),
        "maanged_by": loc.getValue('managed_by')
    };
...

 

Hello,

Unfortunately does not work.  Thanks.

Client Script

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('Cloud_Owned_by');
ga.addParam('sysparm_name', 'updateOwned');
ga.addParam('sysparm_application_service', g_form.getValue("select_your_application_service_environment"));
ga.getXMLAnswer(updateOwned);
}

function updateOwned(answer) {
var clearvalue; // Stays Undefined
if (answer) {
var returneddata = JSON.parse(answer);
g_form.setValue("executive_sponsor", returneddata.owned_by);
g_form.setValue("technical_owner", returneddata.managed_by);
} else {
g_form.setValue("executive_sponsor", clearvalue);
}

//Type appropriate comment here, and begin script below
 
}
SI
var Cloud_Owned_by = Class.create();
Cloud_Owned_by.prototype = Object.extendsObject(AbstractAjaxProcessor, {
updateOwned: function () {
var buildingid = this.getParameter('sysparm_application_service');
var loc = new GlideRecord('cmdb_rel_ci');
if (loc.child != ''){
var results = {
"owned_by": loc.getValue('owned_by'),
"managed_by": loc.getValue('managed_by')
};
return JSON.stringify(results);
 
} else {
return null;
}
},
type: 'Cloud_Owned_by'
});