|
| 1 | +(function executeRule(current, previous /*null when async*/) { |
| 2 | + |
| 3 | + // Extract the first 3 characters of the budget currency code (e.g., "INR", "EUR") |
| 4 | + var currencyCode = current.budget_currency ? current.budget_currency.toString().substring(0, 3) : ''; |
| 5 | + |
| 6 | + // Convert the annual budget value to a float |
| 7 | + var amount = parseFloat(current.annual_budget);//annual_budget is filed where we can enter amount |
| 8 | + |
| 9 | + // Validate input: If currency code is missing or amount is not a valid number, clear the USD field and exit |
| 10 | + if (!currencyCode || isNaN(amount)) { |
| 11 | + current.u_annual_budget_usd = ''; |
| 12 | + return; |
| 13 | + } |
| 14 | + |
| 15 | + // If the currency is already USD, no conversion needed — store the original amount |
| 16 | + if (currencyCode === 'USD') { |
| 17 | + current.u_annual_budget_usd = amount; |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + // Check if the currency exists in the fx_currency table |
| 22 | + var currencyGR = new GlideRecord('fx_currency'); |
| 23 | + currencyGR.addQuery('code', currencyCode); |
| 24 | + currencyGR.query(); |
| 25 | + |
| 26 | + // If currency is not found, clear the USD field and exit |
| 27 | + if (!currencyGR.next()) { |
| 28 | + current.u_annual_budget_usd = ''; |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + // Get the latest exchange rate for the selected currency from fx_rate table |
| 33 | + var fxGR = new GlideRecord('fx_rate'); |
| 34 | + fxGR.addQuery('currency.code', currencyCode); |
| 35 | + fxGR.orderByDesc('sys_updated_on'); // Sort by most recent update |
| 36 | + fxGR.setLimit(1); // Limit to the latest record |
| 37 | + fxGR.query(); |
| 38 | + |
| 39 | + // If no exchange rate found, clear the USD field and exit |
| 40 | + if (!fxGR.next()) { |
| 41 | + current.u_annual_budget_usd = ''; |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + var rate = parseFloat(fxGR.getValue('rate')); // Exchange rate for selected currency |
| 46 | + |
| 47 | + // Get the latest exchange rate for USD from fx_rate table |
| 48 | + var fxGR1 = new GlideRecord('fx_rate'); |
| 49 | + fxGR1.addQuery('currency.code', 'USD'); |
| 50 | + fxGR1.orderByDesc('sys_updated_on'); // Sort by most recent update |
| 51 | + fxGR1.setLimit(1); // Limit to the latest record |
| 52 | + fxGR1.query(); |
| 53 | + |
| 54 | + // If no USD exchange rate found, clear the USD field and exit |
| 55 | + if (!fxGR1.next()) { |
| 56 | + current.u_annual_budget_usd = ''; |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + var usdRate = parseFloat(fxGR1.getValue('rate')); // USD base rate |
| 61 | + |
| 62 | + // Perform conversion only if both rates are valid and non-zero |
| 63 | + if (!isNaN(rate) && !isNaN(usdRate) && rate !== 0) { |
| 64 | + var convertedAmount = (amount / rate) * usdRate; // Convert to USD |
| 65 | + current.u_annual_budget_usd = convertedAmount; // Store the converted value |
| 66 | + } else { |
| 67 | + gs.info("Invalid exchange rate values"); |
| 68 | + current.u_annual_budget_usd = ''; |
| 69 | + } |
| 70 | + |
| 71 | +})(current, previous); |
| 72 | +`` |
0 commit comments