how to delete few rows from mrvs based on a select box variable?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2022 02:54 AM
Hello ,
We have a MRVS , inside that we have 3 variables ( days: select box type, choices: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday, All, Weekdays, Weekend)
Requirement:
suppose if I select days : Monday, Start time: 10:00 ,Stop time: 12:00 and the row will added to MRVS.
Now if I choose days: All, Start time : 9:00, and stop time: 11:30 then 2nd row will add to the MRVS
Here I want to delete the Monday row automatically, because Monday comes under 'All' days . I don't want to repeat the existing row data.
So final output should be only the row with days: 'All'. It should be applicable for every row as well.
Please provide your inputs @Mahendra
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2022 12:57 PM
I'm assuming you don't need to see this right away on the Request form / RITM / Catalog Task when a row is added - it's tricky to trigger a script when that happens, and if the user is paying attention they would delete the row(s), so here's an onSubmit Catalog Client Script (not within the MRVS) assuming the internal name of the MRVS is sched_mrvs, and the 3 variables are named days, start_time, and stop_time:
function onSubmit() {
var weekFlag = false;
var wkndFlag = false;
var newObjArr = [];
var obj = JSON.parse(g_form.getValue('sched_mrvs'));
for (var i=0; i<obj.length; i++) {
if (obj[i].days == 'All') {
newObjArr.push({
days: obj[i].days,
start_time: obj[i].start_time,
stop_time: obj[i].stop_time
});
g_form.setValue('sched_mrvs', JSON.stringify(newObjArr));
break;
} else if (obj[i].days == 'Weekdays') {
weekFlag = true;
} else if (obj[i].days == 'Weekend') {
wkndFlag = true;
}
}
if (weekFlag == true) {
for (var j=0; j<obj.length; j++) {
if (wkndFlag == false) {
if (obj[j].days == 'Weekdays' || obj[j].days == 'Saturday' || obj[j].days == 'Sunday') {
newObjArr.push({
days: obj[j].days,
start_time: obj[j].start_time,
stop_time: obj[j].stop_time
});
}
} else {
if (obj[j].days == 'Weekdays' || obj[j].days == 'Weekend') {
newObjArr.push({
days: obj[j].days,
start_time: obj[j].start_time,
stop_time: obj[j].stop_time
});
}
}
}
g_form.setValue('sched_mrvs', JSON.stringify(newObjArr));
} else if (wkndFlag == true) {
for (var k=0; k<obj.length; k++) {
if (obj[k].days == 'Weekend' || obj[k].days == 'Monday' || obj[k].days == 'Tuesday' || obj[k].days == 'Wednesday' || obj[k].days == 'Thursday'|| obj[k].days == 'Friday') {
newObjArr.push({
days: obj[k].days,
start_time: obj[k].start_time,
stop_time: obj[k].stop_time
});
}
}
g_form.setValue('sched_mrvs', JSON.stringify(newObjArr));
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-09-2022 01:32 AM
Hi Brad,
Thank you for above suggestion.
I have written the same code for Onload Catalog Client script on Variable Set. It is working in catalog item, But not working in Service Portal. Why?
Can you please provide specified code which will work for both catalog item and Service portal.
Thanks
Shabbir
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-09-2022 05:57 AM
I don't see how this exact script would work applied to a MRVS as getValue and setValue are referring to a variable on the parent form.
You can use g_service_catalog.parent.getValue('sched_mrvs') to get the value of the MRVS in the native UI and Service Portal. To set the value, something like this will work for both:
if (this) { // Service Portal method
this.cat_g_form.setValue('sched_mrvs', JSON.stringify(newObjArr));
} else { // native UI method
parent.g_form.setValue('sched_mrvs', JSON.stringify(newObjArr));
}
This requires an onLoad Catalog Client Script on the Catalog Item itself also, not within the MRVS:
function onLoad() {
if (this) {//we only need to do this for Service Portal
//We need to make the g_form object for the parent item available from the MRVS window
this.cat_g_form = g_form;
}
}
g_service_catalog is a newer method, so if that's not working in your version to get the value, use the same this.cat_g_form.getValue... or parent.g_form.getValue...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-09-2022 06:26 AM
The Exact script which you shared previously didn't work when I applied it to MRVS.
I changed some lines of code ( g_service_catalog.parent.getValue('sched_mrvs') and same for parent.g_form.setvalue..
Even after using g_service_catalog.getValue and setValue, It is not working in Service Portal.