Skip to main content

Form Field Display Messages

 function onSubmit() {

g_form.clearMessages(); var aFields = ["variable_1","variable_2"]; var aErrors = []; var iCount = 0; var oErrorMessage = {}; var iLength = aFields.length - 1; oErrorMessage.INVALID_MSG1 = "error 1"; oErrorMessage.INVALID_MSG2 = "error 2"; for (iCount=0;iCount <= iLength;iCount++){ var oError; var sFieldName = '' + aFields[iCount]; g_form.hideFieldMsg(sFieldName, true); //hide any previous messages if (<perform a validation check>){ oError = {}; oError.fieldName = sFieldName; oError.error = oErrorMessage.INVALID_MSG1; aErrors.push (oError); }else{ g_form.setValue(sFieldName,sTrimmedValue); } if (<perform a validation check 2>){ oError = {}; oError.fieldName = sFieldName; oError.error = oErrorMessage.INVALID_MSG2; aErrors.push (oError); }else{ g_form.setValue(sFieldName,sTrimmedValue); } } iLength = aErrors.length - 1; if (iLength >= 0){ g_form.addErrorMessage("There are errors on the form"); for (iCount=0;iCount <= iLength; iCount++){ var oCurrentError = aErrors[iCount]; g_form.showFieldMsg( oCurrentError.fieldName,oCurrentError.error,"error",true); } return false; //stop submit } }

Comments

Popular posts from this blog

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){

Get URL Parameter - server side script (portal or classic UI)

Classic UI : var sURL_editparam = gs . action . getGlideURI (). getMap (). get ( ' sysparm_aparameter ' ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); } Portal : var sURL_editparam = $sp . getParameter ( " sysparm_aparameter " ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); }