Field Message based on color of text inside the field.

Hemanth Naik
Tera Guru

Hello Community,

 

I Have a requirement how can I show field info message on a field(customer) based on color of text inside the field. for example, I have customer field of reference type when I select X (option) the color of X changes to red color, and if I select Y(option), the color Y changes to green color, and if I select Z(option), the color is default. like this I have many options and color of selected option changes based on selection. (I have a script to change the color based on selection)

Now when the color of text is red I need to show info message(?) for customer field as "this customer is in red zone" when the color of text is green I need to show info message(?) for customer field as "this customer is in green zone"

 

Can anyone suggest how to achieve this.

1 ACCEPTED SOLUTION

Hi @Hemanth Naik,

Glad to see that you are following my solution. I'll make sure that your query got solved as soon as possible.

 

Proposed Solution

As per the code you pasted in tailing to my proposed solution, one suggestion from my side that you should not make a "Glide Record" on "Client Side". Instead of this, you should make a "Glide Ajax" call and make a "Script Include". I hope, you are able to get this point of view.

 

As a solution, you just need to add 2 methods of "g_form" like "showFieldMsg()" and "showErrorBox()" in your script as mentioned below. I personally tried it on my Personal Developer Instance to get your query solved as soon as possible. Hope this finds you helpful.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   g_form.showFieldMsg('caller_id', 'This user is in blue zone');
   g_form.showErrorBox('caller_id', 'This user is in red zone');
}

 

For your reference, also attaching screenshots of the outputs that will give you better insights of how this script is working or the best thing will be to follow the solution and execute the script on your instance.

AakashG2703_0-1711985217506.png

Output: -

AakashG2703_1-1711985242251.png

 

If you find any reply/information/knowledge/solution helpful, please don't forget to mark my solution and reply as helpful and accepted.

 

Thanks ‌‌:)

Aakash Garg

ServiceNow Developer

View solution in original post

7 REPLIES 7

AakashG2703
Mega Guru

Hi @Hemanth Naik,

Hope you are doing well.

 

Proposed Solution

 As per my understanding, you wrote an "On Change - Client Script" on that particular field to change the colors of that field or option based on the different conditions you are having. If this is so, you can re-use the same script and logic to display the Info Message as well like you are changing the color of that field using "g_form.addInfoMessage() or g_form.addErrorMessage" via existing Client Script.

 

If you find this information/knowledge/solution helpful, please don't forget to mark my solution and reply as helpful and accepted.

 

Thanks ‌‌:)

Aakash Garg

ServiceNow Developer

Hi @AakashG2703 , Thankyou very much for reply,

 

Yes , i have a on change script  on incident table in place to change the color

 

my script:

function onChange(control,oldValue,newValue,isLoading,isTemplate){

if( isLoading || newValue == "") {

return;

}

var acGr  == newGlideRecord("core_company");

acGr.addQuery('sys_id', newValue);

accGr.query();

if(acGr.next()); {

var stCode = acGr.getValue('u_st_code');

var stColor = acGr.getValue('u_st_color');

if (stCode=='red' && stColor=='green') {

g_form.getDisplayBox('company'). style == 'red';

} else if (stCode=='red' && stColor=='yellow'){

g_form.getDisplayBox('company'). style == 'red';

} else if (stCode=='green' && stColor=='yellow') {

g_form.getDisplayBox('company'). style == 'yellow';

}

}

}

 

this is the rough script i have, based on the conditions given above how can i show msg on the customer field as show in example picture below. 

HemanthNaik_0-1711978138227.png

 

Hi @Hemanth Naik,

Glad to see that you are following my solution. I'll make sure that your query got solved as soon as possible.

 

Proposed Solution

As per the code you pasted in tailing to my proposed solution, one suggestion from my side that you should not make a "Glide Record" on "Client Side". Instead of this, you should make a "Glide Ajax" call and make a "Script Include". I hope, you are able to get this point of view.

 

As a solution, you just need to add 2 methods of "g_form" like "showFieldMsg()" and "showErrorBox()" in your script as mentioned below. I personally tried it on my Personal Developer Instance to get your query solved as soon as possible. Hope this finds you helpful.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   g_form.showFieldMsg('caller_id', 'This user is in blue zone');
   g_form.showErrorBox('caller_id', 'This user is in red zone');
}

 

For your reference, also attaching screenshots of the outputs that will give you better insights of how this script is working or the best thing will be to follow the solution and execute the script on your instance.

AakashG2703_0-1711985217506.png

Output: -

AakashG2703_1-1711985242251.png

 

If you find any reply/information/knowledge/solution helpful, please don't forget to mark my solution and reply as helpful and accepted.

 

Thanks ‌‌:)

Aakash Garg

ServiceNow Developer

Hi @AakashG2703 ,

Thankyou so much for response,

yes, Glide record is not good practice in client script, i will try with script include.

Thankyou