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 sort Key value pair array

sainath3
Giga Guru

Hi Team,

 

Table 1:

 

NameID
Bachu Sainath101

 

Table 2:

IDAmountCurrencyBranch
10120USDDelhi
10180GBPPune
10130EURBangalore
10110USDHyd

Requirement:

1.Amount should convert to EUR, then, display Branch with Amount in Descending order (Amount):

USD to EUR: 1.09

GBP to EUR: 0.85

 

Expected Output in Table 1:

 

NameIDBranch1Amount1Branch2Amount2Branch3Amount3Branch4Amount4
Bachu Sainath101Bangalore70Pune68Delhi22Hyd11

 

My code:

var branch,amount;

var amountForbranch={ };

var gr=new GlideRecord('table1');

gr.addQuery('ID','101');

gr.query();

if(gr.next()){

var gr1=new GlideRecord('table2');

gr1.addQuery('ID',gr.id);

gr1.query();

while(gr1.next()){

var lineamount = covert_currency(gr1.u_currency, 'EUR', gr1.amount);//Convert Amount to EUR

amount=lineamount.toString();

branch=gr1.branch.toString();

amount=gr1.lineamount.toString();

amountForbranch[branch]=amount;

}

for(var key in amountForbranch){
gs.info(" key is " + key + " amountForbranch[key] " + amountForbranch[key]);

}

}

 

function covert_currency(from_curr, to_curr, amount) {
var converted_amount;
if (from_curr == 'EUR') {
return amount;
}
var CurrConv = new GlideRecord('fx_rate');
CurrConv.addQuery('currency.code', to_curr);
CurrConv.orderByDesc('sys_created_on');
CurrConv.query();
while (CurrConv.next()) {
converted_amount = CurrConv.u_conversion_rate * amount;
}
return converted_amount.toFixed(2);
}

Actual Output is:

key is Delhi amountForbranch[key] 22
key is Pune amountForbranch[key] 68
key is Bangalore amountForbranch[key] 70
key is Hyd amountForbranch[key] 11

 expected output is:

 key is Bangalore amountForbranch[key] 70
key is Pune amountForbranch[key] 68
key is Delhi amountForbranch[key] 22
key is Hyd amountForbranch[key] 11

Please help me how to sort Amount(After converting to EUR) & Branch in Descending order.

6 REPLIES 6

Whats the error you are receiving?

Hi

 

var amountForbranchArray = Object.entries(amountForbranch);// Undefined