- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 03:54 AM
HI Community,
I have requirement in catalog form we have fields currency, amount and amount in eur. based on the currency and amount selected data will convert and populate in amount in eur variable. amount in eur value is showing as 648899.73 it should be 648 899.73
i need the number format should include a space separator for thousands, in addition to the dot for decimals - for example: 1 000.00 10 000.00 100 000.00 1 000 000.00
onchange client script:
variable name : amount
script include:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 04:13 AM
Hi @suuriyas
Inyou script include before returning the value just use the below regex pattern.
var Currency_Conversion_Utils = Class.create();
Currency_Conversion_Utils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
conversionforNewPaymentForms: function() {
var u_curr = this.getParameter('sysparm_currency');
var u_amt = this.getParameter('sysparm_amount');
var fxcurrency = new GlideRecord('fx_currency');
fxcurrency.addQuery('sys_id', u_curr);
fxcurrency.query();
if (fxcurrency.next()) {
var fxrate = new GlideRecord('fx_rate');
fxrate.addQuery('currency', fxcurrency.getUniqueValue());
fxrate.orderBy('currency');
fxrate.addEncodedQuery("currency.active=true^sys_created_onONYesterday@javascript:gs.daysAgoStart(1)@javascript:gs.daysAgoEnd(1)");
fxrate.query();
if (fxrate.next()) {
var rate = fxrate.rate;
var amtineuro = u_amt / rate;
var roundValue = parseFloat(amtineuro).toFixed(2);
// ---------------START----------------------------------
var str = roundValue.toString();
return str.replace(/\B(?=(\d{3})+(?!\d))/g, " ");
//-----------------END-----------------------------------
} else {
return "Exchange not found";
}
} else {
return "Exchange not found";
}
}
});
Hope this helps.
Regards,
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 04:05 AM
Hello @suuriyas,
Please ensure you are using String variable for the "amount in eur" and adjust your client script by adding a new function to format your number and use it when setting the value as follow:
function doSomething(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('amount_in_eur', formatNumber(answer)); //Setting the formatted number
}
function formatNumber(num) {
if (typeof num !== "number") {
return "Invalid input";
}
var parts = num.toString().split(".");
var integerPart = parts[0];
var decimalPart = parts.length > 1 ? "." + parts[1] : "";
var formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, " ");
return formattedInteger + decimalPart;
}
I have tested the formatNumber format and it is working as expected:
gs.log(formatNumber(648899.73)); // Output: "648 899.73"
gs.log(formatNumber(123456789)); // Output: "123 456 789"
What I did not get is "1 000.00 10 000.00 100 000.00 1 000 000.00". Could you explain what are the requirements in here?
If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 04:22 AM
HI @Medi C ,
I tried the script but it is showing as invalid
Amount in eur variable is single line text only, these are just an examples 1 000.00 10 000.00 100 000.00 1 000 000.00 for example if the value is thousand then it should be like 1 000.00 and not like 1000.00 similarly it should be like 10 000.00 and not like 10000.00
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 04:07 AM
Hello @suuriyas
You don't need all this scripting.
Convert that field to string. And under the Type Specification you would be having Validation regex.
Create a new Validation regex with below 👇 patter -
(?<!\d)(\d{1,3})(?=(\d{3})+(?:\.\d{2})?\b)
Please try and let me know if modifications needed I will make it.
Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket.
Regards,
Shivalika
My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 04:13 AM
Hi @suuriyas
Inyou script include before returning the value just use the below regex pattern.
var Currency_Conversion_Utils = Class.create();
Currency_Conversion_Utils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
conversionforNewPaymentForms: function() {
var u_curr = this.getParameter('sysparm_currency');
var u_amt = this.getParameter('sysparm_amount');
var fxcurrency = new GlideRecord('fx_currency');
fxcurrency.addQuery('sys_id', u_curr);
fxcurrency.query();
if (fxcurrency.next()) {
var fxrate = new GlideRecord('fx_rate');
fxrate.addQuery('currency', fxcurrency.getUniqueValue());
fxrate.orderBy('currency');
fxrate.addEncodedQuery("currency.active=true^sys_created_onONYesterday@javascript:gs.daysAgoStart(1)@javascript:gs.daysAgoEnd(1)");
fxrate.query();
if (fxrate.next()) {
var rate = fxrate.rate;
var amtineuro = u_amt / rate;
var roundValue = parseFloat(amtineuro).toFixed(2);
// ---------------START----------------------------------
var str = roundValue.toString();
return str.replace(/\B(?=(\d{3})+(?!\d))/g, " ");
//-----------------END-----------------------------------
} else {
return "Exchange not found";
}
} else {
return "Exchange not found";
}
}
});
Hope this helps.
Regards,
Siva