Client script both onchange and onload
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2023 01:03 AM - edited 10-31-2023 01:14 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2023 01:52 AM
Hi @CE_ ,
You can modify the script logic as per your requirement & check if it works or not
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2023 01:11 AM - edited 10-09-2023 01:12 AM
Certainly! To achieve this behavior, you can use a client script in ServiceNow. Here's the client script code that you can use to handle the onChange and onLoad events for the "agreement" field:
function onLoad() {
checkAgreement();
}
function onChange(control, oldValue, newValue, isLoading) {
if (!isLoading) {
checkAgreement();
}
}
function checkAgreement() {
var agreementField = g_form.getControl('agreement');
var agreementValue = agreementField.value;
var message1 = g_form.getMessages('message1');
var message2 = g_form.getMessages('message2');
if (agreementValue === 'none') {
if (message1.length === 0) {
g_form.addInfoMessage('message1');
}
if (message2.length > 0) {
g_form.removeMessage('message2');
}
} else if (agreementValue === 'yes') {
if (message2.length === 0) {
g_form.addInfoMessage('message2');
}
if (message1.length > 0) {
g_form.removeMessage('message1');
}
} else if (agreementValue === 'no') {
// If 'no' is chosen, no message is shown
if (message1.length > 0) {
g_form.removeMessage('message1');
}
if (message2.length > 0) {
g_form.removeMessage('message2');
}
}
}
In this script:
- The `onLoad` function is triggered when the form is loaded.
- The `onChange` function is triggered whenever the "agreement" field value changes.
- The `checkAgreement` function checks the value of the "agreement" field and shows/removes messages accordingly.
Make sure to replace `'agreement'` with the actual name of your field in ServiceNow. Also, replace `'message1'` and `'message2'` with the actual names of your sys_ui_message records.
This script should cover the behavior you described: showing specific messages based on the selected value of the "agreement" field. Remember to test the script thoroughly in your ServiceNow instance to ensure it behaves as expected.
Mark my answer helpful & accepted if it helps you resolve your query.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2023 01:34 AM
Thanks, I'll try this. Why do I need these: (message1.length === 0) and these if (message2.length > 0)?
Can you explain what these does, why I cant write like this:
if (agreementValue === 'none') {
g_form.addInfoMessage('message1');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2023 03:07 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2023 02:20 AM
Hello @CE_ ,
You can use onChange client script for both onChange and onLoading
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
// your onLoad client script
}
// your onChange client script
}
Please mark my answer correct & helpful, if it helps you
Thank you
Thank you
G Ramana Murthy
ServiceNow Developer