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
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
Ryan Farley,
Hey Ryan,
Thanks for the reply, however that didn't work. The CardMode returns undefined, and when inspecting it in developer tools (request.$context object) the value is always 'edit' , when inserting the first time or when updating. Is there anything else I can do to fix the issue? Thanks
Best,
Mohamad
Yurii Sokil,
Hey Yurii,
Thanks for the reply, I have tried moving the logic to backend, however I encountered some issues. The overloaded method OnInserted cannot be async, and therefore I cannot call APIs directly from that method.
There is another option to call asynchronously using this method https://academy.creatio.com/docs/8.x/dev/development-on-creatio-platform/back-end-development/objects-business-logic#title-2173-11 . However that doesn't solve the problem as if there were any errors when executing the asynchronous code, the record gets saved normally without indicating errors. Its a fire and forget operation and I need something that prevents a record from being inserted if the API call failed. Is there any way I can achieve this? Thanks for help
Best,
Mohamad
I need something like this but on freedom UI page instead of classic UI page
Mohamad Salhani,
If all you're after is to set up an auto number column, you can use the new auto number column default value. See https://customerfx.com/article/working-with-autonumber-fields-in-creatio/
Ryan
Ryan Farley,
No, so basically I need to populate a field once a record is created, and the value comes from an external API that I have to call
Ryan Farley,
Thanks for the support, I was finally able to solve it by checking the createdOn and modifiedOn dates for the record and see if they are equal, this way I can identify if the record was added for the first time or not.
Thanks again!
Hi Mohamad,
You can use AsyncPump.Run() to call async methods synchronously in your event listener. Also you can override OnInserting rather than OnInserted so your logic is called before the record is actually saved.
Yurii Sokil,
Hey Yuri,
Thanks for the reply! It did work however I have a question. How can I pass arguments to the method passed in AsyncPump.Run() ?
I have this code, but I can't pass arguments to the method.
[EntityEventListener(SchemaName = "UsrPetolTest")] public class CustomEntityEventListener : BaseEntityEventListener { public override void OnInserting(object sender, EntityBeforeEventArgs e) { base.OnInserting(sender, e); var entity = (Entity)sender; var userConnection = entity.UserConnection; AsyncPump.Run(testing); } private async void testing() { try {
Best,
Mohamad