- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-28-2023 12:12 AM
Categories currently exist: Test, Software, Hardware and Network.
Category: When selecting a test, display "This is a test" in the short description.
However, when a category other than test is selected, the short description "This is a test" is deleted and the user writes an arbitrary description.
Here, when the category is selected from non-test to non-test, the description entered in the short description will not be deleted and will continue to be used.
For example, when selecting Category: Software, enter "I am a company employee" in the short description. Next, when Category: Network is selected, the value of short description will continue to be displayed as "I am a company employee" that was entered when Category: Software was selected.
To achieve this, I wrote the script below. but it didn't work. Can someone please help me? Thank you.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2023 08:20 PM
I see what you are saying. I tested it again and saw the issue.
Figured out that oldValue is not something that we can catch until the record is saved. So I tried to find something to fetch the previous value and came across this post:
https://www.servicenow.com/community/developer-forum/onchange-client-script-previous-value/m-p/21614...
With the help of this, I could rewrite the script and it worked fine for me.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var previousValue;
if (g_scratchpad.previousValue == undefined) {
previousValue = oldValue;
} else {
previousValue = g_scratchpad.previousValue;
}
if (previousValue == 'test') {
// g_form.addInfoMessage(previousValue);
g_form.clearValue('short_description');
}
if (newValue == 'test') {
g_form.setValue('short_description', "This is a test short description");
} else {
g_form.setValue('short_description', g_form.getValue('short_description'));
}
g_scratchpad.previousValue = newValue;
}
Please test once with required changes from your end.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-28-2023 10:44 AM
Hi @matsuo akari ,
Can you please try below once
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2023 05:21 PM
Thank you, @Sonam Tiwari !
I tried implementing the script you taught me once, but when I selected another category from Category: Test, the value of shortdescription at Category: Test did not disappear and was used as it was. I want to erase this. Thank you for your help!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2023 05:23 PM
Thank you @Sonam Tiwari !
I tried implementing the script you taught me once, but when I selected another category from Category:Test, the value of shortdescription at Category: Test did not disappear and was used as it was. I want to erase this. Thank you for your help!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2023 08:20 PM
I see what you are saying. I tested it again and saw the issue.
Figured out that oldValue is not something that we can catch until the record is saved. So I tried to find something to fetch the previous value and came across this post:
https://www.servicenow.com/community/developer-forum/onchange-client-script-previous-value/m-p/21614...
With the help of this, I could rewrite the script and it worked fine for me.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var previousValue;
if (g_scratchpad.previousValue == undefined) {
previousValue = oldValue;
} else {
previousValue = g_scratchpad.previousValue;
}
if (previousValue == 'test') {
// g_form.addInfoMessage(previousValue);
g_form.clearValue('short_description');
}
if (newValue == 'test') {
g_form.setValue('short_description', "This is a test short description");
} else {
g_form.setValue('short_description', g_form.getValue('short_description'));
}
g_scratchpad.previousValue = newValue;
}
Please test once with required changes from your end.
Thanks