- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thursday
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
Thursday
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
Thursday
Glad to know that you got solution.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader