using the validateField in a client script - upon ...
# suitescript
m
using the validateField in a client script - upon entering a value in a field I check if this value already exists in the system and if it does I alert the user. Is it possible to maintain focus in that field, meaning only allow moving away from the field if the value is unique?
t
Have you tried returning false? I'm not 100% sure if that works, but I seem to remember using that in the past
m
adding a return false doesn't work as it breaks, meaning it keeps throwing the alert infinitely
t
What if you put the return true statement in an else block after the if(item_exists == item_name)?
m
I'm convinced that the infinite loop with
return false;
on validateField is a defect (I know for sure it hasn't always acted like that) but I've been fighting support on and off for years to get them to admit it/do something about it and have, so far, come up empty handed. I'm not really sure of the purpose of validateField separate from fieldChanged if you can't
return false;
without issues.
👍 1
That said, I've hacked my way around it with something like the following:
Copy code
if ( validation === false ) {
	alert('Message to the user');
	
	var fieldObject = document.getElementById(fieldID);
	var fieldOnBlur = fieldObject.onblur;
	fieldObject.onblur = function () {};
	
	setTimeout(function() {
		fieldObject.focus();
		fieldObject.onblur = fieldOnBlur;
	},50);
	
	return false;
}
👍 2
m
Thanks @MTNathan
Maybe ill give that a go
m
In other situations I've set the field's value to a blank string and then I add a check in validateField to skip the validation if the field is empty, but that might not work depending on your situation (and it's definitely a worse user experience).
m
btw var fieldObject = document.getElementById(fieldID); would this be the field id from NS or do I get this from the html source code? got an erorr using NS ID - item_name is not defined
m
I've always gotten it to work using the NetSuite script ID of the field as a string - I realize now the latter part may have not been super clear. So something like
var fieldObject = document.getElementById('custpage_field_id');
or, if you're using this on multiple fields and want to create a loop or reusable function, you could use a variable like:
Copy code
var fieldID = 'custpage_field_id';
var fieldObject = document.getElementById(fieldID);
m
so I just tried getElementById('custage_custrecord_item_name') & getElementById('custrecord_item_name'). First one throws error: Cannot read property 'onblur' of null and nothing happens on the second. where do you find the ID? must it be field or lable ID?
m
Based on your example code earlier, I'd expect
document.getElementById('custrecord_item_name');
to work (you could probably just as easily pass in
context.fieldId
as a variable, especially if you want the code to be reusable), and if it's not throwing the 'can't find property' error I'd say that's a good sign. What does the rest of your code look like? Appearing like it's not doing anything might actually mean it's working, depending on what else your function's trying to do.
m
function validateField(context) {             var currentRecord = context.currentRecord;             var field_name = context.fieldId;             /**Screen Messages**/             var itame_name_exists_alert = {                 title: 'WARNING: Duplicate Name',                 message: 'Item Name exist, please choose another name.'             };             if (field_name == 'custrecord_item_name') {                 var item_name = currentRecord.getValue('custrecord_item_name');                 var item_exists = "test123"                 if (item_exists == item_name) {                     dialog.alert(itame_name_exists_alert);                     var fieldObject = document.getElementById('custrecord_item_name');                     var fieldOnBlur = fieldObject.onblur;                     fieldObject.onblur = function () { };                     setTimeout(function () {                         fieldObject.focus();                         fieldObject.onblur = fieldOnBlur;                     }, 50);                     return false;                 }             }             return true;         }
the return false doesn't work - acts off, the alert doesn't disappear
m
Strange, that looks right to me, it's pretty much identical to at least one of my applications. When you say the alert doesn't disappear, do you mean it just keeps popping up every time you click 'OK'? If so, that's odd because that's the specific behavior that this workaround has helped me avoid up to this point. If you really can't get it to work, as a potential alternative you could set the
custrecord_item_name
field to a blank string before returning false (and then skip the whole
onblur
workaround). It's definitely not as good of a user experience, but it's at least functional if the repeating popups are preventing you from deploying this code.
👍 1
m
yeah, it just keeps popping up and with each click the screen colour appears to change! strange. It's not the end of the world, I'll just send the alert as a warning and then do another check on save and prevent saving...