Access record after @delete event

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Maarten
    Member
    • Jun 2020
    • 56

    Access record after @delete event

    I have a workflow that catches the @delete event. I want to be able to take an action following this event, for which I need some attributes from the deleted record. However, the record is not available anymore, and I'm getting thrown null errors in the logfile.

    Is there a way to access attributes from deleted records?
  • abidoss
    Senior Member
    • Mar 2023
    • 230

    #2
    can you share the log file?

    Comment

    • rabii
      Active Community Member
      • Jun 2016
      • 1250

      #3
      Originally posted by Maarten
      I have a workflow that catches the @delete event. I want to be able to take an action following this event, for which I need some attributes from the deleted record. However, the record is not available anymore, and I'm getting thrown null errors in the logfile.

      Is there a way to access attributes from deleted records?
      you can try something like below (just as example) apply your own entity instead of opportunity:

      PHP Code:
      $id = record\findOne('Opportunity', 'createdAt', 'desc', 'id=', id, 'deleted', true);
      
      $name = record\attribute('Opportunity', $id, 'name'); 
      
      You can still access the deleted record using (deleted = true).

      hope this helps
      Rabii
      Web Dev

      Comment

      • Maarten
        Member
        • Jun 2020
        • 56

        #4
        Hi Rabbi,

        Thanks for your response. I've tried the 'deleted' true filter on a record that was deleted, but it returns a null still.

        You code also suggest that the $id returned by record\findOne is different from id. But shouldn't they not be the same?






        Comment


        • rabii
          rabii commented
          Editing a comment
          can you share your code and flowchart ? is the target entity for the flowchart is the same entity type that gets deleted ?
      • Maarten
        Member
        • Jun 2020
        • 56

        #5
        I've slimmed it down to a very basic workflow, that catches a @delete event, and calls a BPM process that creates a notification.

        Click image for larger version  Name:	image.png Views:	0 Size:	45.3 KB ID:	94940
        ​
        The Travel event that I'm removing has this id: 64a24fea1f9d88514

        I am seeing the flow trigger:
        Click image for larger version  Name:	image.png Views:	0 Size:	103.2 KB ID:	94933​ ​
        The notification is sent fine if I create the notification directly in the workflow, instead of the BPM process that is called by the workflow.

        This sounds like a bug?

        I'm on Espo 7.5.5
        PHP 8.2.4 (cli)
        Last edited by Maarten; 07-03-2023, 03:28 PM.

        Comment


        • rabii
          rabii commented
          Editing a comment
          why not just use a workflow instead of bpmn, i have tried it and it seems to work only one thing is that i couldn't access the deleted record attributes on the on notification but i could access attributes to update related record with data from delete record.
      • yuri
        Member
        • Mar 2014
        • 8451

        #6
        You should be able to access attributes of a deleted record with entity\attribute('attributeName'); But I'm not sure.

        I'd recommend using a workflow rule for such simple tasks.
        If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

        Comment


        • Maarten
          Maarten commented
          Editing a comment
          "[2023-07-03 19:52:08] ERROR: Workflow[6489e19bddfe58373]: Action failed [executeFormula] with cid [2], details: Formula: Entity required but not passed..
          "

          This is what I get when I do entity\attribute('id') in a formula script in a workflow that triggers on @delete event.

          So it seems that the deleted entity is not passed to formula scripting environment in the workflow.

          However it does seem to be accessible in a prescripted action, such as update related records

        • rabii
          rabii commented
          Editing a comment
          you can't access Id of the entity that way it is by design for security reason if i remember.
      • Maarten
        Member
        • Jun 2020
        • 56

        #7
        I use a BPM because my full flow is much more complex:

        Click image for larger version

Name:	image.png
Views:	332
Size:	22.2 KB
ID:	94949​

        I don't think I can loop over an array of IDs in a workflow?

        I'm still unclear why I get this error in BPM. Even if I don't use any attributes of the record (as shown above), it fails.

        It seems that the workflow can access a deleted record by ID, but if that same record is used to call a BPM flow, then it fails.

        I don't think this should happen?

        Comment

        • Maarten
          Member
          • Jun 2020
          • 56

          #8
          Ok, I found a workaround for now. Since in my particular case, when I delete a Travel record, I want all related Traveler records to be deleted. So now, instead of the above BPM loop, I delete all related traveler records when the Travel is deleted. Then I catch the @delete of the Traveler, and perform the API calls to delete the traveler from the sharepoint list.

          Comment

          • Maarten
            Member
            • Jun 2020
            • 56

            #9
            Rabii, I see in the forum you make a lot of reference to this formula that should work to retrieve a deleted record:

            $travel = record\findOne('Travel', 'createdAt', 'DESC', 'id=', '64a24fea1f9d88514', 'deleted', true);

            However, it doesn't work for me. When I restore the record I can find it with:
            $travel = record\findOne('Travel', 'createdAt', 'DESC', 'id=', '64a24fea1f9d88514', 'deleted', false);

            When I delete the record, I cannot find it with:
            $travel = record\findOne('Travel', 'createdAt', 'DESC', 'id=', '64a24fea1f9d88514', 'deleted', true);

            Can someone double check that with espo v 7.5.5 this is actually working for them?

            Comment

            • rabii
              Active Community Member
              • Jun 2016
              • 1250

              #10
              Hey Maarten

              You are right, this seems to be working fine in older version. i have checked it again and it is not working anymore. double checked and code for most formula function has been rewritten and use a different approach. Maybe a request to double check with the team if they want to consider this again. This could be achieve just by calling a method on the query builder like below:

              PHP Code:
              $builder->withDeleted(); 
              
              Issue is that this needs to be applied to most of the function. i am working now on creating an extensions that allow to interact with deleted records. i will share with you once i am done.
              Rabii
              Web Dev

              Comment

              • yuri
                Member
                • Mar 2014
                • 8451

                #11
                It never worked.

                > most formula function has been rewritten and use a different approach.

                I don't think this is correct. I don't see any changes besides code style and some refactoring.
                If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

                Comment


                • rabii
                  rabii commented
                  Editing a comment
                  that is weird it used to work and i have used it in different workflows/bpmn script formula. not sure but i think this could be important hence it would allow the user to access data. what about a formula like record\findDeleted() alongside with record\restoreDeleted() these will come handy which will allow the user to access deleted records when needed.

                • yuri
                  yuri commented
                  Editing a comment
                  If we make record\findOne to return deleted records by default, it would be a breaking change. It will never happen. Adding new functions, maybe. But I wouldn't hurry with it. The system was not designed to work with deleted records at all.
                  Last edited by yuri; 07-04-2023, 01:53 PM.

                • rabii
                  rabii commented
                  Editing a comment
                  i was thinking of adding functions rather then modifying existing ones. Of course if this is something you think could help improve the user experience for certain use case.
              • yuri
                Member
                • Mar 2014
                • 8451

                #12
                Both BPM and Workflow were not designed to work with deleted target records. Moreover, the concept was that it's supposed that regular users don't have access to delete action. Better to use some status for that.
                If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

                Comment


                • yuri
                  yuri commented
                  Editing a comment
                  As I workaround, you could forbid delete access for your entity and use some statue (like 'Canceled', 'Discarded'). Then you can have a scheduled workflow that removes such record after a while if you need.

                • rabii
                  rabii commented
                  Editing a comment
                  I just thought having such capacity would allow the (admins) to design a workflow that can recover info from deleted records or even have ability to restore them if they are deleted for certain reason. Adding more statuses sometimes work but sometimes it doesn't depends on what we are trying to achieve.
              • rouhu
                Member
                • Sep 2020
                • 39

                #13
                Originally posted by Maarten
                I have a workflow that catches the @delete event. I want to be able to take an action following this event, for which I need some attributes from the deleted record. However, the record is not available anymore, and I'm getting thrown null errors in the logfile.

                Is there a way to access attributes from deleted records?
                Yes, copy the attributes to a related record in the @delete workflow actions.
                Select action Update Related Record where you have some temp fields to store values.
                description=workflow\targetEntity\attributeFetched ('id'); <-- will save the ID of the deleted record to description field of the related entity.

                Comment

                Working...