Comparing two fields on service catalog form

anushadande1793
Tera Contributor

Hi 

 

Need help on below requirement. Can someone help on this?

we need to compare two fields and one field value shouldn't be exceeded and it should be negative always.

we have written onchange client script to allow only negative values but not able to compare with other field and restrict with less values. From the below screenshot, Accum should allow only less values than cost and always negative values. Any inputs really helps me alot

anushadande1793_0-1735541634201.png

 

 

Thanks & Regards,

Anusha

 

 

1 ACCEPTED SOLUTION

@anushadande1793 

I shared script below.

Sharing updated script again

try that and share your updates, it should be onChange on Accum variable

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

    g_form.hideFieldMsg('accum');
    // Ensure Accum is negative
    if (parseInt(newValue) >= 0) {
        g_form.showFieldMsg('accum_to_retire', 'Accum must be a negative value', 'error');
        g_form.clearValue('accum_to_retire');
        return;
    }

    // Compare Accum with Cost
    var cost = g_form.getValue('cost');
    if (parseFloat(newValue) >= parseFloat(cost)) {
        g_form.showFieldMsg('accum_to_retire', 'Accum must be less than Cost', 'error');
        g_form.clearValue('accum_to_retire');
    }
}

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

11 REPLIES 11

Rohit99
Mega Sage

Hi @anushadande1793 ,
You may try with the following Onchange Client script on Accum field

 

function onChange(control, oldValue, newValue, isLoading) {
    var accum = parseFloat(g_form.getValue('u_accum'));
    var cost = parseFloat(g_form.getValue('u_cost'));
    if (accum >= 0) {
        g_form.showFieldMsg('u_accum', 'Accum must be a negative value!', 'error');
        return;
    }
    if (accum >= cost) {
        g_form.showFieldMsg('u_accum', 'Accum must be less than Cost!', 'error');
        return;
    }
  g_form.hideFieldMsg('u_accum');
}

 

 

 

Please mark my response as correct and helpful if it helped solved your question.

 

Thanks,

Rohit Suryawanshi

Thank you All.

It helps me a lot.