Set a field value based on the first letter of another field

Rohansalwi
Tera Contributor

I would like to have a field value set to Billable if another field has a value that starts with E and Non-billable if that field has a value that starts with P,I,O. I tried below onChange script but it isn't working.

 

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

 

    // Get the first character of the new value
    var firstChar = newValue.charAt(0).toUpperCase();

 

    // Determine billable status
    if (firstChar === 'E') {
        g_form.setValue('billable_status', 'Billable');
    } else if (['P', 'I', 'O'].includes(firstChar)) {
        g_form.setValue('billable_status', 'Non-billable');
    } else {
        g_form.clearValue('billable_status'); //  clear if not matching
    }
}
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Rohansalwi 

Since this is for catalog item and the variable is of type Reference, you need to get the display value and then check the char

Something like this will work in native + portal

I created blog for this in past

Get Display value of reference variable Service Catalog 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    try {
        if (window == null) {
            var valuePortal = g_form.getDisplayValue('variableName');
            var firstChar = valuePortal.charAt(0).toUpperCase();
            if (firstChar === 'E') {
                g_form.setValue('billable_status', 'Billable');
            } else if (['P', 'I', 'O'].includes(firstChar)) {
                g_form.setValue('billable_status', 'Non-billable');
            }
        }
    } catch (ex) {
        var valueNative = g_form.getDisplayBox('variableName').value;
        var firstChar = valueNative.charAt(0).toUpperCase();
        if (firstChar === 'E') {
            g_form.setValue('billable_status', 'Billable');
        } else if (['P', 'I', 'O'].includes(firstChar)) {
            g_form.setValue('billable_status', 'Non-billable');
        }
    }
    //Type appropriate comment here, and begin script below
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

GlideFather
Tera Patron

Hi @Rohansalwi 

what is the field type where you will be checking the first character?

 

I don’t consider this as a good practice… from the design architecture it sounds very weak and i would encourage you to think of some better alternatives. 

I don’t know the details but if you are fine sharing them, do so and we cam discuss it 

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


The variable from where the script is reading the project code is a reference type field. I think it needs to be converted to a string first. I tried below as well.

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

 

    // Get the display value of the reference field
    var displayValue = g_form.getDisplayValue(control.getName());

 

    // Ensure displayValue is valid
    if (!displayValue) {
        g_form.clearValue('billable_non_billable');
        return;
    }

 

    // Get the first character and convert to uppercase
    var firstChar = displayValue.charAt(0).toUpperCase();

 

    // Set billable status based on first character
    if (firstChar === 'E') {
        g_form.setValue('billable_non_billable', 'Billable');
    } else if (['P', 'I', 'O'].includes(firstChar)) {
        g_form.setValue('billable_non_billable', 'Non-billable');
    } else {
        g_form.clearValue('billable_non_billable'); // Clear if no match
    }
}

Chaitanya ILCR
Kilo Patron

Hi @Rohansalwi ,

Try this

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    alert(newValue);
    var firstChar = newValue[0].toUpperCase();

    if (firstChar == 'E') {
        g_form.setValue('billable_status', 'Billable');
    } else if (['P', 'I', 'O'].includes(firstChar)) {
        g_form.setValue('billable_status', 'Non-billable');
    } else {
        g_form.clearValue('billable_status');
    }
}

 I have added an alert statement just wanted to check if the client script is even triggering 

 

If it's coming please check if you have selected the correct variable name, catalog name,  type of client script in the client script. Please share the screenshot of the client script of it's not throwing the alert 

 

 

Regards 

Chaitanya 

Also check if the billable_status variable choices are correct  you should be using the value of the choice not the label 

 

Regards 

Chaitanya