Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Restrict if any field value starting with apart from 7

ARAVINDA11
Tera Contributor

@Ankur Bawiskar  @Community Alums  @Sohail Khilji @Maik Skoddow ,

 

Guys please help me

In a field I will be having series number starting with 7*

Example

7456

7869

7789

 

if field number starting with 7 series then only record should be created if not it should not create any record

 

help me with the script & where i can achieve this

3 REPLIES 3

SanjivMeher
Mega Patron
Mega Patron

You can use something like

var srNumber = srNumber.toString();

if (srNumber.charAt(0) === '7')

{

//proceed;

}


Please mark this response as correct or helpful if it assisted you with your question.

SAI VENKATESH
Kilo Patron
Kilo Patron

Hi @ARAVINDA11 

 

You can try the below script OnSubmit

 

   function onSubmit() {
    // Replace 'field_name' with the actual field name you want to check
    var fieldValue = g_form.getValue('description');

    // Check if the field value starts with '7'
    if (!fieldValue.startsWith('7')) {
        alert('The field value must start with 7.');
        return false; // Cancel form submission
    }
    return true; // Allow form submission

   
}

 

 

Thanks and Regards

Sai Venkatesh

VEEN PRANEETH K
Tera Contributor

@ARAVINDA11 

You can use before Business Rule to achieve the functionality.

VEENPRANEETHK_0-1717527557586.png

Script:

(function executeRule(current, previous /*null when async*/) {
    // Get the value of the field that should start with '7'
    var numberFieldValue = current.numberField;

    // Check if the value starts with '7'
    if (numberFieldValue && !numberFieldValue.startsWith('7')) {
        // If it does not start with '7', prevent the record from being inserted
        gs.addErrorMessage('Record cannot be created because the number does not start with 7.');
        current.setAbortAction(true);
    }
})(current, previous);
 
Note: Replace "numberField" with your field name you are trying to validate for.
 
Thanks