Using Condition String field type

lberthold
Kilo Contributor

Hello -

I wanted to create a custom string field on the Contract table that would follow a specific syntax...for example, the number of characters in the field should equal 8, and the first two should start with FA, while the remaining six characters should be numbers (e.g., FA123456).

Could I use a Condition String field type to achieve this? I'm just not sure how to validate the syntax though...

Any ideas?

Thansk!

Loir

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi Lori,



You would just use a string field here, but then apply a Client Script to that field to enforce the specific syntax with a regular expression (regex). I have an example that checks that the appropriate number of characters have been entered, and formats appropriately or lets the user know if they did not input properly. I've made a slight modification here to match your description. What it essentially does is checks for the format you want. If it does not match, is strips out all the non-digits to see if there are 6 digits. If there are, then it puts them in the appropriate format with FA at the front. If not, it posts a message letting the user know what format to enter. You would need to replace FIELDNAME with the field you are using. I think this will do what you are looking for.



Ben



function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading || newValue == '') {


  return;


  }



  var testString = '';


  testString = g_form.getValue('FIELDNAME);


  var testFieldFormat = /^FA[0-9]{6}$/; // Pattern for appropriate format



  // Check for match to format, if it does not match, strip non-digit characters and apply format


  if(!testFieldFormat.test(testString)){


  testString = testString.replace(/[^\d]/g,'');


  var testDigitsPattern = /^[0-9]{6}$/; // Determine if six numbers entered



  if(!testDigitsPattern.test(testString)){


  // g_form.clearValue('FIELDNAME');   // Uncomment this if you want to clear the value


  g_form.showFieldMsg('FIELDNAME', "Invalid Field Value.   Please enter the 6 digit FA code in the form FA######",'error');


  }


  if(testDigitsPattern.test(testString)){


  g_form.hideAllFieldMsgs();


  newNum = 'FA' + testString;


  g_form.setValue('FIELDNAME',newNum);


  }


  }



  if(testFieldFormat.test(testString)){


  g_form.hideAllFieldMsgs();


  }


}


View solution in original post

6 REPLIES 6

jake_mckenna
ServiceNow Employee
ServiceNow Employee

The best way to achieve what you are looking for is to use the number maintenance setup, but since there is already one for the number field you will not be able to add another. Another option would be to setup a system property that could act as your counter starting from whatever number you would like and you could just do a gs.getProperty() to get the next value.



When you get to the point of populating the field for a record the best way would most likely be a business rule that looks up the property and generates the string like in your example. The logic could be constructed there and populated on an insert of a new record.


Community Alums
Not applicable

Hi Lori,



You would just use a string field here, but then apply a Client Script to that field to enforce the specific syntax with a regular expression (regex). I have an example that checks that the appropriate number of characters have been entered, and formats appropriately or lets the user know if they did not input properly. I've made a slight modification here to match your description. What it essentially does is checks for the format you want. If it does not match, is strips out all the non-digits to see if there are 6 digits. If there are, then it puts them in the appropriate format with FA at the front. If not, it posts a message letting the user know what format to enter. You would need to replace FIELDNAME with the field you are using. I think this will do what you are looking for.



Ben



function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading || newValue == '') {


  return;


  }



  var testString = '';


  testString = g_form.getValue('FIELDNAME);


  var testFieldFormat = /^FA[0-9]{6}$/; // Pattern for appropriate format



  // Check for match to format, if it does not match, strip non-digit characters and apply format


  if(!testFieldFormat.test(testString)){


  testString = testString.replace(/[^\d]/g,'');


  var testDigitsPattern = /^[0-9]{6}$/; // Determine if six numbers entered



  if(!testDigitsPattern.test(testString)){


  // g_form.clearValue('FIELDNAME');   // Uncomment this if you want to clear the value


  g_form.showFieldMsg('FIELDNAME', "Invalid Field Value.   Please enter the 6 digit FA code in the form FA######",'error');


  }


  if(testDigitsPattern.test(testString)){


  g_form.hideAllFieldMsgs();


  newNum = 'FA' + testString;


  g_form.setValue('FIELDNAME',newNum);


  }


  }



  if(testFieldFormat.test(testString)){


  g_form.hideAllFieldMsgs();


  }


}


Hi, Ben -



This is really helpful, thanks very much for sharing!



Lori


Community Alums
Not applicable

You're welcome. Glad I could help.