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

Ankur Bawiskar
Tera Patron
Tera Patron

@anushadande1793 

both the variables are editable and of type string?

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

 

Yes, Ankur

 

Regards,

Anusha

@anushadande1793 

check script shared below for onChange on Accum

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

Hi Ankur,

It is not working and allowing users to enter positive values and greater values. Can you please verify once and help me . Also, showing some javascript error pop up on browser

 

anushadande1793_0-1735544200668.png

 

Below script is working to allow negative numbers but we need to allow less values as well than cost field value

var cost=parseFloat(g_form.getValue('accum_to_retire'));
    var total = Math.round(parseFloat(cost * 100));
    var div=parseFloat(total/100);
    g_form.setValue('accum_to_retire',div);
    // var regex = /^-\d+|0\.\d\d$/;
   
    var regex1 = /^(?:0(?:\.0+)?|-\d+(?:\.\d+)?)$/;

    if (!regex1.test(div)){

        g_form.showFieldMsg('accum_to_retire','Only Negative Numbers are allowed',Error);
        //g_form.addInfoMessage("Only Negative Numbers are allowed");
        //alert("Only Negative Numbers are allowed");

        g_form.clearValue('accum_to_retire');

 

 

Thanks & Regards,

Anusha