Announcement

Collapse
No announcement yet.

Formula Question - Attribute change from to

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

  • Formula Question - Attribute change from to

    Need Formula genius or outside of the box thinker!

    Anyway can we formula so that it activate only when it from 1 Value to another value?

    There is the isAttributeChanged

    But that only True or False.

    Here is the setup:

    Entity: Task
    Frequency: Weekly
    Status: Started

    So when Status of "Started" is changed to "Complete", then I want to do something.
    Last edited by esforim; 06-16-2023, 08:26 AM.

  • #2

    Comment


    • #3
      How to use this @item?

      I'm doing something stupid like this but it fail. I will keep trying at the moment.

      Code:
      ifThen
          (entity\attribute('status')=='Schedule' && entity\isAttributeChanged('status'),
              ifThen
                  (status=='Paid' && datePaid!=null,
                  entity\setAttribute('description', 'working')
                  )
          )​

      Comment


      • #4
        WOW! I think this is exactly what I wanted. Here is the update code, time to debug it and test if it working like I wanted it to!



        Code:
        ifThen
        (entity\attributeFetched('status')=='Schedule',
        ifThen
        (status=='Paid' && datePaid!=null,
        entity\setAttribute('description', 'working')
        )
        )​

        Comment


      • #5
        Hi,
        formula is beforeSave
        attributeFetched retrieve value of database instant 0
        when you modify in front-end sample is instant 0+1
        when you save, is instant 0+2
        so in beforeSave, you can compare instant 0 value with instant 1 value

        Comment


        • #6
          Originally posted by espcrm View Post

          WOW! I think this is exactly what I wanted. Here is the update code, time to debug it and test if it working like I wanted it to!



          Code:
          ifThen
          (entity\attributeFetched('status')=='Schedule',
          ifThen
          (status=='Paid' && datePaid!=null,
          entity\setAttribute('description', 'working')
          )
          )​
          you can still use one condition no need to nest if statement because you can do all checking on one level something like below would also work:

          PHP Code:
          ifThen(entity\attributeFetched('status') == 'Schedule' && entity\attribute('status') == 'Paid' && datePaid != null,
              
          entity\setAttribute('description''working')
          )
          ​ 
          Rabii
          Web Dev

          Comment


          • esforim
            esforim commented
            Editing a comment
            Thank you for the optimization Boss, I give it a try.

        • #7
          Anyone have any idea why this formula is delay by 1 edit?

          For example:

          Entity: Accounting
          Field: debit

          Purpose, update the current "balance" of an Bank Account.
          I tried with both API before and after Save, it have the same issue. It working but I need to edit the field Debit twice before data get shown.

          For example:

          Update 1: Debit: 500
          Update 2: Debit 600

          Balance is shown as: 500, once I Update 3: Debit 700, Balance will then shown as 600. It always "1 time behind". On this logic I thought, would not double run the formula but it doesn't work.

          Anyone doing something similar or know how to debug?

          Thank you!

          In my Accounting (custom) entity formula.
          Code:
          ifThen(
          entity\isAttributeChanged('debit'),
          record\update('Asset', array\at(assetsIds, 0), 'formulaRefresh', datetime\now()));
          record\update('Asset', array\at(assetsIds, 0), 'formulaRefresh', 'second run')​
          In my Asset (bank account) formula I calculate the Balance:

          Code:
          $credit = entity\sumRelated('accountings', 'debit');
          $debit = entity\sumRelated('accountingsAssetTo', 'credit');
          
          ifThen(
          assetType=='Bank Account' || assetType=='Cash',
          assetValue=$credit-$debit)​

          Comment


          • rabii
            rabii commented
            Editing a comment
            What is the relationship between Accounting and Asset ? beside (record\update('Asset', array\at(assetsIds, 0), 'formulaRefresh', datetime\now())) what array\at(assetsIds, 0) means it should point to an index. i think updating the records should be easy i guess you will need to use a loop in Accounting to loop through all related assets / specific asset and update it. if you can share some screenshots i will help you with the code.

          • esforim
            esforim commented
            Editing a comment
            It is a ManytoMany n;n relationship between the two.

            array\at(assetsIds, 0)

            Because I can't get the assetID to work properly (being a Many to Many, and Multi-Links), so I use array\at(assetsIds, 0) to get 1st ID from the relationship.

            I will get more details (video/screenshot) as my wording probably does make much sense.

          • rabii
            rabii commented
            Editing a comment
            yap try to get a video so we could understand better
        Working...
        X