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
Announcement
Collapse
No announcement yet.
Trigger formula on (not longer) related entities
Collapse
X
-
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
Leave a comment:
-
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
- Likes 1
Leave a comment:
-
-
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.
Leave a comment:
-
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.Tags: None
Leave a comment: