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

Saturn
Tera Contributor

Hi @Rohansalwi , 

 

I tried your script in my pdi with a catalog form. It is working as expected. Try debugging your script with alerts within 'if' condition to check if your client script is running or not. Also verify the backend names of field and choice values.

 

 

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.

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