View Only
  • 1.  Calculated Attributes - if-then-else logic

    Posted Aug 30, 2012 02:11 PM
    Has anyone found a way to incorporate if-then-else logic in populating the value of a calculated attribute?

    For example, let's suppose we have 4 fields.

    Field1, Field2, Field3, Field4

    The calculated field for the sum is straight-forward enough sum(Field1, Field2, Field3, Field4)

    However, if I have a requirement psedo-coded as follows:

    if (field2=5 or field3 = 5)
    then the value of the calculated field is to be 35
    else the value of the calcuated field is sum(Field1, Field2, Field3, Field4)

    Any and all ideas and input is welcome.


  • 2.  RE: Calculated Attributes - if-then-else logic

    Posted Aug 31, 2012 12:50 AM
    Don't think that would be possible

    Instead, would suggest that you create a stored procedure, and get the calculated field populated


  • 3.  RE: Calculated Attributes - if-then-else logic
    Best Answer

    Posted Aug 31, 2012 03:38 AM
    In short - "no, not really".


    I have managed to get something like that to work, but it relies on a database value somewhere;

    I have an object which has different "versions" and the calculated score algorithm varies as per the version of the object instance, so I need an IF/THEN/ELSE logic in the calculated attribute.

    i.e. my logic is IF version_01 then score = ( A + B + C ) ELSE IF version_02 then score = ( B + C + D ) etc ( A,B,C,D are just other attributes on my record)

    And I can do this in my calculated attribute because I have a boolean attribute that indicates which version I am looking at, i.e. my record EITHER has version_01 = 1 OR version_02 = 1 (and this is always the case).
    So I can build the calculated attribute like this;

    version_01 * ( A + B + C ) + version_02 * ( B + C + D)

    However, I can not see how I could get that to work in your case unless you had an boolean attribute called "field2_or_field3_equals5" which was either or 1... and you'd need to maintain that attribute somehow automatically* (i.e. through a back end SP/GEL/XOG procedure) and once you start building a backend process, you might as well just get the backend process to populate an attribute on your object (which is what NJ is suggesting cool)).

    * - it has to be a "real" attribute and not another calculated attribute since you can't build calculated attributes using other calculated attributes (which is reasonable because you can not guarantee their calculation sequence!)


    HOWEVER there is an alternate solution perhaps.... if you build a parametrised lookup, which takes the record instance ID as the parameter, then you can "code" the IF/THEN/ELSE logic in the NSQL for the lookup and return a display value as your calculated score, the lookup needs to ALWAYS return "1" as its hidden key and you then need a dummy attribute on your object that always has the value "1" to associate the lookup to - then you can use that dummy attribute as a display-only, pull-down lookup and it will display your calculated score OK. I have written about this technique a few times on these boards (I'll see if I can find the links with a bit more explanation in). NOTE that this solution will only work on the main object maintenance screens (i.e. the EDIT view) , it will not work on a LIST view (because of a limitation in parametrised lookups). If you need the data on a LIST view, a backend solution is the only thing I think will work.

    EDIT: more info on this technique here ; Resource (General) page: how to display info from secondary page?
    and here ; Dynamic Query Lookup
    (one day, if I get some time and inclination, I might write this technique up "properly" as a "tip" rather than keep trying to explain it (badly) every time it comes up! :ph34r: )

  • 4.  RE: Calculated Attributes - if-then-else logic

    Posted Nov 22, 2012 11:54 AM

    Dave wrote:

    (one day, if I get some time and inclination, I might write this technique up "properly" as a "tip" rather than keep trying to explain it (badly) every time it comes up! :ph34r: )
    That day has come ;

    Tip : How To Put Any Dynamically Generated Value On A Clarity Object