π§© Overview
In many Joget applications, users enter transaction amounts in different currencies. To maintain financial consistency, itβs crucial to automatically convert these values into a standard base currency (like AED or USD).
This guide shows how to automate currency conversion inside a Joget form using JavaScript, Fetch API, and jQuery Autocomplete β all without writing any backend plugin code.
βοΈ How It Works
β On form load:
- The script sets the AED amount field to zero.
 - Fetches live currency symbols from a safe exchange rate API.
 - Activates autocomplete on the currency input field.
 
βοΈ Whenever a user changes:
- The currency,
 - The amount, or
 - The product name,
 
the script recalculates the AED value dynamically and updates the related fields (amountInAed, BalanceAmount, and exchangeRate).  
π» Full Code
<script>
  $(document).ready(function () {
    // Initialize amount field
    $('#amountInAed').val("0");
    let currency = "";
    // Fetch all currency symbols from a safe API endpoint
    fetch(`https://your-safe-api-endpoint/v4/latest/AED`)
      .then((data) => data.json())
      .then((data) => {
        currency = Object.keys(data.rates);
        // Enable autocomplete for currency input field
        $('#CURRENCY').autocomplete({
          source: currency
        });
        // (Optional) Populate dropdown instead of autocomplete
        // currency.forEach((data) => {
        //   $("select[name='CURRENCY']").append(`<option value=${data}>${data}</option>`);
        // });
      });
    // Trigger conversion whenever key fields change
    $("input[name='CURRENCY'], input[id='AMOUNT'], select[name=productName]").on("change keyup click", function () {
      JogetgetResults();
    });
    // Fetch conversion rates and update results
    function JogetgetResults() {
      fetch(`https://your-safe-api-endpoint/v4/latest/USD`)
        .then(currency => currency.json())
        .then(JogetdisplayResults);
    }
    // Perform conversion and update fields
    function JogetdisplayResults(currency) {
      var fromRateValue = $('input[name="CURRENCY"]').val();
      let total = 0;
      let fromRate = currency.rates[fromRateValue];
      let toRate = currency.rates['AED'];
      total += toRate / fromRate * $("input[id='AMOUNT']").val();
      total = Number.isNaN(total) ? 0 : total;
      // Update converted amount fields
      $("input[id='amountInAed']").val(total);
      $("input[id='amountInAed1']").val(total.toLocaleString('en-IN'));
      // Update balance fields
      $('#BalanceAmount').val((parseFloat($('[name=rembudgetAmount]').val() || 0) - total));
      $('#BalanceAmount1').val((parseFloat($('[name=rembudgetAmount]').val() || 0) - total).toLocaleString('en-IN'));
      // Display exchange rate information
      let DividedValue = Number.isNaN(toRate / fromRate) ? 0 : (toRate / fromRate).toFixed(3);
      $('#exchangeRate').val(`1 ${fromRateValue} = ${DividedValue} AED`);
      $('#exchangeRate1').val(`1 ${fromRateValue} = ${DividedValue} AED`);
    }
  });
</script>
π§ Example Use Cases
β
 Expense Forms β Automatically convert foreign expenses into AED.
β
 Quotation Requests β Display AED equivalent of multi-currency offers.
β
 Budget Management β Show live exchange rate for spending insights.  
π Customization Tips
π‘ Replace "AED" and "USD" with your preferred base and target currencies.
π‘ You can use a dropdown instead of autocomplete by uncommenting the related lines.
π‘ Adjust toLocaleString('en-IN') for your region (e.g., 'en-US' for US format).  
π Key Benefits
β
 Fully client-side β no backend logic needed.
β
 Reduces human error in currency entry.
β
 Provides real-time and transparent conversion.
β
 Easy to extend or modify for multiple targets.  
π Security Note
Always use a trusted and HTTPS-secured API endpoint to fetch exchange rates.
Avoid exposing sensitive API keys directly in client-side code.  
π Final Thoughts
This lightweight JavaScript solution empowers Joget users to manage real-time currency conversion directly in forms β boosting both usability and data accuracy.
    
Top comments (0)