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

Danish Bhairag2
Tera Sage
Tera Sage

Hi @jonathangilbert ,

 

Can u trying putting an alert for all these variables n check if the values are coming properly or not

var amount= g_form.getValue('amount_in_document_currency');
alert(amount);
        var tax = g_form.getValue('vat_tax');
alert(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;
 
Thanks,
Danish
 

Hi Danish, I have put the alert in as you say and I am gettign nothing come  up on screen, so I dont knwo if that because for some reason it cant see the variable or the client scritp isnt executing again

@jonathangilbert 

 

Lets put an alert for newValue before the if condition in ur script & lets see if the alert comes two times or NOT.

Once when u select some value n other when u make it empty.

 

Thanks,

Danish

 

SunilKumar_P
Giga Sage

Hi @jonathangilbert, Your script looks, just make sure the two variables 'amount_in_document_currency' and 'vat_tax' are present on the form and are not null. I have optimized your script to below.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    if (newValue != '') {
        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);
        }
    }
}
 
Regards,
Sunil