- 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-08-2024 01:19 AM
HI Akshay,
I have another scenario, that I missed, When users input data everything maps as should coutesy of your onChange script. However, we direct input from other sources such as MS Sentinel which uses playbooks to populate the "Category". The onChange does not cover this scenario, so I think that an "onLoad" ClientScript would be ideal. I was trying to reverse engineer your script it based on other onLoad scripts, but with no success. What would be the syntax if this was an onLoad script?
The user story would be:
On loading if the "category" is null, then do nothing. If the "category" is populated then update the type of service based on the mapping.
Kind regards,
Damian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2024 08:41 AM
I could not reverse engineer your advanced script as an onLoad but reworked my original one and it works as expected:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2024 06:11 AM
Thank you much for this. It worked perfectly once I replaced the ###### with my specific values. I do have a follow on question.
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: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-08-2024 08:42 AM
Thank you.