Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to calculate Rent with VAT?

Raj90
Tera Guru

Hi All,

1. Rent with VAT = Rent w/o VAT * with Tax VAT

   find_real_file.png

2.   Rent with VAT = 550 * 7.7% = 592.00

 

   find_real_file.png

3. Rent with VAT = 550

     find_real_file.png

Anyone having knowledge on this please help me.

Thanks

Raj

1 ACCEPTED SOLUTION

H again,

The decimal separator i cannot manipulate. Thats the browser that determines that.

In Google Chrome i get:
find_real_file.png

And in Firefox i get:

find_real_file.png

Opposite group separators and decimal deparators
I have edited the script to take care of this but i cannot change the way its displayed

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	
	if(newValue == 'no'){ //Check the value of the "No" choice and adjust this if its not correct (maybe it "No" or something like that)
		
		g_form.setValue('u_rent_with_vat', g_form.getValue('u_rent'));
		
	}else{
		
		var decimal_separator = g_user_decimal_separator; //We get decimal separator
		var group_separator = g_user_grouping_separator; //Group separator - can be 1.000,50 as an example then the group separator is .
		
		var re = new RegExp(RegExp.quote(group_separator),"g"); //Because we cannot just replace . through regex then we need to add \ before the . char
		
		var rent = g_form.getValue('u_rent');
		//rent is a currency field so format is currency;amount : USD;50.50
		var currency = rent.split(';')[0]; //to get current currency
		
		rent = parseFloat(rent.split(';')[1].replace(re, '').replace(decimal_separator, '.'));
		var calculatedValue = ((rent / 100) * parseFloat(newValue)) + rent;
		
		g_form.setValue('u_rent_with_vat', currency + ';' + calculatedValue);
	}
	
}

RegExp.quote = function(str) {
	return str.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
	};
	

 

View solution in original post

14 REPLIES 14

Simon Christens
Kilo Sage

This is made with onChange on With tax (TVA)

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	
	var rent = g_form.getValue('u_rent');
	//rent is a currency field so format is currency;amount : USD;50
	var currency = rent.split(';')[0]; //to get current currency
        //If , (comma) is used then we need to change it to . (dot) so parseFloat doesnt ignore the number after the delimmiter
	rent = parseFloat(rent.split(';')[1].replace(',', '.'));
	var calculatedValue = ((rent / 100) * parseFloat(newValue)) + rent; 
	alert(calculatedValue);
	g_form.setValue('u_rent_with_vat', currency + ';' + calculatedValue);
	
}

Script updated

 

Simon Christens
Kilo Sage

And remember that the value of your choice field needs to be with . (dot) and not , (comma). (in the above example the value is 7.7)

I have chosen "include none" so theres no need of validate if the VAT field have a "no" in that case

 

Sorry, Simon

It's not working

Actually, With TAX VAT = 7,7% by mistake i put 7.7%

 

    Form:

    find_real_file.png

 

   Choice field:

find_real_file.png

 

 

onChange Client script:

find_real_file.png

 

 

The value of the choice should be changed from "VAT = 7.7%" to 7.7

Only the value. The label can be the same as now.
Remember that you calculate. You cannot calculate a value that is "VAT = 7.7%"

You also need to make sure that the other fields are correct. "u_rent" and "u_rent_with_vat"
Its the name of the fields you have created

find_real_file.png

Thanks so much, Simon for your supporting

Small issue in our code.

1. Vat = 7.7% .(dot)

    Rent = 250.00

    Rent with VAT = 250 * 7.7% = 269.25

 

Currently, I'm getting like in the below way

     find_real_file.png

     

find_real_file.png

In the below way we want

find_real_file.png

  

 

 2. Vat = No

     Rent =255.75

     Rent With VAT = 255.75

    

      Currently, I'm getting like in the below way

      find_real_file.png

 

  In the below way we want

   find_real_file.png

 

onChange Client script:

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


var rent = g_form.getValue('u_rent');
var currency = rent.split(';')[0]; 
rent = parseFloat(rent.split(';')[1].replace(',', '.'));
var calculatedValue = ((rent / 100) * parseFloat(newValue)) + rent;


g_form.setValue('u_rent_with_vat', currency + ';' + calculatedValue);


}

 

Thanks

Raj