How to re-initialize the local variable which is created while start of the expression rule? and how to execute a block of code in the 'Else' part of the 'If' condition.

Hi,

1) May I know how to change or re-initialize the local variable which has been initialized in the starting of the expression rule. (I have marked the local variable which needs to be re-initialized in 'Green' color).

2) Also may I know how to execute queryentity or execute a block of code in the 'Else' part of the 'If' condition. (I have marked the block of code which needs to be executed in the 'Else' part of 'If' condition in 'Yellow' color)

load(
  local!QuoteDetails: null,
  local!quoteSection: tointeger({}),
  if(
    ri!action = "Start",
    cons!QUOTE_UI_ROUTE_NAMES[3],
    {
      local!QuoteDetails: a!queryEntity(
        cons!MONITOR_QUOTE_SECTION,
        a!query(
          selection: a!querySelection(
            columns: {
              a!queryColumn(
                field: "Quote_Section"
              ),
              a!queryColumn(
                field: "Created_On"
              )
            }
          ),
          filter: a!queryFilter(
            "Policy_Id",
            "=",
            if(
              ri!policyId = "",
              0,
              ri!policyId
            )
          ),
          pagingInfo: a!pagingInfo(
            startIndex: 1,
            batchSize: 1,
            sort: a!sortInfo(
              "Created_On",
              false()
            )
          )
        )
      ).data,
      local!quoteSection: index(
        local!QuoteDetails,
        "Quote_Section",
        ""
      ),
      if(
        isnull(
          local!quoteSection
        ),
        cons!QUOTE_UI_ROUTE_NAMES[1],
        cons!QUOTE_UI_ROUTE_NAMES[local!quoteSection]
      )
    }
  )
)


Regards,

Balaji.R

  Discussion posts and replies are publicly visible

Parents
  • +1
    Certified Lead Developer

    > May I know how to change or re-initialize the local variable which has been initialized in the starting of the expression rule.

    Basically, you don't.  That's not how local variables work, especially in an expression rule.  Basically the real solution to your question is, you need to learn the proper way to structure local variables within expression rules.  Also, I would advise you to always use with() to store variables in an expression rule, especially if it'll potentially be called from SAIL forms.

    /* first we check for the override condition and pass back the default value */
    if(
      ri!action = "Start",
      cons!QUOTE_UI_ROUTE_NAMES[3],
    
      /* if the override condition is not true, then we do our querying to local variables etc. */
      with(
    
        /* do the initial query */
        local!QuoteDetails: a!queryEntity(
          cons!MONITOR_QUOTE_SECTION,
          a!query(
            selection: a!querySelection(
              columns: {
                a!queryColumn(
                  field: "Quote_Section"
                ),
                a!queryColumn(
                  field: "Created_On"
                )
              }
            ),
            filter: a!queryFilter(
              "Policy_Id",
              "=",
              if(
                ri!policyId = "",
                0,
                ri!policyId
              )
            ),
            pagingInfo: a!pagingInfo(
              startIndex: 1,
              batchSize: 1,
              sort: a!sortInfo(
                "Created_On",
                false()
              )
            )
          )
        ).data
      ),
        
      /* find the "Quote_Section" property, if it exists */
      local!quoteSection: property(
        local!QuoteDetails,
        "Quote_Section",
        ""
      ),
    
      /* return eventual result */
      if(
        isnull(
          local!quoteSection
        ),
        cons!QUOTE_UI_ROUTE_NAMES[1],
        cons!QUOTE_UI_ROUTE_NAMES[local!quoteSection]
      )
    
    )

  • Hi Mike,

    Thanks for your detailed response, that was really informative and am calling the expression rule from webapi and not from SAIL forms so I hope i can go with load() instead of with().

    Regards,
    Balaji.R
  • 0
    Certified Lead Developer
    in reply to rp_balaji

    That's right - in use cases outside of use on SAIL forms (including expression rules directly called by SAIL forms), load() and with() work identically as far as i know, since there is no chance (or need) for the dynamic reevaluation of the values stored within with() variables. Point being, it should be safe to use load() if you like.

Reply Children
  • 0
    Certified Lead Developer
    in reply to Mike Schmitt
    I'm with on this one; I like to use with() inside expressions as those variables only really "live" as long as the expression executes. From what I remember, there are also some places where you can't call a rule that uses a load - but I think they are deprecated now.
  • 0
    Certified Lead Developer
    in reply to PhilB

    The issue I've run into unexpectedly in the past, and which has caused trouble for new developers here and there whom i've had the opportunity to help troubleshoot, is this: when an expression rule is called as a "helper utility" from a SAIL form, and uses local data stored in one or more load() variables, the data does NOT refresh as a user might initially expect between instances.

    In one case, it was an expression rule I wrote to run semi-complex logic to determine which values to show as the choices for a dropdown, which should change depending the values provided for some other elements on the same SAIL form.  The expected dropdown options would populate correctly the first time the other on-form options were chosen, but if I changed the selected options, the dropdown selections did not refresh accordingly.  It was all because the local variables in the expression rule were being stored (originally) within load(), which of course only initializes its value (in any particular instance) once, and then remains static until acted upon externally.  Therefore I advise all utility-style expression rules to store their values in with() variables, unless and until load() is specifically required.