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.

Unable to Sum the Yaun Value, the returned result shows in in USD

Imran1
Giga Guru

Hi All,

 

I have recently enabled Multi-Currency in my instance, I have both USD and Yaun as currencies. 

When I do the aggregation of two fields and try to set the value into another currency field I get the aggregate in USD instead of Yaun as displayed in my screenshot.

Below is my code which I am running in OnBefore BR (should I change it to run OnAfter)

(function executeRule(current, previous /*null when async*/ ) {

	var currencyCode = current.total_app_budget.getCurrencyCode();
	var total = current.total_app_budget + current.total_app_expense;
    current.not_to_exceed_amount.setValue(currencyCode + ";" + total);

})(current, previous);

 

 

I also tried to do something like the below using the background script still to no avail.

var current = new GlideRecord("table_name");
if (current.get("sys_id of the record")) {
    var currencyCode = current.total_app_budget.getCurrencyCode();
	gs.info(currencyCode +"CurrencyCode");



    var totalBudget = current.total_app_budget.getCurrencyDisplayValue();
	var totBud = trim(totalBudget.toString().split("¥").splice(1));
	totBud = (totBud/1);
	gs.info('FieldType1: '+typeof(totalBudget)+ ' Value: '+totBud);

    var totalExpense = current.total_app_expense.getCurrencyDisplayValue();
	var totExp = parseFloat(totalExpense.toString().replace("¥",""));

	gs.info('FieldType2: '+typeof(totalExpense) + ' Value2: '+totExp);
    var total = totBud + totExp;
	gs.info('Total & Type '+total + ' type: '+typeof(total));
    // current.not_to_exceed_amount.setValue(currencyCode + ";" + total);
}

 

Request your assistance to get the aggregation in the required currency and set the value in the field

Regards,

Imran

 

 

1 ACCEPTED SOLUTION

One of my colleague was able to sort it out for me 

 

(function executeRule(current, previous /*null when async*/) {

var currencyCode = current.total_app_budget.getCurrencyCode();
var totalBudget = parseFloat(current.total_app_budget.getCurrencyValue()); 
var totalExpense = parseFloat(current.total_app_expense.getCurrencyValue()); 
var total = totalBudget + totalExpense;

current.not_to_exceed_amount.setValue(currencyCode + ";" + total.toFixed(2)); // Use toFixed for two decimal places
})(current, previous);

. Thank You for your help @Runjay Patel 

 

View solution in original post

7 REPLIES 7

You need it in yaun right? What’s the value showing now?

The currency symbol/code shows in Yaun but the value is showing incorrect see the attached screenshot.

The Aggregate value should sum to = 449,375.84 but shows = 61,998.72

 

One of my colleague was able to sort it out for me 

 

(function executeRule(current, previous /*null when async*/) {

var currencyCode = current.total_app_budget.getCurrencyCode();
var totalBudget = parseFloat(current.total_app_budget.getCurrencyValue()); 
var totalExpense = parseFloat(current.total_app_expense.getCurrencyValue()); 
var total = totalBudget + totalExpense;

current.not_to_exceed_amount.setValue(currencyCode + ";" + total.toFixed(2)); // Use toFixed for two decimal places
})(current, previous);

. Thank You for your help @Runjay Patel