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

Runjay Patel
Giga Sage

Hi @Imran1 ,

use below code.

(function executeRule(current, previous /*null when async*/) {
// Ensure `total_app_budget` and `total_app_expense` are numeric values
var currencyCode = current.total_app_budget.getCurrencyCode();
var totalBudget = parseFloat(current.total_app_budget); // Convert to number if needed
var totalExpense = parseFloat(current.total_app_expense); // Convert to number if needed

// Calculate total and ensure the currency is included
var total = totalBudget + totalExpense;

// Set the value for the 'not_to_exceed_amount' field in the format: currency;total
current.not_to_exceed_amount.setValue(currencyCode + ";" + total.toFixed(2)); // Use toFixed for two decimal places
})(current, previous);

Accept the solution if it helped

Still, the aggregated result shows in Yaun

Even after using your supplied code

(function executeRule(current, previous /*null when async*/) {
// Ensure `total_app_budget` and `total_app_expense` are numeric values
var currencyCode = current.total_app_budget.getCurrencyCode();
var totalBudget = parseFloat(current.total_app_budget); // Convert to number if needed
var totalExpense = parseFloat(current.total_app_expense); // Convert to number if needed

// Calculate total and ensure the currency is included
var total = totalBudget + totalExpense;

// Set the value for the 'not_to_exceed_amount' field in the format: currency;total
current.not_to_exceed_amount.setValue(currencyCode + ";" + total.toFixed(2)); // Use toFixed for two decimal places
})(current, previous);

 

 

Sorry: Shows in USD, It should show in Yaun Currency and the actual value aggregated value should be 449,375.84

Sorry: Shows in USD, It should show in Yaun Currency and the actual value aggregated value should be 449,375.84