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

Hi Sunil, I have copied your script into my instance and the Tax variable does not recalculate, it still remains at 0.00

Hi @jonathangilbert, Do you have the variables 'amount_in_document_currency' and 'vat_tax' on form and has value in it before the newValue is deselected or changed?

 

Regards,

Sunil

Hi Sunil, yes there is values already in the 2 variables you mention before the newValue is deselected or changed.

The attached screenshot shows values that are present already

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

 

Sunil, you are a legend, that works perfectly, thankyou so much 🙂