Want to Hide and Show certain choices based on Product
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2024 03:15 AM
Hello All ,
We want to Hide and Show certain choices based on a Product. We have tried achieving the same with the help of OnLoad Client script, but no luck. Below is the script written for the same.
Any suggestions/help would be greatly appreciated.
Client Script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2024 03:38 AM
Hi @Shweta Desai ,
I tried your problem in my PDI and it works fine for me please check below script
var choicefield = 'u_cancellation_reason';
// g_form.clearOptions(choicefield);
var prodOff = g_form.getValue('u_product_offering');
alert('show prodoff' + prodOff);
var choicesforPDPL = ['Customer Request'];
var choicesforAccess = ['None', 'Access Problem', 'Circuit now working', 'Fault location change', 'Cost implication'];
if (prodOff == 'd1cb50d2472602100df5ad54116d43cb') { //PDPL
for (var i = 0; i < choicesforPDPL.length; i++) {
alert('inside for loop');
g_form.addOption("choicefield", choicesforPDPL[i], choicesforPDPL[i]); // "choicefield" in String
g_form.removeOption("choicefield", choicesforAccess[i], choicesforAccess[i]);
}
} else { // You forget to add one curly braces
g_form.clearOptions(choicefield); //Access
for (var j = 0; j < choicesforAccess.length; j++) {
g_form.addOption("choicefield", choicesforAccess[j], choicesforAccess[j]);
g_form.removeOption("choicefield", choicesforPDPL[j], choicesforPDPL[j]);
}
}
I made some changes in your code to make it run on my PDI
var choicefield = 'u_cancellation_reason';
var choicesforPDPL = ['Customer Request'];
var choicesforAccess = ['None', 'Access Problem', 'Circuit now working', 'Fault location change', 'Cost implication'];
if (g_form.getValue('assigned_to') == '46d44a23a9fe19810012d100cca80666') { //PDPL
for (var i = 0; i < choicesforPDPL.length; i++) {
alert('inside for loop = ' + choicesforPDPL[i]);
g_form.addOption('state', choicesforPDPL[i], choicesforPDPL[i]);
g_form.removeOption('state', choicesforAccess[i], choicesforAccess[i]);
}
} else {
g_form.clearOptions(choicefield); //Access
for (var j = 0; j < choicesforAccess.length; j++) {
alert('inside for loop = ' + choicesforPDPL[j]);
g_form.addOption('state', choicesforAccess[j], choicesforAccess[j]);
g_form.removeOption('state', choicesforPDPL[j], choicesforPDPL[j]);
}
}
Result
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2024 03:43 AM
Instead of using onload client script, simply use the UI policy (add condition i.e., product valuse in when to apply section and after saving add set choices visible true in the script or in UI action related tab below)
Or
Use the onchage client script(onchange field - product and add hide/show logic for choices)
Please refer the below articles:
https://www.servicenow.com/community/itsm-forum/show-or-hide-choices-of-field-based-on-selection-of-...
https://www.servicenow.com/community/developer-forum/show-hide-choices-of-field-based-on-selection-o...
https://www.servicenow.com/community/itsm-forum/display-and-hide-choices-based-on-a-choice-of-anothe...
If my answer helps you please mark as accepted/helpful by adding thumb in bottom of my post.
Thank You!!
Thank you!!
Dnyaneshwaree Satpute
Tera Guru