Refreshing field value underlying record after quick creating record

3.9k Views Asked by At

I have an entity form (Entity A) which contains an subgrid to display linked records of another entity (Entity B), where multiple B records link to one A record. Records of entity B are linked to entity A by clicking the '+' icon on the subgrid, filling out an quick create form for entity B and saving. After the save, the newly created record B shows up in the subgrid on the form of record A: all as expected.

When filling in the quick create form for entity B, a field containing an amount needs to be filled. After saving, a server side plugin fetches all records of entity B linked to the specific record of entity A, calculates the sum of all their amounts and fills the total amount in a field on the form of the record of entity A. This also works as expected, the newly calculated value is stored in the database. The problem is that the field that displays the total amount on form A does not refresh. It keeps on displaying the same amount it did when the form was loaded and only updates after a full page refresh. The value though, is updated every time a new record of entity B gets linked. The value on A only shows it's 'new' value when the page is refreshed.

The problem is that the users link 10+ records of B in a row, without refreshing. We get the request to make the field refresh automatically quite a lot, but i have no idea if this can be done, so: Is it possible to refresh (/re-render) a field on the form of entity A on the on save event of entity B? I presume this has to be done in javascript, since it is the client side representation of the fields value and the field value is already correctly stored in the database.

Thanks!

3

There are 3 best solutions below

0
On

Using JavaScript, there is a grid refresh event you can subscribe to. From there you have a couple options.

  1. In the refresh event you could trigger a rest call to retrieve the value from the server, and then update the value on the form. I'd also disable submit for the field as well, since you don't want the client updating it.
  2. Or- Call save explicitly. You'd either have to ensure the form is valid or change all required fields to no longer be required to allow the save to happen. The save event will return the updated value from the database and automatically update the form.

If you've never made a rest call before, the first option is probably harder, but IMHO, it is the better option.

0
On

The easiest solution I can think of would be to poll the value by adding a new handler to entity A's form OnLoad event.

The code would then employ setTimeout to constantly read the value of the field from the REST API, compare the content of the field, and if it's changed it would invoke Xrm.Page.data.refresh to asynchronously reload the form without a full page reload.

Another (IMHO better) option: upgrade to a more recent version. Starting in 2015 Update 1, subgrids have OnLoad event too: you just need to handle that, invoking Xrm.Page.data.refresh without the weight of repeatedly pinging the server.

0
On

If helps. it can be done in C#. You can make plugin using the logic you described here.

That plugin would be registered using Plugin Registration Tool. you would have to add two steps for Entity B. First one would have Create message, and the second one Update. For the second step you would select just the amount filed update, so it is triggered only when that filed is updated.

It can also be done using Rollup Fields. They can be updated on click, but also have automatic update on every 12 hours, if that is frequent enough for you.

I don't have an idea how to do it using JavaScript, but i am new in CRM, so someone else might appear with that type of solution, I would also be glad to read it.