UpdateRecord for contacts doesnt work from rule from another custom module

UpdateRecord for contacts doesnt work from rule from another custom module

I have a function that is triggered by an automatic rule when a record is created or edited in a custom module. The function searches for a record in the Contacts module and saves it in a lookup field of the newly created or updated record. It also updates another field in the found record within the Contacts module.

The issue is that when the rule is triggered by an update, everything works fine. However, when the rule is triggered by a newly created record, the update of the field in the Contacts record does not happen. Everything else works correctly—there are no errors, and the lookup field in the created record is updated properly, but the updaterecord operation on the Contacts record doesnt works

The same function also performs other tasks, such as creating another record in a different module, and that works in all cases. However, the update of the field in the Contacts record only works when the rule is triggered by an update, not when a record is created.