Need to add an Alert to a Reference Field when it shows the error "Invalid Reference"

ksmithdev
Mega Expert

Hi there,

 

I have a reference field that will be looking to make sure a number matches what is in the database. The thing I need to add, is that when the field can't match the input to the database/can't find the record, OOB service-now puts the red dotted line and a hover over message saying "Invalid Reference". When this happens, I need to figure out a way to put a g_form alert message to the field to say that the "Number is Invalid". I am guessing this has to be a client script.

 

I have looked all over the wiki, and what I did find (an observe.click function ordeal) did not work. Anyone else have a better idea?

7 REPLIES 7

PeterWiles
Kilo Sage

Little unsure what your trying to do.


You can add a reference qualifier to the reference field. This can just be a simple 'u_number!=null' or you can create a script include for it to look-up.



http://wiki.servicenow.com/index.php?title=Reference_Qualifiers



Pete


Or to do a look-up, you can use an AJAX call to do your database look-up and then display your error message.


GlideAjax - ServiceNow Wiki


No sorry. I pretty much need the field to validate the value against the table, and then when it doesn't find a match, error under the field.


Hi Kay,



This is a bit messy, but it should get you where you need to be. It's an onblur event handler we can put on an input field in ServiceNow. An onChange doesn't work because it won't fire if the newValue isn't valid. What ServiceNow does when the value is invalid is change the title attribute on the input field to Invalid reference, so the script looks for that. In order to personalize this change the 2 spots where I'm referencing caller_id on incident on lines 2 and 3.



function onLoad() {


      $('sys_display.incident.caller_id').onblur = function() {


              var fieldObj = $('sys_display.incident.caller_id');


              if (fieldObj.title.toLowerCase() == 'invalid reference') {


                      alert(fieldObj.title);


              }


      }


}



Edit: This needs to be in an onload client script on the table where the form is. In my case it was incident.