- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2024 01:11 PM - edited 01-31-2024 01:43 PM
Hi Team,
I am working on portal end. I am trying to populate type field with my reference value when list collector "Parent" contains RITM type tickets.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var list = g_form.getDisplayValue('parent').toString();
var array = list.split(',');
alert("list is " +array);
for (var i = 0; i < array.length; i++) {
if (array[i].includes('RITM')) {
g_form.setValue('type', 'Enhancement::Planned Change');
alert("set type field for ritm");
// return;
} else if (array[i].includes('PRB')) {
g_form.setValue('type', 'HI22');
// return;
}
}
}
Can you please let me know what is the issue with this script? i am writing an onchange catalog client script.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2024 09:13 PM
Hi @Hervi
I've checked around your script, you can find my observation below.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var list = g_form.getDisplayValue('parent').toString(); //getDisplayValue won't work in servicecatalog_cat_item_view within platform view.
var array = list.split(',');
for (var i = 0; i < 1; i++) { //loop only the first element => i < array.length
if (array[i].includes('RITM')) {
var b = g_form.setValue('type', '152c8359375293402c4a005a54990e56', 'Enhancement::Planned Change');
break; //not required
} else if (array[i].includes("PRB")) {
g_form.setValue('type', 'HI22');
break; //not required
}
//What if the parnet list collector contains both RITM and PRB?
}
}
You can consider to pass the list collector to an Ajax script include to do your validation, then return the Type accordingly.
Sample
Script Include
var CLCatalogItemUtilAJAX = Class.create();
CLCatalogItemUtilAJAX.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getTypeByTaskID: function() {
var taskIDs = this.getParameter('sysparm_parent');
var classes = [];
var grTask = new GlideRecord('task');
grTask.addQuery('sys_id', 'IN', taskIDs);
grTask.query();
while (grTask.next()) {
var className = grTask.getRecordClassName();
if (classes.indexOf(className) === -1) {
classes.push(className);
}
}
var type = {};
if(classes.indexOf('sc_req_item') >= 0){
//try to avoid hard-coding by storing sys_id in a system property
type.sys_id = '152c8359375293402c4a005a54990e56';
type.name = 'Enhancement::Planned Change';
}
if(classes.indexOf('problem') >= 0){
//try to avoid hard-coding by storing sys_id in a system property
type.sys_id = '152c8359375293402c4a005a54990e52'; //replace the HI22's sys_id record
type.name = 'HI22';
}
//What if the parent list collector contains both RITM and PRB?
return JSON.stringify(type);
},
type: 'CLCatalogItemUtilAJAX'
});
Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('CLCatalogItemUtilAJAX');
ga.addParam('sysparm_name', 'getTypeByTaskID');
ga.addParam('sysparm_parent', newValue);
ga.getXMLAnswer(function(answer){
var type = JSON.parse(answer);
if(type && type.sys_id != undefined){
g_form.setValue('type', type.sys_id, type.name);
}
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2024 03:36 PM - edited 01-31-2024 03:40 PM
Is the type field the reference field you're refering to in your question title? If so, you should be setting the values to be sys_ids not plaintext. So for example:
g_form.setValue('type', <sys_id of Enhancement::Planned Change type>);
(Consider also passing a third argument to setValue() above, see for more details).
You should also break from your loop or return from your function once you find a match to exit early and stop unnecessary iterations of your loop. Do bear in mind that if you exit early then you'll be setting the type based on the first match rather than the last match from the list collector. Currently your loop sets it based on the last match from your list collector. If you want to set it based on the last match I would suggest iterating your array in reverse order and exiting early.
If that doesn't resolve the issue then it might help to describe what isn't working, what type of field is the type field as well as share what the alerts are displaying.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2024 04:13 PM
tried with sys id still not working. yes type is my reference field. I have used break in my updated script. alert is not displaying the set value.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var list = g_form.getDisplayValue('parent').toString();
var array = list.split(',');
alert("list is " +array);
for (var i = 0; i < 1; i++) {
alert("reference value is " +array[i]);
if (array[i].includes('RITM')) {
alert("before setting");
g_form.setValue('type', '152c8359375293402c4a005a54990e56');
alert("set type field for ritm");
break;
} else if (array[i].includes("PRB")) {
g_form.setValue('type', 'HI22');
break;
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2024 04:18 PM
Can you share what alerts you're seeing? Also, your loop now only does 1 iteration, are you sure that the item you're looking for is at index 0 in your array?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2024 04:23 PM
i have captured setvalue in a variable and alert shows undefined. Yes i am picking up right indexing.