I want the currency field on my catalog item form.

dhineshkumar
Tera Guru

Hi Experts.

How to make hte currency field on my catalog form using onchange client script??

Example: If I enter 3863(any value) should be convert as $3,863.00. How to write the script using onchange method.

1 ACCEPTED SOLUTION

Shripal
Tera Expert

Hi,

You will have to use 2 Single line text variables to achieve this on the catalog item. One variable will be visible to the users on the portal and the other one will be hidden. In my example I have used "Currency" (name - currency, type - Single line Text) and "Currency Hidden" (name - currency_hidden, type - Single line Text).

I wrote an onChange Catalog Client script on the "currency" field: 

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	// My Field Names (you can change as per your value)
	var field_name = 'currency';
	var field_name_hidden = 'currency_hidden';

	if (g_form.getValue(field_name_hidden)) {
		g_form.clearValue(field_name_hidden);
		return;
	}
	
	// Check if the entered value is a valid Floating point value or not
	var reg = /^[+]?\d+(\.\d+)?$/;
	var valid = reg.test(newValue);
	if (!valid) {
		g_form.clearValue(field_name);
		alert("Please enter valid floating point numbers.");
		return;
	}

	var new_value = newValue;
	new_value = parseFloat(new_value).toFixed(2);      // Get the floating point value and modify the the amount to only 2 decimal points
	var amount = new_value.split(".");                 // Split the integer and float part of the amount
	var int_var = amount[0];
	var float_var = amount[1];
	var string = "0";

    var reverse = int_var.split('').reverse().join('');
    while (reverse != '') {
        var cut = reverse.substring(0,3);
        reverse = reverse.substring(3);
        cut = cut.split('').reverse().join('');
        if (string == "0") string = cut;
		else string = cut + "," + string;
    }

	// Add "$" and the float part to the integer and make the final string
	string = "$" + string + "." + float_var;
	
	// Set this field value in the hidden field
	// This will be used when this onChange code will be called again
	// we will check if the hidden field has value or not. (Line 10 if this script)
	// If the hidden field has any value that means that this onChange code should not run anymore. (Line 11, 12 of this script)
	// If the hidden field is blank, then only we will run this code completely.
	g_form.setValue(field_name_hidden, string);
	
	// Set the string to the field. - This will eventually make this code run again because the variable value has been changed. So this onChange script wil run again
	// But since the hidden variable has also this value, this code will not run again.
	g_form.setValue(field_name, string);
}

 

This will finally look like this: (Keeping "Currency Hidden" variable NOT hidden for complete visibility) 

1. When the value is entered

2. The final response after the script runs:

find_real_file.png

View solution in original post

13 REPLIES 13

Valmik Patil1
Kilo Sage

Hi,

Please find find below thread which has solution for this.

https://community.servicenow.com/community?id=community_question&sys_id=962a8f69db5cdbc01dcaf3231f96...

Thanks,

Valmik Patil

Shripal
Tera Expert

Hi,

You will have to use 2 Single line text variables to achieve this on the catalog item. One variable will be visible to the users on the portal and the other one will be hidden. In my example I have used "Currency" (name - currency, type - Single line Text) and "Currency Hidden" (name - currency_hidden, type - Single line Text).

I wrote an onChange Catalog Client script on the "currency" field: 

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	// My Field Names (you can change as per your value)
	var field_name = 'currency';
	var field_name_hidden = 'currency_hidden';

	if (g_form.getValue(field_name_hidden)) {
		g_form.clearValue(field_name_hidden);
		return;
	}
	
	// Check if the entered value is a valid Floating point value or not
	var reg = /^[+]?\d+(\.\d+)?$/;
	var valid = reg.test(newValue);
	if (!valid) {
		g_form.clearValue(field_name);
		alert("Please enter valid floating point numbers.");
		return;
	}

	var new_value = newValue;
	new_value = parseFloat(new_value).toFixed(2);      // Get the floating point value and modify the the amount to only 2 decimal points
	var amount = new_value.split(".");                 // Split the integer and float part of the amount
	var int_var = amount[0];
	var float_var = amount[1];
	var string = "0";

    var reverse = int_var.split('').reverse().join('');
    while (reverse != '') {
        var cut = reverse.substring(0,3);
        reverse = reverse.substring(3);
        cut = cut.split('').reverse().join('');
        if (string == "0") string = cut;
		else string = cut + "," + string;
    }

	// Add "$" and the float part to the integer and make the final string
	string = "$" + string + "." + float_var;
	
	// Set this field value in the hidden field
	// This will be used when this onChange code will be called again
	// we will check if the hidden field has value or not. (Line 10 if this script)
	// If the hidden field has any value that means that this onChange code should not run anymore. (Line 11, 12 of this script)
	// If the hidden field is blank, then only we will run this code completely.
	g_form.setValue(field_name_hidden, string);
	
	// Set the string to the field. - This will eventually make this code run again because the variable value has been changed. So this onChange script wil run again
	// But since the hidden variable has also this value, this code will not run again.
	g_form.setValue(field_name, string);
}

 

This will finally look like this: (Keeping "Currency Hidden" variable NOT hidden for complete visibility) 

1. When the value is entered

2. The final response after the script runs:

find_real_file.png

Much Appreciation, You helped me out and fulfill my requirement, I need suggestion how to implement this kind of stuff. 

Its is working fine for integer part .But if I give like 10000 and its giving $1.How to include zeros in total amount.