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.

Prefix validation

rajibosss11
Tera Contributor

I have a Choice filed called 

Version : When i select version is v1 then Version name should start with New_
If version :When i select version is V2 then Version name Should start with Old_

 

here version_name is String filed 

and version field is choice 

 

3 REPLIES 3

Laveena-Agarwal
Kilo Sage

Hi @rajibosss11 

 

Share your script what you have written so far and will help you to fix it.

Vishal_Jaiswal
Mega Guru

Hi @rajibosss11 ,

Please follow the steps below to get the solution.

 

- Create an onChange client script on the version variable.

- Use the script below

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    // 1. Get the current value of the target field
    var currentName = g_form.getValue('version_name');

    // 2. logic to remove existing prefixes so we don't get "New_Old_Name"
    // This cleans the string first
    var cleanName = currentName.replace('New_', '').replace('Old_', '');

    // 3. Apply the new prefix based on selection
    // IMPORTANT: Ensure 'v1' and 'V2' match the 'Value' column in your Choice List, not just the Label.
    if (newValue == 'v1') {
        g_form.setValue('version_name', 'New_' + cleanName);
    } 
    else if (newValue == 'V2') {
        g_form.setValue('version_name', 'Old_' + cleanName);
    }
}

 

Regards,

Vishal

Deepak Shaerma
Kilo Sage
Kilo Sage

hi @rajibosss11 

You need an onchange Client Script for this Requirement, 
Type: onChange
Field name: Version

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    // Get the current value of the Version Name field
    var versionName = g_form.getValue('version_name');

    // Logic for V1
    if (newValue == 'v1') {
        // Only set it if it doesn't already start with New_
        if (versionName.indexOf('New_') !== 0) {
            g_form.setValue('version_name', 'New_' + versionName); 
            // Note: logic above appends New_ to existing text. 
            // If you want to CLEAR the field and just put New_, use:
            // g_form.setValue('version_name', 'New_');
        }
    }

    // Logic for V2
    if (newValue == 'v2') {
        // Only set it if it doesn't already start with Old_
        if (versionName.indexOf('Old_') !== 0) {
             g_form.setValue('version_name', 'Old_' + versionName);
        }
    }
}


 

Happy to help! If this resolved your issue, kindly mark it as the correct answer   and Helpful and close the thread 🔒 so others can benefit too.

Warm Regards,

Deepak Sharma

Community Rising Star 2025