Issues with the sysid comparsion for related list array to value from variable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2023 07:33 AM
Hello everyone,
I have created script include and client script, my aim is to compare sysid of the value of the variable 'project manager' and check if this sysid is in the saved array from script include. GlidRecord and script include is working correctly, there must be some issue with the logic for saving the array values or iteration in the client script but I dont know how to correctly implement it.
isDifferentManager: function() {
var programSysID = this.getParameter('sysparm_program_sys_id');
var programGR = new GlideRecord('table');
programGR.addQuery('sys_id', programSysID);
programGR.setLimit(1);
programGR.query();
var programManagers = [];
while (programGR.next()) {
var programManagerRef = programGR.manager;
if (programManagerRef) {
var programManagerSysID = programManagerRef.getValue('sys_id');
programManagers.push(programManagerSysID);
return programManagers;
}
}
},
type: 'utils'
});
Client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var projectManagerValue = g_form.getValue('project_manager');
var programValue = g_form.getValue('program');
// Exit if program is empty
if (!programValue) {
return;
}
var UtilsAJAX = new GlideAjax('utils');
UtilsAJAX .addParam('sysparm_name', 'isDifferentManager');
UtilsAJAX .addParam('sysparm_program_sys_id', g_form.getValue('program'));
UtilsAJAX .getXML(function(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
var programManagers = answer.split(',');
for (var i = 0; i < programManagers.length; i++) {
if (programManagers[i] === projectManagerValue) {
g_form.addErrorMessage(getMessage('message'));
break;
}
}
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2023 08:42 AM
Hi @Spike236 ,
I take a look in your script and some things doesnt make sense,
var programGR = new GlideRecord('table');
- In the GlideRecord('table'); you need to specify the table would you want to use like GlideRecord('pm_program'); table doesnt exist
programGR.addQuery('sys_id', programSysID);
programGR.setLimit(1);
- Why you set a limit to your query? If you specify the sys_id and make a query from that you can only have 1 possible result.
On that part the programGR is the you would return the sys_id of the manager not a record from the sys_user table, must be something like this:
while (programGR.next()) {
var programManagerRef = programGR.manager;// That would return a sys_id of the manager, reference to the sys_user table, you need to make sure the field of the table is called manager and not something like program_manager
if (programManagerRef) {
return programManagerRef;
}
}
If you are returning a single string containing the sys_id of the manager you wont need to return and array, because the I thing you can only have 1 manager for program.
Another point is I dont understand so much the context of what are you trying to do, if you can provide more information about what are you trying to do from which table and to which another table with the fields names we can provide a better solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2023 12:18 AM
Hello Marco0o1, thank you for your message, i put the 'table' to make it anonymous. You are correct, setlimit is useless in this script. Problem is there can be more than one manager for the program field as its a list type, there are cases when there is only one person but also cases with several managers.