The CreatorCon Call for Content is officially open! Get started here.

MRVS GlideAjax

Chelsea Moore
Tera Guru

Hello,

I am attempting to auto-populate variables in a multi-row variable set based on the logged in user whom is accessing the form. If the logged in user is listed as the owner of a business service, the service name, class, owner fields will auto-populate from all of the records the user owns on the cmdb_ci_service table. Below is my onLoad Catalog Client Script and Script Include which isn't working. Any help would be appreciated!

 

ChelseaMoore_0-1712238574818.png

 

Client Script:

function onLoad() {
    var ownerID = g_user.userID;
    var ga = new GlideAjax('getServiceInfo');
    ga.addParam('sysparm_name', 'getServiceOwner');
    ga.addParam('sysparm_ownerID', ownerID);
    ga.getXML(updateService);

function updateService(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    var servicesList = JSON.parse(answer);

    //servicesList.forEach(printService);

    if (servicesList.length > 0) {
        g_form.setValue('business_service_selection', JSON.stringify(servicesList));
    }
}
}
 
Script Include:
var getServiceInfo = Class.create();
getServiceInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getServiceOwner: function() {
        var servicesList = [];
        var service = new GlideRecord('cmdb_ci_service');
        service.addQuery('managed_by', ownerID);
        service.query();

        while (service.next()) {
            var obj = {};
            obj.service = service.getDisplayValue('name');
            obj.managed_by = service.getDisplayValue('managed_by');
            obj.service_class = service.getValue('sys_class_name');
            servicesList.push(obj);
        }
        return JSON.stringify(servicesList);
    },

    type: 'getServiceInfo'
});

 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Hi Chelsea,

In your Script Include, ownerID is undefined, so you'll need a line like this prior to using it in the addQuery:

 

var ownerID = this.getParameter('sysparm_ownerID');

 

Also when building the obj, since your first two MRVS variables are reference, they need the sys_id, not the displayValue, so try:

obj.service = service.getUniqueValue();
obj.managed_by = service.getValue('managed_by');

Beyond that, add some alerts to your client script, and gs.info lines to the Script Include so that you can confirm that each is running, the value passed between client and server and back, and if the GlideRecord is returning the expected records.

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Hi Chelsea,

In your Script Include, ownerID is undefined, so you'll need a line like this prior to using it in the addQuery:

 

var ownerID = this.getParameter('sysparm_ownerID');

 

Also when building the obj, since your first two MRVS variables are reference, they need the sys_id, not the displayValue, so try:

obj.service = service.getUniqueValue();
obj.managed_by = service.getValue('managed_by');

Beyond that, add some alerts to your client script, and gs.info lines to the Script Include so that you can confirm that each is running, the value passed between client and server and back, and if the GlideRecord is returning the expected records.

Thank you! I few tweaks and adding that line. I was able to get it working. I was so close!

Great to hear - and you are welcome!

 

 

Connect with me https://www.linkedin.com/in/brad-bowman-321b1567/