Facing Issue in Business Rule

karan1061
Tera Contributor

 

 

In my After Business Rule, I’ve written a script to fetch values from the fx_currency_instance table.

When I run the same script in Background Script, it correctly logs:
Currency map: {"field_1":"5796.5","field_2":"23186"}

However, when I test it through the Business Rule, the log shows:
Currency map: {"field_1":"5000","field_2":"20000"}

It’s printing the amount field value from the fx_currency_instance table instead of fetching from the reference_amount field.

Here’s the script I used:

karan1061_0-1761312462465.png

 

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@karan1061 

what's your actual business requirement and share the script and not the image

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

 

Hi @Ankur Bawiskar,

Here’s my requirement:
I need to convert USD to EUR for a specific table (since the target currency may differ across tables).

So, I created a Business Rule that updates the currency type to EUR whenever a new record is created in that table.
Here’s the script I used — it works fine:

 

 
var gr = new GlideRecord('fx_currency_instance');
gr.addEncodedQuery('id=' + current.sys_id);
gr.query();
while (gr.next()) {
    gr.currency = 'EUR';
    gr.update();
}​

Now, there’s another use case — I also need to compare values from the same fields.
For this comparison, I’m fetching the reference_amount from the fx_currency_instance table.

But the issue is: when I compare, it returns the EUR values instead of the original USD values.

Example:

  • Expected (USD): Currency map: {"field_1":"5796.5","field_2":"23186"}

  • Actual (EUR): Currency map: {"field_1":"5000","field_2":"20000"}

Here’s the script I used for fetching:

var currencyMap = {};
var cur = new GlideRecord('fx_currency_instance');
cur.addQuery('id', current.getUniqueValue());
cur.addQuery('field', 'IN', 'field_1,field_2');
cur.query();

while (cur.next()) {
    var f = cur.getValue('field');
    var amt = cur.reference_amount.toString();
    currencyMap[f] = amt;
}
gs.info('Expense sys_id2= currency arr :' + JSON.stringify(currencyMap));

 

@karan1061 

try this

  • Fetch and store reference (USD) amounts before you update currency in the business rule.
  • Use that stored data for comparisons.
  • Once currency is changed to EUR, the amounts you fetch from those records will be converted.
// In your After Insert business rule

// 1. Fetch original reference_amount values before update or store them somewhere
var originalCurrencyMap = {};
var cur = new GlideRecord('fx_currency_instance');
cur.addQuery('id', current.sys_id);
cur.addQuery('field', 'IN', 'field_1,field_2');
cur.query();

while (cur.next()) {
    var fieldName = cur.getValue('field');
    var refAmt = cur.getValue('reference_amount');  // original USD
    originalCurrencyMap[fieldName] = refAmt;
}

// 2. Update currency to EUR
var gr = new GlideRecord('fx_currency_instance');
gr.addQuery('id', current.sys_id);
gr.query();
while (gr.next()) {
    gr.currency = 'EUR';
    gr.update();
}

// 3. Use originalCurrencyMap for further comparisons or logging
gs.info('Original expense amounts (USD): ' + JSON.stringify(originalCurrencyMap));

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader