Catalog Client Script

jonathangilbert
Kilo Sage

Happy Christmas ServiceNow Community 🙂

 

Can anyone point out where I am going wrong with this catalog client script:- 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
       return;
    }
    // UK VAT rate
    if (newValue == "56fd15578364359066b200c8beaad3d2") {
        g_form.setValue('tax_amount_in_document_currency', '0.0');
    }
    //if Supplier Fines is deselected original VAT amount should repopulate
    if (newValue != "56fd15578364359066b200c8beaad3d2") {
        var amount= g_form.getValue('amount_in_document_currency');
        var tax = g_form.getValue('vat_tax');
        var a = (tax *(amount/100));// calculates the value
        var round = a.toFixed(2);// callculates the value to nearest 2 decimal places
        var total = round;
        g_form.setValue('tax_amount_in_document_currency', total);
    }
 }
 
Basically if the value "56fd15578364359066b200c8beaad3d2" is selected in a variable then it should update another tax variable to "0.0" (which works), but if the user then decides to deselect the "56fd15578364359066b200c8beaad3d2" from the variable, the second half of the script should execute to recalculate the tax amount. The sceond half of the script is not working, the tax variable is still displaying "0".
1 ACCEPTED SOLUTION

Hi @jonathangilbert, I have got it working with slight modification as highlighted below.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }
        if (newValue == "56fd15578364359066b200c8beaad3d2") {
            g_form.setValue('tax_amount_in_document_currency', '0.0');
        }
        //if Supplier Fines is deselected original VAT amount should repopulate
        else {
            var amount = g_form.getValue('amount_in_document_currency');
            var tax = g_form.getValue('vat_tax');
            var a = (tax * (amount / 100)); // calculates the value
            var round = a.toFixed(2); // callculates the value to nearest 2 decimal places
            g_form.setValue('tax_amount_in_document_currency', round);
        }
}
 
I have tested it in my PDI, it is working. The script I used is as below:
 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }
    if (newValue == '62826bf03710200044e0bfc8bcbe5df1') { // Abel Tutor
        g_form.setValue('tax_amount', '0.0');
    } else {
        var amount = g_form.getValue('amount');
        var tax = g_form.getValue('tax');
        var a = (tax * (amount / 100)); // calculates the value
        var round = a.toFixed(2); // callculates the value to nearest 2 decimal places
        g_form.setValue('tax_amount', round);
    }
 
SunilKumar_P_0-1703759284442.pngSunilKumar_P_1-1703759298807.png

 

Regards,

Sunil

 

View solution in original post

11 REPLIES 11

Tai Vu
Kilo Patron
Kilo Patron

Hi @jonathangilbert 

It's because of these lines in your script. That means if new value is empty, it will return with no action.

 

if (isLoading || newValue === '') {
   return;
}

 

 

So you can remove the newValue condition.

 

if (isLoading) {
   return;
}

 

 

Or you can separate it to a new block to do something when the field comes to empty. (Ex: Clear value of some fields)

 

Cheers,

Tai Vu

Pavankumar_1
Mega Patron

Hi @jonathangilbert ,

i will suggest first you to add logs (g_form.addInfoMessage('test'))on the loops make sure those logs entering into the loops based the values that you have given . Once those logs working fine then just use your logic for calculation if you are able to do calculation then combine with your if conditions.

May be in this way you will get clear idea how it is working.

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar