- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2024 05:05 AM
My first attempt at scripting, so be gentle!
The business scenario is we want to identify the the Operational Teams area based on the "Category" selected. I am testing ths in my PDI and I have created both a new Choice and String field to capture the output. (I was not sure which was the best type of field).
I have written the following "OnChange" script to populate either one of these fields with a value based on the Category selected. I have removed a number of Categories, and wondered if there is a more efficient syntax.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2024 05:39 AM
Your script looks fine, and is what needs to be done in scenarios like this. An alternate approach to consider is using the Switch statement
https://www.w3schools.com/js/js_switch.asp
One is not necessarily better or more efficient than the other, but you may prefer coding the case/switch format, or reading it when you or someone else comes back to update this script later.
Assuming you are running this onChange of Category, you don't need
if (g_form.getValue('category')) {
since the very first if statement stops the script from running if the field was changed to blank/empty.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2024 06:56 AM - edited 08-03-2024 06:58 AM
Hi @Damian Mudge ,
You are correct with onchange client script.
I have just modified the code for you. This should just work fine for your use case.
You can replace client script with this snippet.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var categoryMapping = {
'#####_ba': 'SOC',
'#####_cd': 'SOC',
'#####_edr': 'SOC',
'#####_mdr': 'SOC',
'#####_sdr': 'SOC',
'#####_ti': 'SOC',
'#####_tprm': 'SOC',
'#####_vi': 'SOC',
'network': 'NOC',
'firewall': 'NOC',
'wifi': 'NOC',
'lam': 'NOC',
'database': 'Internal',
'hardware': 'Internal',
'software': 'Internal',
'other': 'Internal',
'Sentinel': 'Platform'
};
var typeMapping = {
'SOC': '1',
'NOC': '2',
'Internal': '3',
'Platform': '4'
};
var typeOfService = categoryMapping[newValue];
if (typeOfService) {
g_form.setValue('u_type_of_service', typeMapping[typeOfService], typeOfService);
g_form.setReadOnly('u_type_of_service', true);
}
}
Please mark this as solution and helpful.
Thanks
Akshay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2024 06:24 AM
Hi @Damian Mudge - You would typically make the field visible, set its value, then hide it. It is possible to use GlideAjax, however, in most cases that would not be the most appropriate or efficient method.
While GlideAjax can be used to update the value of a hidden field, it involves an additional server call, which can be less efficient and more complex compared to handling the field visibility and value setting directly on the client side. GlideAjax is more appropriate for scenarios where server-side processing is required beyond simple field value setting.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2024 05:39 AM
Your script looks fine, and is what needs to be done in scenarios like this. An alternate approach to consider is using the Switch statement
https://www.w3schools.com/js/js_switch.asp
One is not necessarily better or more efficient than the other, but you may prefer coding the case/switch format, or reading it when you or someone else comes back to update this script later.
Assuming you are running this onChange of Category, you don't need
if (g_form.getValue('category')) {
since the very first if statement stops the script from running if the field was changed to blank/empty.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2024 06:13 AM
Thank you Brad for this. It worked perfectly.
As I mentioned to Asjkay further down the thread, the field 'u_type_of_service' field has to be visible on the Incident form in order for this to work. When I add it to the form it works perfectly, but when I remove it, it does not update. Is there something else I should be doing so that the 'u_type_of_service' field is updated behind the scenes so to speak.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2024 06:52 AM
The field has to be added to the form in order for g_form to set the value in a Client Script, but it will still update the value even if the Visible or Display attribute is set to false, and/or Read only is true, so if you don't want the u_type_of_service field to be displayed on the form, you can hide it in an onLoad Client
Script or UI Policy, and the onChange script should still work fine.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2024 06:56 AM - edited 08-03-2024 06:58 AM
Hi @Damian Mudge ,
You are correct with onchange client script.
I have just modified the code for you. This should just work fine for your use case.
You can replace client script with this snippet.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var categoryMapping = {
'#####_ba': 'SOC',
'#####_cd': 'SOC',
'#####_edr': 'SOC',
'#####_mdr': 'SOC',
'#####_sdr': 'SOC',
'#####_ti': 'SOC',
'#####_tprm': 'SOC',
'#####_vi': 'SOC',
'network': 'NOC',
'firewall': 'NOC',
'wifi': 'NOC',
'lam': 'NOC',
'database': 'Internal',
'hardware': 'Internal',
'software': 'Internal',
'other': 'Internal',
'Sentinel': 'Platform'
};
var typeMapping = {
'SOC': '1',
'NOC': '2',
'Internal': '3',
'Platform': '4'
};
var typeOfService = categoryMapping[newValue];
if (typeOfService) {
g_form.setValue('u_type_of_service', typeMapping[typeOfService], typeOfService);
g_form.setReadOnly('u_type_of_service', true);
}
}
Please mark this as solution and helpful.
Thanks
Akshay