Announcement

Collapse
No announcement yet.

Trigger formula on (not longer) related entities

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trigger formula on (not longer) related entities

    Hello, I have had the following problem several times now, for which I have not found a reasonable solution myself. Perhaps someone here can help me:

    Let's assume I have:

    Entity A ----(1:n)---- Entity B

    If i want to know how many Bs are connected to an A, I can use the formula: entity\countRelated(). So far so good.

    Let's take an example:

    A1 -> B1;B2;B3 (count = 3)
    A2 -> B4 (count = 1)

    Now, if I change the connection at B2 from A1 to A2, I have:

    A1 -> B1;B3 (count = 2)
    A2 -> B2;B4 (count = 2)

    But I don't know how I can trigger formula in A1, to recount all related Bs.

    With workflows I can only trigger A2, so then I have:

    A1 -> B1;B3 (count = 3)
    A2 -> B2;B4 (count = 2)

    ​What is obviously wrong.

    And I simply can't find a reasonable solution. I am now working with scheduled workflows so that the errors are fixed at least at a certain time, but that is of course not an ideal solution.​

  • #2
    Did you try workflow on @relate and @unrelate signals?

    Comment


    • #3
      No, I didn't tried @unrelate signal. I didn't even know that existed. I'm going to try it out. That should work. Thank you very much dimyy !

      Comment


      • #4
        Ok, I've tried it, but I can't get it working.


        Entity A ----(1:n)---- Entity B: Link name cLink


        Then I created an workflow:
        Target entity: A
        Signal: @unrelate.cLink

        This should be triggered if I remove or change B in A, right?

        But it's not getting triggered at all.
        Last edited by Stefan; 08-21-2024, 06:41 AM.

        Comment


        • rabii
          rabii commented
          Editing a comment
          @relate and @unrelate won't work as they are meant to work for Many to Many relationship.

      • #5
        Did you clear cache after creating workflow? I try on account-contact link - all ok
        Attached Files

        Comment


        • rabii
          rabii commented
          Editing a comment
          It worked because it is a many to many relationship and @relate and @unrelate work on on many to many relationships

      • #6
        You can achieve this by using a formula in B entity so that it will trigger if aLink is changed then you will need to recalculate all Bs in that link A entity. Here is a code example below that you should put in B Entity.

        PHP Code:
        // Replace the links and entity name with the correct.
        if (entity\isAttributeChanged('aLinkId')) {
            
            
        // If was not linked and now it is linked
            
        if (!entity\attributeFetched('aLinkId') && aLinkdId) {
                
        $id record\fetch('AEntity'entity\attribute('aLinkId'));
            
                if (
        $id) {
                    
                    
        $count record\count('BEntity''aLinkId='$id);
                
                    
        record\update('AEntity'$id'totalCount'$count);  // replace totalCount with the field that hold the count.  
                
        }    
            }
            
            
        // If was linked and now unlinked
            
        if (entity\attributeFetched('aLinkId') && !aLinkdId) {
                
        $id record\fetch('AEntity'entity\attributeFetched('aLinkId'));
            
                if (
        $id) {
                    
                    
        $count record\count('BEntity''aLinkId='$id);
                
                    
        record\update('AEntity'$id'totalCount'$count);  // replace totalCount with the field that hold the count.  
                
        }    
            }
            
            
        // If was linked and link is changed to a different link
            
        if (
                
        entity\attributeFetched('aLinkId') &&
                
        aLinkdId &&
                (
        aLinkdId != entity\attributeFetched('aLinkId'))
            ) {
                
        $oldLinkId record\fetch('AEntity'entity\attributeFetched('aLinkId'));
                
                
        $newLinkId record\fetch('AEntity'entity\attribute('aLinkId'));
            
                if (
        $oldLinkId) {
                    
                    
        $count record\count('BEntity''aLinkId='$oldLinkId);
                
                    
        record\update('AEntity'$oldLinkId'totalCount'$count);  // replace totalCount with the field that hold the count.  
                
        }
                
                if (
        $newLinkId) {
                    
                    
        $count record\count('BEntity''aLinkId='$newLinkId);
                
                    
        record\update('AEntity'$newLinkId'totalCount'$count);  // replace totalCount with the field that hold the count.  
                
        }  
            }


        This code simply is triggered when the A link in B entity is changed and it checks 3 possible scenarios and update the totalCount (where you hold the count in your entity A). 3 scenarios (B was l=not linked and is now linked to A) (B was link to A and now is unlinked) ( B was linked to A1 and now is linked to A2)

        if you use this code in B entity then you don't need to use any other code as this will always update entity A whenever it is linked to entity B.

        I hope this help
        Rabii
        Web Dev | Freelancer

        Comment


        • #7
          rabii: I've understood the Documentation to say that @relate only works for many-to-many, but since @unrelate doesn't say so, I thought it would also work for other relationships.Thank you very much for the code! I will try this out immediately

          Comment


          • rabii
            rabii commented
            Editing a comment
            yeah they must have just missed to mention it on documentation but i am sure that even @unrelate would not work for your use case as it is meant for many to many as mentioned before.

        • #8
          Hey Stefan

          There is also another way to do this in a simple way by using readLoaderClassName and listLoaderClassName for your field. e.g the field that holds the count count be calculated automatically using using the read and list loaders.

          here is an example i provided in another post

          Hi, I created a formula field 'totalQuotes' that counts the number of records linked ('quotes'): totalQuotes = entity\countRelated('quotes'), It works OK when I create a new record or save one after a modification. But I need to update the value of the field 'totalQuotes' every time a related record is added in the bottom

          Rabii
          Web Dev | Freelancer

          Comment

          Working...
          X