Need help in Number field

Pihu1
Tera Contributor

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

1 ACCEPTED SOLUTION

shloke04
Kilo Patron

Hi @Pihu 

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:

find_real_file.png

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

10 REPLIES 10

Hi @Pihu 

First of all, what I mentioned above is it may confuse user as there will be two different prefix for the same number field which was my concern.

However it will not lead to duplicate as everytime ServiceNow generates a new number and script above is just adding a new prefix to an existing number generated.

Now this is not clear to me still the statement which you have given below:

"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'."

Every table can have their own number maintainenence so I am still confused with your statement when you say that when state is 2(in progress) you want to fetch the prefix from some other table? Is this your requirement?

If yes, I would strongly ask you to check with the person who has given this and what is the benefit which is going to come out of this?

What is the actual use case is he or you looking for and for that you have come up with this kind of approach which is technically possible as explained above and will work for you as well, but sometime I believe we should say a NO to a requirement as well and make the customer understand.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

 

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Pihu1
Tera Contributor

Hi Shloke,

 

Let me explain my scenario.

There are two incident types ,one is First party(1P)  and third party (3P) both are stored in two different tables and can be differentiate by Number with prefix INCI and INC and we have another module "All Incident" where user can create both type of incidents So my requirement is when user select 1P the Number field should populate with INCI prefix and if they select 3P , the Number field should populate without I or INC.

Let me know if any information is needed.

Thanks,

Pihu

Got it. So basically through All Module user have the capability to create either First Party or Third party incident.

Couple of things here which you need to note and take next step:

1) I think incident is getting created in the same table only , it might be different views getting loaded which is giving an expression that it is getting created in different tables.

2) Also I believe there must be a field on the Incident form which the user must be using to differentiate the type of Incident . So you can use the same client script shared above and that should solve your purpose.

3) Also would like to add that if there is a field on the Incident form then why not use that field only to differentiate the two type of Incident?

Let me know your thought. If my understanding is correct based on what you have shared.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Pihu1
Tera Contributor

Hi,

1) I think incident is getting created in the same table only , it might be different views getting loaded which is giving an expression that it is getting created in different tables.

No, we have two different tables for 1P and 3P

2) Also I believe there must be a field on the Incident form which the user must be using to differentiate the type of Incident . So you can use the same client script shared above and that should solve your purpose.

Yes ,there is a field Incident Type based on that only it should populate Number with prefix and the client script above is working but it is just removing 'I' when user selects 3P and that is correct but my concern here is Number generated here from All Incidents may be same with incident created from 3P party module.

I hope you got my concern.

NOTE: We have currently two Number maintenance for 1P and 3P and in ALL Incidents module whenever we create incident by default INCI prefix is populating and when we select 3P it is just removing 'I' and all other numbers are same

Thanks,

Pihu

Yes, then instead of using a On Change Client script use a On Load Client Script which will check for the value of the field which identifies the type of Incident and then use the script below:

use this in an On Load Client Script:

if(g_form.getValue('Field Name')== 'Value'){ // Replace "Field Name" with the field which check for type of Incident
			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);
		}

else if(g_form.getValue('Field Name')== 'Value'){ // Replace "Field Name" with the field which check for type of Incident)
// Write the other logic with same script as above and just alter the abc with what you want.


}

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke