calculate the total sum of the son entity's field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lj4353
    Senior Member
    • Nov 2023
    • 114

    calculate the total sum of the son entity's field


    I have created two entities, otherIncome and bonus, with a one-to-many relationship between them. I want to calculate the total sum of the amount field in the bonus entity and display it in the number field of the otherIncome entity.​
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    #2
    easy, below the code you need to apply to the otherIncome entity formula:

    PHP Code:
    if (entity\isAttributeChanged('bonusesIds')){
        number = entity\sumRelated('bonuses', 'amount');
    }

    make sure that the names of the relationships are correct i just assumed that one otherIncome has many bonuses
    Rabii
    Web Dev

    Comment


    • lj4353
      lj4353 commented
      Editing a comment
      Great!problem solved
  • zerosix
    Member
    • Jan 2024
    • 45

    #3
    but is this code will work after deleting or unlinking bonuses?
    Last edited by zerosix; 01-21-2024, 11:11 PM.

    Comment


    • rabii
      rabii commented
      Editing a comment
      No it won't delete or unlink any bonuses.

    • zerosix
      zerosix commented
      Editing a comment
      Hello Rabii, i mean is this code (formula) will work after deleting or unlinking bonuses?i think it's working just in save
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    #4
    hey zerosix

    It should work if you have [Link Multiple Field] option enabled on your relationship otherwise it won't work and you will have to amend the code to something like below in order to work on both unlink and remove:

    PHP Code:
    if (entity\countRelated('bonuses')) {
        number = entity\sumRelated('bonuses', 'amount');
    } else {
        number = null;
    }
    Rabii
    Web Dev

    Comment

    Working...