How to show Field Info message & Field error message on some condition ?

ads
Tera Expert

Hi All,

I have 3 varibales suppose A, B, C refering to the user table, If A=B, then it will show a field info message under the B field. If A=B=C, then it will show error message under the C field and clear the value of C as well.

I have tried the below , but its not working.


if (g_form.getValue('field_name') == g_form.getValue('field_name')) {
g_form.showFieldMsg('field_name', 'It cannot be the same person.', 'info', true);
}

else if ((g_form.getValue('field_name') == g_form.getValue('field_name')) &&(g_form.getValue('field_name') == g_form.getValue('field_name'))) {

g_form.clearValue('field_name');
g_form.showErrorBox('u_regional_director', 'Please note that the Hiring manager, Department head and Regional director cannot be the same person.', 'error');
}
}

 

@Ankur Bawiskar 

22 REPLIES 22

@ads

if your task is to restrict values based on other variables then use ref qualifier.

Why to allow user to select the value and then run the validation if the record selected is valid or not? it's bad user experience.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi @Ankur Bawiskar  ,

You concern is right , but I have 3 variables where 2 varibales can be same values and 3 varibales can not be same values , In this case ref qualifier will work or not .

 

And I hvae tried the below ref qualifier, still its not working.

'active=true^sys_idNOT IN' + current.variables.u_hiringmanager+ ',' + current.variables.u_department_head;

Hi,

then you just need to validate if all 3 values are unique because 2 variables which can have same value can be AB or AC or BC

then use onSubmit

function onSubmit(){

	var arr = [];

	arr.push(g_form.getValue('field_name1').toString());
	arr.push(g_form.getValue('field_name2').toString());
	arr.push(g_form.getValue('field_name3').toString());

	var isUnique = checkIfArrayIsUnique(arr);
	if(!isUnique){
		alert('Please note that the Hiring manager, Department head and Regional director cannot be the same person.');
		return false;
	}

}
function checkIfArrayIsUnique(arr) {
	var map = {}, i, size;

	for (i = 0, size = arr.length; i < size; i++){
		if (map[arr[i]]){
			return false;
		}

		map[arr[i]] = true;
	}

	return true;
}

regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Ankur Bawiskar - In the above onsubmit client script , when the alert is showing, I changed one of the field value , but still the alert is showing & and not allowing to submit.

Hi,

alert will only show when there is no unique sysId

did you alert what comes in that array?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader