replace one word in short description field on change other field value by client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2024 05:27 AM
When an Agent updates or changes the System(Choice field) field in the agents input the Short description fields need to be updated accordingly.
We need replace system field value with one of the word in the Short description field value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2024 01:01 AM
Please correct me
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2024 09:43 AM
Hey @vinnus ,
I see you're running into an issue with the script you implemented to update the short description based on the user's course selection. The script seems to be throwing an error because it attempts to use a variable (replacementWord) before it's assigned a value. Let's break down the problem and provide a corrected script that incorporates some improvements.
Understanding the Problem:
The original script defines a mapping between system values (like "SAP") and their corresponding replacement words (like "SAP HANA") in the systemToWordMap object. However, the script tries to access replacementWord even if there's no matching entry for the chosen system value. This leads to an error since replacementWord is undefined in such cases.
Your Implementation:
You've implemented a solution that addresses this issue and offers some additional enhancements:
- Handling Missing Replacements: The script now includes an else block that logs a warning message if the chosen system doesn't have a replacement word in the mapping. This helps you identify scenarios where the script might not behave as expected.
- Flexible Matching: The script uses a regular expression to match the course value anywhere within the short description. This makes it more versatile compared to assuming the course value is always the first word.
- Case-Insensitive Matching: The regular expression includes the gi flag, making the replacement case-insensitive. This ensures that "SAP" and "Sap" would both be replaced with the corresponding word.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || control.id !== 'u_course') { // Replace 'u_course' with your actual system field ID
return;
}
var shortDescField = g_form.getField('short_description');
if (shortDescField) {
var newDesc = shortDescField.getValue();
var chosenSystem = newValue;
// Define a mapping between chosen system value and replacement word
var systemToWordMap = {
"SAP": "SAP HANA",
"SAP Analytics": "Tableau",
// Add more mappings as needed
};
// Check if chosen system has a corresponding replacement word
var replacementWord = systemToWordMap[chosenSystem];
if (replacementWord) {
// Use regular expression for flexible matching (case-insensitive)
var regex = new RegExp("\\b" + chosenSystem + "\\b", "gi");
newDesc = newDesc.replace(regex, replacementWord);
shortDescField.setValue(newDesc);
} else {
// Handle missing replacement (optional)
gs.warning("No replacement word found for course: " + chosenSystem);
}
}
}
- Replace 'u_course' with the actual ID of your system field where the user selects the course.
- Replace 'short_description' with the actual ID of your short description field.
- Modify the systemToWordMap object to include all desired system values and their corresponding replacement words.
By implementing these changes, the script should function as intended and provide a more robust solution for updating the short description based on the user's course selection.
Thanks
Aravind Panchanathan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2024 03:11 AM
Hi
You can use replace() method
var newValue = g_form.getValue('system_value');
var ShortDescription = shortDescription.replace('word_to_be_replace', newValue);
g_form.setValue('short_description', ShortDescription);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2024 10:23 AM - edited 03-09-2024 10:27 AM
Hi there @vinnus
Firstly, create a new client script in ServiceNow and define the logic to extract the value of the System field and determine the replacement word for the Short Description field based on this value. Attach this client script to the System field in the form where agents input data.
sample script below
function onChangeSystemField() {
var systemFieldValue = g_form.getValue('system');
var replacementWord = '';
var shortDescriptionValue = g_form.getValue('short_description');
var updatedShortDescriptionValue = shortDescriptionValue.replace(systemFieldValue, replacementWord);
g_form.setValue('short_description', updatedShortDescriptionValue);
}
If this helps kindly mark the response as accepted thanks much,
Regards
Azar
Kind Regards,
Mohamed Azarudeen Z
Developer @ KPMG
Microsoft MVP (AI Services), India