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

Retrieve Related Record Data from all Records in a List Field

JamieD
Tera Contributor

I am looking to retrieve associated attributes from Application CI records as they are applied to a Project's field called "Impacted Applications" (field type = List).  With the client script below, I am able to do this if a single Application CI exists in the Impacted Application field, but it does not work when there are 2 or more Application CIs in the list.  Below is the script that is working with a single Application.  Could someone help me get this to loop through the Application CIs when multiple are applied and identify related user values for all Applications in the list?

 

Client Script 

JamieD_1-1699999775831.png

 

JamieD_0-1699999200198.png

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

//List of Applications impacted by the project
var ci = g_form.getValue('u_impacted_applications');

//store current project team values in scratchpad so they are not lost
    g_scratchpad.currdev = g_form.getValue('u_system_admins_developers');
    g_scratchpad.currsme = g_form.getValue('u_sme_s');

//Call Script Include to look up Application Ci user field values
var ga = new GlideAjax('ApplicationCIFields');
ga.addParam('sysparm_name', 'getAppValues');
ga.addParam('sysparm_ci', ci);
ga.getXML(myCallBack);

//Populate the Project fields with values returned from Script Include
function myCallBack(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    var jsonObj=JSON.parse(answer);
    g_form.setValue('u_system_admins_developers', g_scratchpad.currdev); //retain prior values
    g_form.setValue('u_system_admins_developers', jsonObj.buslead);
    g_form.setValue('u_system_admins_developers', jsonObj.appowner);
    g_form.setValue('u_sme_s', g_scratchpad.currsme);  //retain prior values
    g_form.setValue('u_sme_s', jsonObj.techprim);
    g_form.setValue('u_sme_s', jsonObj.techsec);
    g_form.setValue('u_sme_s', jsonObj.infraprim);
    g_form.setValue('u_sme_s', jsonObj.infrasec);
    g_form.setValue('u_sme_s', jsonObj.prodowner);
    }

}

 

1 ACCEPTED SOLUTION

Hi @JamieD 

Okay, let's try my approach below.

#ClientCallable Script Include

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

    getAppValues: function() {
        var appIDs = this.getParameter('sysparm_ci');
        var arrApp = [];
        var grApp = new GlideRecord('cmdb_ci_business_app');
        grApp.addQuery('sys_id', 'IN', appIDs);
        grApp.query();
        while (grApp.next()) {
            var jsonObj = {};
            jsonObj.rto = CI.getValue('u_dr_priority_rto');
            jsonObj.sox = CI.getValue('u_sox');
            jsonObj.slagrp = CI.getValue('u_app_sla_group');
            jsonObj.avail = CI.getValue('u_expected_availability');
            jsonObj.aprv = CI.getValue('u_app_change_approval_needed');
            jsonObj.buslead = CI.getValue('owned_by');
            jsonObj.prodowner = CI.getValue('u_product_owner');
            jsonObj.pba = CI.getValue('u_primary_business_analyst');
            jsonObj.sba = CI.getValue('u_secondary_business_analyst');
            jsonObj.appowner = CI.getValue('managed_by');
            jsonObj.techprim = CI.getValue('supported_by');
            jsonObj.techsec = CI.getValue('u_technical_sme_secondary');
            jsonObj.infraprim = CI.getValue('u_infrastructure_sme_primary');
            jsonObj.infrasec = CI.getValue('u_infrastructure_sme_secondary');
            arrApp.push(jsonObj);
        }
        return JSON.stringify(arrApp);

    },

    type: 'CLProjectUtilsAJAX'
});

 

#OnChange Client Script (Impacted Applications)

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }

    //clear value when Application changes to Empty
    if (newValue === '') {
        g_form.clearValue('u_system_admins_developers');
        g_form.clearValue('u_sme_s');
        return;
    }

    //Use this when you'd like to keep the existing users
    //who are manually added (not from the application attributes)
	/*
    var devs = g_form.getValue('u_system_admins_developers').split(',');
    var smes = g_form.getValue('u_sme_s').split(',');
	*/

    var ga = new GlideAjax('CLProjectUtilsAJAX');
    ga.addParam('sysparm_name', 'getAppValues');
    ga.addParam('sysparm_ci', newValue);
    ga.getXMLAnswer(function(response) {
        var arrApp = JSON.parse(response);

        var arrDev = [];
        var arrSME = [];
        for (var i in arrApp) {
            //dev
            arrDev.push(arrApp[i].buslead);
            arrDev.push(arrApp[i].appowner);
			//etc

            //sme
            arrSME.push(arrApp[i].techprim);
            arrSME.push(arrApp[i].prodowner);
			//etc

        }

        //Use this when you'd like to keep the existing users
        //who are manually added (not from the application attributes)
		/*
		for (var j=0; j<devs.length; j++) {
			if (arrDev.indexOf(devs[j]) === -1) {
				continue;
			}
			arrDev.push(devs[j]);
		}

        for (var k=0; k<smes.length; k++) {
            if (arrSME.indexOf(smes[k]) >= 0) {
				continue;
            }
			arrSME.push(smes[k]);
        }
		*/

        g_form.setValue('u_system_admins_developers', arrDev.join(','));
        g_form.setValue('u_sme_s', arrSME.join(','));

    });
}

 

Happy Coding Hours !

TaiVu_0-1700115515909.png

 

Cheers,

Tai Vu

View solution in original post

10 REPLIES 10

Aylee Andersen
Kilo Sage

Hi @JamieD,

 

Could you send the getAppValues function from your Script Include as well? My guess is that you're not splitting the list up correct in that Script Include function and that's why it's only working for one Application CI.

 

Also, within your myCallBack function, it looks like you're setting the u_system_admins_developers and u_sme_s fields over and over again. Currently the way you're doing it, it will override the previous value and only save the final instance of that field value. Are those both list fields? If so, you'll need to concat all the sys_id's as a string and then set the field value. 

Example:

var developers = g_scratchpad.currdev + "," + jsonObj.buslead + "," + jsonObj.appowner; // make sure all those are sys_id's and not display values
g_form.setValue('u_system_admins_developers', developers);

 

Hope that helps some!

- Aylee

Below is what I am using for the Script Include.  When I comment out the repeat additions in the Client Script and use the example you provided, the value "undefined" displays in the u_system_admins_developers list field.  

Script Include
var ApplicationCIFields = Class.create();

ApplicationCIFields.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getAppValues: function() {

var jsonObj={};
var uid = this.getParameter('sysparm_ci');
var CI = new GlideRecord('cmdb_ci_appl');
CI.addQuery('sys_id',uid);
CI.query();
if (CI.next()) {

jsonObj.rto=String(CI.u_dr_priority_rto);
jsonObj.sox=String(CI.u_sox);
jsonObj.slagrp=String(CI.u_app_sla_group);
jsonObj.avail=String(CI.u_expected_availability);
jsonObj.aprv=String(CI.u_app_change_approval_needed);
jsonObj.buslead=String(CI.owned_by);
jsonObj.prodowner=String(CI.u_product_owner);
jsonObj.pba=String(CI.u_primary_business_analyst);
jsonObj.sba=String(CI.u_secondary_business_analyst);
jsonObj.appowner=String(CI.managed_by);
jsonObj.techprim=String(CI.supported_by);
jsonObj.techsec=String(CI.u_technical_sme_secondary);
jsonObj.infraprim=String(CI.u_infrastructure_sme_primary);
jsonObj.infrasec=String(CI.u_infrastructure_sme_secondary);
}
return JSON.stringify(jsonObj);
},

type: 'ApplicationCIFields'
});

Tai Vu
Kilo Patron
Kilo Patron

Hi @JamieD 

In the function from your AJAX script include, you can define an array to push the application object into it.

Sample below.

getAppValues: function() {
	var appIDs = this.getParameter('sysparm_ci');
	var arrApp = []; //array result

	var grApp = new GlideRecord('cmdb_ci_business_app');
	grApp.addQuery('sys_id', 'IN', appIDs);
	grApp.query();
	while (grApp.next()) {
		var obj = {}; 
		obj.buslead = grApp.getValue('owned_by');
		obj.appowner = grApp.getValue('it_application_owner');
		//etc
		arrApp.push(obj);
	}
	return JSON.stringify(arrApp);
},

 

Let me know if it works for you.

 

Cheers,

Tai Vu

JamieD
Tera Contributor

I incorporated these changes to my Script Include and it doesn't appear to be working.  Result: There was no change to the users in the lists when I added a second application CI. 

 

I replied to Aylee above with my original Script Include.  Below is what I currently have configured with the changes you suggested.  Let me know if I misapplied your suggested changes:

Updated Script Include

var ApplicationCIFields = Class.create();
ApplicationCIFields.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
getAppValues: function() {
var uid = this.getParameter('sysparm_ci');
var arrCI = [];
 
var CI = new GlideRecord('cmdb_ci_appl');
CI.addQuery('sys_id','IN', uid);
CI.query();
while(CI.next()) {
var jsonObj={};
jsonObj.rto=CI.getValue('u_dr_priority_rto');
jsonObj.sox=CI.getValue('u_sox');
jsonObj.slagrp=CI.getValue('u_app_sla_group');
jsonObj.avail=CI.getValue('u_expected_availability');
jsonObj.aprv=CI.getValue('u_app_change_approval_needed');
jsonObj.buslead=CI.getValue('owned_by');
jsonObj.prodowner=CI.getValue('u_product_owner');
jsonObj.pba=CI.getValue('u_primary_business_analyst');
jsonObj.sba=CI.getValue('u_secondary_business_analyst');
jsonObj.appowner=CI.getValue('managed_by');
jsonObj.techprim=CI.getValue('supported_by');
jsonObj.techsec=CI.getValue('u_technical_sme_secondary');
jsonObj.infraprim=CI.getValue('u_infrastructure_sme_primary');
jsonObj.infrasec=CI.getValue('u_infrastructure_sme_secondary');
arrCI.push(obj);
}
return JSON.stringify(arrCI);
},
 
type: 'ApplicationCIFields'
});