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

Hi,

did you check what came in the isUnique variable?

alert(isUnique);

Regards
Ankur

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

@Ankur Bawiskar - Yes .. Its showing false when 2 varibales values is same.

 

find_real_file.png

 

Hi,

I didn't the case.

You are saying 2 variables can be same but all 3 must be unique

Regards
Ankur

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

@Ankur Bawiskar 

Let me explain ,

2 variables can be same , but 3 can not be same .

-If 2 variables are same and another one is different then it will allow to submit the request.

- If 3 varibales are same then it will not allow to submit, this is the only case where it should not allow or any other combination it will allow.

Hi,

then you can do this

function onSubmit(){

	var arr = [];

	var val1 = g_form.getValue('field_name1').toString();
	var val2 = g_form.getValue('field_name2').toString();
	var val3 = g_form.getValue('field_name3').toString();

	if(val1 == val2 || val1 == val3 || val2 == val3){
		// do nothing as it should be allowed
	}
	else{
		arr.push(val1);
		arr.push(val2);
		arr.push(val3);
		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