- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2022 01:09 AM
Hi Community,
I have a requirement where I need to change the prefix of Number field onchange of a field. For example if State changes to 'XYZ' the prefix should be 'ABC' and if the state changes to 'TEST' the prefix should be 'abc'.
How we can achieve this?
thanks in advance.
Pihu
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2022 06:08 AM
Hi
Agree to what Mark has said and would suggest altering number is not a good practice to set it to something apart from what has been set in Number maintainenence table as it can cause issue with multiple starting number for different records present on the same table.
Also would make it difficult for user to search and they might not be aware that there are set of records which starts with custom string like abc
But still if you want, you can write a on Change client Script on State field and use the script below:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if(newValue == 2){
var getCurrentNumber = g_form.getValue('number');
var extractDigit = getCurrentNumber.substr(3,getCurrentNumber.length); // Replace 3 with the number from where your current number digit is starting. Like for example I have done this for Incident table where number currently starts with "INC0010126" so I am eliminating INC
var updatedNum = 'abc' + extractDigit;
g_form.setValue('number',updatedNum);
}
//Type appropriate comment here, and begin script below
}
Result:
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2022 01:12 AM
Hi there,
Technically it would be possible. Though be aware, that changing the prefix could cause other effects. For example, searching on the changed number will not result in finding your record. Are you aware of that?
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020-2022 ServiceNow Community MVP
2020-2022 ServiceNow Developer MVP
---
LinkedIn
Community article, blog, video list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2022 01:21 AM
I don't want to change the existing number , it is required whenever user is creating new record and depending on the changed field the number field should change.
Thanks,
Pihu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2022 06:08 AM
Hi
Agree to what Mark has said and would suggest altering number is not a good practice to set it to something apart from what has been set in Number maintainenence table as it can cause issue with multiple starting number for different records present on the same table.
Also would make it difficult for user to search and they might not be aware that there are set of records which starts with custom string like abc
But still if you want, you can write a on Change client Script on State field and use the script below:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if(newValue == 2){
var getCurrentNumber = g_form.getValue('number');
var extractDigit = getCurrentNumber.substr(3,getCurrentNumber.length); // Replace 3 with the number from where your current number digit is starting. Like for example I have done this for Incident table where number currently starts with "INC0010126" so I am eliminating INC
var updatedNum = 'abc' + extractDigit;
g_form.setValue('number',updatedNum);
}
//Type appropriate comment here, and begin script below
}
Result:
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2022 02:29 AM
Hi Shloke,
As you said, it will effect on Search for user or it may lead to duplicate numbers Is there any other alternative to do this without any bad effect?
Actually , we have two Number Maintenance for two tables whenever the state is 2 ,the number should come for one table with prefix 'xyz' and whenever state is not 2 it sholud come for other table with prefix 'abc'.
Can you please help with this?
Thanks,
Pihu