- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2023 02:11 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2023 10:18 PM
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 !
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2023 08:24 AM
Hi @JamieD
From the function getAppValues in the script include, it now return an array.
So in your client script, after parsing, you will have an array. So you can loop through each object inside that array.
At these line
var answer = response.responseXML.documentElement.getAttribute("answer");
var arrApp = JSON.parse(answer);
for (var i in arrApp){
//do something
//you can get app value by using arrApp[i].<attribute>
}
Could you please clarify your intentions with the app data obtained from the script include? in order to understand what should be done in the client script.
What type of field is "u_sme_s"? I'm also uncertain about the reason for setting values multiple times for this field from your client script.
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2023 09:16 AM
These are all List fields: u_impacted_applications (Reference cmdb_ci_appl), u_system_admins_developers (Reference sys_user), u_sme_s (Reference sys_user).
The intent is that when an "Impacted Application" is added to a Project, that several of the user attributes on the Application CI record be applied to the Project record's Developer and SME list fields so the Project manager automatically knows which employees will be involved in the project in those capacities. I am probably setting values "multiple times" because I am not sure how best to script this to retain the existing values in these Lists and adding the new values pulled in from the CI. I'm not well-versed in scripting and rely on Community searches to help with items like this. I found this method awhile back and it worked fine when the starting field was a single Application reference value and not a list that could include multiple Applications.
I hope that helps. Let me know if I can provide further clarity. I appreciate the assist.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2023 10:18 PM
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 !
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2023 07:01 AM
Thank you for your help Tai Vu. Unfortunately, the new CI values are not populating. I did make a few changes to the code you provided to align variables and the table I'm using in the script include, but it's still not pulling in the new values. The Client Script does retain the existing values, though, so that portion is working as needed.
Maybe there is just a typo as it must be working for you given your screen print.
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_appl');
grApp.addQuery('sys_id', 'IN', appIDs);
grApp.query();
while (grApp.next()) {
var jsonObj = {};
jsonObj.rto = grApp.getValue('u_dr_priority_rto');
jsonObj.sox = grApp.getValue('u_sox');
jsonObj.slagrp = grApp.getValue('u_app_sla_group');
jsonObj.avail = grApp.getValue('u_expected_availability');
jsonObj.aprv = grApp.getValue('u_app_change_approval_needed');
jsonObj.buslead = grApp.getValue('owned_by');
jsonObj.prodowner = grApp.getValue('u_product_owner');
jsonObj.pba = grApp.getValue('u_primary_business_analyst');
jsonObj.sba = grApp.getValue('u_secondary_business_analyst');
jsonObj.appowner = grApp.getValue('managed_by');
jsonObj.techprim = grApp.getValue('supported_by');
jsonObj.techsec = grApp.getValue('u_technical_sme_secondary');
jsonObj.infraprim = grApp.getValue('u_infrastructure_sme_primary');
jsonObj.infrasec = grApp.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;
}
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);
//sme
arrSME.push(arrApp[i].techprim);
arrSME.push(arrApp[i].techpsec);
arrSME.push(arrApp[i].infraprim);
arrSME.push(arrApp[i].infrasec);
arrSME.push(arrApp[i].prodowner);
}
for (var j=0; j<devs.length; j++) {
if (arrDev.indexOf(devs[j]) >= 0) {
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(','));
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2023 07:57 PM - edited 11-19-2023 09:02 PM
Hi @JamieD
I couldn't figure out something wrong in the code. It also works well per my check.
Just double-check your Script Include, the Client Callable must be enabled.
Below is my test case.
Cheers,
Tai √u