- 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 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
02-01-2024 01:20 AM
Hi Timi,
I suppose this would be the correct way .
but what if i have to show more than 1 value . as in i dont have to set value rather in reference field i have to show them 3 options out of 6 to choose from. [This is for my PRB ticket requirement]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2024 02:55 AM
I am trying below codes -
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){
type.push({
sys_id:'152c8359375293402c4a005a54990e56',
name:'Enhancement::Planned Change'
});
}
if(classes.indexOf('problem') >= 0){
type.push({
sys_id:'d79870f60a25810200e92b77e3a41433',
name:'Caused by::Caused'
});
type.push({
sys_id:'d798ba000a2581020048305ef5287403',
name:'Resolved by::Resolves'
});
}
if(classes.indexOf('task') >= 0){
type.sys_id = '152c8359375293402c4a005a54990e52'; //replace the HI22's sys_id record
type.name = 'Enhancement delivered by::Delivers enhan';
}
//What if the parent list collector contains both RITM and PRB?
return JSON.stringify(type);
},
type: 'CLCatalogItemUtilAJAX'
});
My client script -
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var typeField = g_form.getReference('type');
if (typeField) {
typeField.clearOptions();
var ga = new GlideAjax('CLCatalogItemUtilAJAX');
ga.addParam('sysparm_name', 'getTypeByTaskID');
ga.addParam('sysparm_parent', newValue);
ga.getXMLAnswer(function(answer) {
var options = JSON.parse(answer);
options.forEach(function(option) {
typeField.addOption(option.sys_id, option.name);
});
});
}
}
not sure but this is not working. It is showing all records of reference field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2024 08:19 PM
Can you all try my code in portal view...it is definitely some other issue. As everything is running on native view.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2024 08:22 PM - edited 01-31-2024 10:42 PM
It's difficult for us to reproduce your issue as we don't have the same reference records nor do we know what your fields look like.
Perhaps you can try and create a new test catalog item just with this script and the two fields (type and your list collector) and see if that works. If it does, then that tells you the issue is with something else on the form.