Script to calculate quantity with price and then get total price?

Luis Paulo1
Tera Contributor
Do I need to create a script or use something I have in ServiceNow to develop a way to calculate the quantity of the product times the unit value to arrive at the total value?

Someone managed to help me with some logic, follow the images of where I'm going to carry out this development
1 ACCEPTED SOLUTION

Murthy Ch
Giga Sage

Try this code :

But u have to write this code for 2 fields.

Then copy the value in 3rd field.

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

    if (newValue != oldValue && newValue != '') {



        var f1 = g_form.getValue('value1'); // give quantity name

        var f2 = g_form.getValue('value2'); //give value name

        if (f1 != '' && f2 != '') {

            g_form.setValue('result', parseInt(f1) * parseInt(f2)); //give total name

        }

    }
}

Many thanks

Thanks,
Murthy

View solution in original post

4 REPLIES 4

Murthy Ch
Giga Sage

@Luis Paulo 

You need to write the on change() client script in order to achieve this.

 

Many thanks

Thanks,
Murthy

I see in the picture that the value field is in read-only 

So how will you enter the value in that field?

 

Thanks,
Murthy

Murthy Ch
Giga Sage

Try this code :

But u have to write this code for 2 fields.

Then copy the value in 3rd field.

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

    if (newValue != oldValue && newValue != '') {



        var f1 = g_form.getValue('value1'); // give quantity name

        var f2 = g_form.getValue('value2'); //give value name

        if (f1 != '' && f2 != '') {

            g_form.setValue('result', parseInt(f1) * parseInt(f2)); //give total name

        }

    }
}

Many thanks

Thanks,
Murthy

Thank you very much, your logic worked perfectly,
the only problem I'm having is to add all the values ​​added in multirow, because I can't set the value in a field outside of multirow,
can you help me with what I can do,
follow the images so you can understand.