freedom UI - how to execute code when creating a record for the first time

Hello,

 

I need to call a code once a record gets created for the first time. This worked using the following:

handlers: /**SCHEMA_HANDLERS*/[
			{
        request: "crt.SaveRecordRequest",
        handler: async (request, next) => {

 

however what this does is execute the code for each attribute value change, which is not what is needed. How can I call this exactly once just when the record is created for the first time and that's it, without having to call it again for each save request? Thanks

 

Note: I have tried replacing SaveRecordRequest with CreateRecordRequest, however the code was not executed once saving, or on any save.

 

Best,

Mohamad

Like 0

Like

2 comments

You can use the CardState to see if the record is in add mode (record is being added, saved for first time) or edit mode:

const mode = await request.$context.CardMode;
if (mode === “add”) {
    //
}

If you need to call some code exactly once when a record is created, I'd suggest moving the whole logic to backend. Specifically EntityEventListener and implement OnInserting or OnInserted methods to call your logic before or after the record is saved

Show all comments