- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11 hours ago
Hi Folks,
I have a requirement which is on custom table where i have a name field that should accept value that starts with "IND" space "Region" space "Code"
so the expected result should be IND Region 001 or IND Region 002
IND and Region are static where as Code is dynamic. We have 5 codes. how can we fetch codes from a variable or an Array to get expected result.
i am trying with below code:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11 hours ago
Hi @Vijay Baokar ,
Please review below code-
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Get value from the field
var value = g_form.getValue('u_name_format');
// Define allowed codes
var allowedCodes = ['001', '002', '003', '004', '005'];
// Build dynamic regex pattern
var codePattern = allowedCodes.join('|'); // e.g., "001|002|003|004|005"
var pattern = new RegExp('^IND Region (' + codePattern + ')$');
// Validate the input
if (!pattern.test(value)) {
g_form.showFieldMsg('u_name_format', 'Value must be in the format "IND Region XXX", where XXX is one of: ' + allowedCodes.join(', '), 'error');
g_form.setValue('u_name_format', ''); // Optional: clear the invalid value
} else {
g_form.hideFieldMsg('u_name_format', true);
// Extract code part
var code = value.split(' ')[2]; // Get the "XXX" part
// Set values to other fields (example)
g_form.setValue('u_region_code', code); // Sets another field (e.g., u_region_code) with the code
g_form.setValue('u_region_name', 'India Region ' + code); // Custom text, optional
}
}
If you found my response helpful, please mark it as helpful and accept it as the solution.
Thank you
Nawal Singh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11 hours ago
Hi @Vijay Baokar ,
Please review below code-
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Get value from the field
var value = g_form.getValue('u_name_format');
// Define allowed codes
var allowedCodes = ['001', '002', '003', '004', '005'];
// Build dynamic regex pattern
var codePattern = allowedCodes.join('|'); // e.g., "001|002|003|004|005"
var pattern = new RegExp('^IND Region (' + codePattern + ')$');
// Validate the input
if (!pattern.test(value)) {
g_form.showFieldMsg('u_name_format', 'Value must be in the format "IND Region XXX", where XXX is one of: ' + allowedCodes.join(', '), 'error');
g_form.setValue('u_name_format', ''); // Optional: clear the invalid value
} else {
g_form.hideFieldMsg('u_name_format', true);
// Extract code part
var code = value.split(' ')[2]; // Get the "XXX" part
// Set values to other fields (example)
g_form.setValue('u_region_code', code); // Sets another field (e.g., u_region_code) with the code
g_form.setValue('u_region_name', 'India Region ' + code); // Custom text, optional
}
}
If you found my response helpful, please mark it as helpful and accept it as the solution.
Thank you
Nawal Singh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11 hours ago
Hi @Nawal Singh Thanks for the response. I have tried your code but its giving me the same result as mine , i mean its executing "
g_form.setValue('u_name_format', '');
always clearing the value without any fieldMsg. However if i give correct value as "IND Region 002" its accepting but if format is incorrect then msg is not coming.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11 hours ago
Hi @Nawal Singh i have moved
g_form.showFieldMsg
after
g_form.setValue('u_name_format', '')
now its working. Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10 hours ago
Glad to know that you got solution.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader