Adding a record that uses a dynamic currency in a different data object

I am having trouble building a form to add a record that uses a dynamic currency in a different data object.

I have a Region table, which has 2 fields - Region and Region Currency. I have a Product table that has 2 fields - Product Name and Aggregated Limit. Each Product record is related to a Region record. The Aggregated Limit field is monetary with a dynamic currency which is the ‘Region Currency’ in the related Region record.
Whenever I create a Product record, I want to set the Region (which determines the currency of the Aggregated Limit) and the Aggregated Limit.
I am struggling to find a way to get the currency prefix to display in the Aggregated Limit field as soon as I set the Region.
I can do it by saving the record in 2 stages (save after adding the Region, set the Aggregated Limit and then save again). But that is messy.
I have started building a callback on Aggregated Limit that is triggered when Region is updated, but haven’t got very far.
Can anyone help with this?

Hi Angela, one of our builders is going to take a look for you. He’ll be in contact shortly, I’ve given him your email address as he’ll need a few clarifications. When we have the answer we can post it in here to make sure everyone gets to see the response. Richard

1 Like

After some back and forth with the different options the following was agreed as the solution to the problem.

Posting here for others to see…

  • Make the adding of a product a 2 step process (just like you tried) – on the first page add the region to a product with submit button “Next” which will take you to the next page where you will see the prefix on your field.

  • On the second page of adding a product you should have a form with destination settings “Redirect to related record”, filled with the relationship from product to the object where you add products… This will ensure that when submitting the second part of the product information you will be redirected back to the list of products against your main record.

  • Because the user can abandon the second page without filling in the Aggregate Level field it will be good to have a subset filtering out those records with empty Aggregate Level where the products are listed.

  • It is also a good idea to have an admin page where those records can be managed and cleared from the system if necessary.

Hi Richard,
Please note that, although it works, it is still not an ideal solution. This is because, as it is a 2-stage process, it is a bit ‘clunky’ from the UX point of view.
I suspect that, in order to get a solution that has a good UX (i.e. a one-step solution), it will be necessary to resort to code.
Angela

Hi Angela,

That is a great question. We don’t have a natural way to support changing the displayed currency when a relationship is set to a record that holds the currency that the money field depends upon.

We will look to see if we can add support for this in a standard way. I tested a solution that will work for now, if you don’t mind a slightly complicated setup.

  1. In the Region selector fragment settings:
  • Set “Trigger Event” (in “Advanced”) to “E1” (This will fire whenever the Region changes and declares to listening fragments that event “E1” happened)
  1. Add the Region Currency fragment setting into the form (or a dummy currency field - see below)
  • Set its wrapper to “Hidden row” so it will not be seen by your user
  • Select the Callback to run that will set its own value to the currency of the selected Region (code below)
  • Set “Refresh on Event” to “E1”.
  • Set “Trigger Event” to “E2”.
  1. In the Money field fragment settings:
  • Set “Refresh on Event” to “E2”.
  • Ensure that the “Dynamic currency” setting corresponds to the currency field added in step 2.

Here is the Callback Code I used in my quick test:

> // Get the record id of the selected Region from the dropdown field in the form
> var product_region = fragment_presenter.get_values()[cs.ref('product_region')];
> 
> // Look up the currency associated with the Region in the database
> var currency_of_selected_region = cs.record(product_region).get(cs.ref('region_currency'));
> 
> // Set the currency field in the input in the form
> fragment_presenter.set_value(currency_of_selected_region);

Note that the above setup will actually submit the region’s currency value. This should be fine because in theory it will always be set to the same value as the selected relation (although if you have no relation selected, that could cause the creation of a new Region).

In any case, it is better to prevent this. You could have a dummy currency field on the Product record that you use in place of the real one above. Or you could create a data processor (set in the form settings), to strip out the currency field from the submitted data.

Here is the data processor:

var form_values = params.fields;

// Remove the currency from the data to be saved
delete form_values.fields[cs.ref(“product_currency”)];

return form_values;

The above seeks to make use of our built-in, dynamic currency logic. But you might be able to cheat, by ignoring that and just specifying a prefix to show (like below, but with the dynamic currency). This might be awkward though to translate 3-digit ISO codes into symbols and have them displayed an htmlentities correctly.

fragment_presenter.set_setting(‘prefix’, ‘£’);

As I said above, we will endeavour to make this simplier very soon!

I’d be happy to talk it through on the phone if you’d like.

Paul

2 Likes

Hi Paul,
Thank you for your solution. It worked perfectly. :smile:

I will be able to replicate it across all forms that set monetary values at the same time that the Region / Currency is set.

Angela

1 Like