How to sort Key value pair array
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-16-2023 08:02 AM
Hi Team,
Table 1:
Name | ID |
Bachu Sainath | 101 |
Table 2:
ID | Amount | Currency | Branch |
101 | 20 | USD | Delhi |
101 | 80 | GBP | Pune |
101 | 30 | EUR | Bangalore |
101 | 10 | USD | Hyd |
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:
Name | ID | Branch1 | Amount1 | Branch2 | Amount2 | Branch3 | Amount3 | Branch4 | Amount4 |
Bachu Sainath | 101 | Bangalore | 70 | Pune | 68 | Delhi | 22 | Hyd | 11 |
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2023 03:50 PM
Whats the error you are receiving?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2023 11:27 AM
Hi
var amountForbranchArray = Object.entries(amountForbranch);// Undefined