Announcement

Collapse
No announcement yet.

calculate the total sum of the son entity's field

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

  • 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.​

  • #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

    Comment


    • lj4353
      lj4353 commented
      Editing a comment
      Great!problem solved

  • #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

  • #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;
    }
    ​ 

    Comment

    Working...
    X