Assigning reference type field value in code

I need to assign one dataset reference type field with key from another dataset field on form submit.
Currently i am doing something like that:

$w(“#datasetEventsAudiths”).setFieldValue(“eventID”, $w(“#dynamicDataset”).getCurrentItem());
in AfterSaved event, but its not realy saved to database, although i can see it updated in the dataset collection

EventID is a reference field, so what value should i put in there?

Hi Chana,

There are several things I would like you to pay attention to:

First of all , A dataset needs to load its data before you call the setFieldValue() function.
Usually a dataset finishes loading a short time after the page it is on finishes loading. So if you call the function inside the page’s onReady() event handler, the dataset might not be ready yet.
The solution is to use the dataset’s onReady() function inside the page’s onReady() event handler to ensure that both the page and the datasets have finished loading.

$w(" #datasetEventsAudiths “).onReady( () => {
$w(” #dynamicDataset “).onReady(() => {
$w(” #datasetEventsAudiths ").setFieldValue(“title”, “New Title”);
} );

Second , $w(" #dynamicDataset ").getCurrentItem() function return an object.
There for, in order to use the setFieldValue() function, you need to set as parameter your desirable field (object’s property) from the object.

$w(" #datasetEventsAudiths “).setFieldValue(“eventID”, $w(” #dynamicDataset ").getCurrentItem.(“Field key”) );

Third , I suggest you to check that the field “eventID” is a Filed key and not a Field name;

After that you would see that both dataset and database were updated.

Have a nice day and best of luck!

1 Like

it worked for me, thanks!