Variable is not refreshing

Good afternoon all.

Here is what's going on.

I am given a string, or Text variable. This variable is used to store employee IDs. Although it is one variable, it can contain several IDs (or contain none). There is nothing I can do about this and I cannot change this. Now, I need to check if this value has any of the following: no values, one value, or many values. This Text can dynamically change when a user selects IDs, or removes them, from a custom picker. If the Text has several values, I am currently using split() to separate the IDs into an array, which is working fine. Then, I have a local variable that uses the array to get the names for each of those IDs. Unfortunately, the last ID is contained in the new array, but fails to update to the name. For example, if the following IDs exist: 1, 2, and 3, It will show: Peter, Paul, and 3. I'm currently using Appian 21.3.

local!employeeIDs: if(
    isnull(ri!record.employeeIDs),
    null,
    split(ri!record.employeeIDs, ";")
  ),	
local!employeeNames: a!refreshVariable(
    value: if(
      or(
        rule!isEmpty(local!employeeIDs),
        length(local!employeeIDs) = 0
      ),
      null,
      a!forEach(
        items: local!employeeIDs,
        expression: rule!GetNameByID(fv!item)
      )
    ),
    refreshOnVarChange: local!employeeIDs
  )

If I did a terrible job explaining my issue, please let me know. I will gladly elaborate on the issue. Any and all advice will be greatly, greatly appreciated. Thanks.

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    I don't see anything particularly wrong with the posted code snippet (beyond certain code style nitpicks).  One important thing to note is that your "refreshOnVarChange" is unnecessary as it's redundant; all a!localVariables() variables automatically have "refreshOnReferencedVarChange" set to TRUE by default unless overridden, so your local!employees variable would already refresh on any update to local!employeeIDs.  The same would be true for local!employeeIDs upon update to ri!record.employeeIDs.

    Just to double check, what data type is ri!record.employeeIDs?  Are we assuming it's a single string of semicolon-separated ID values with no additional spaces in between?  I'm just checking because a common point of confusion around here arises from accidentally assuming Appian's default method of displaying an array of data in text (separated by semicolons) is equivalent to a semicolon-separated text list, even though these two things aren't direclty equivalent without some type-casting.

  • Thanks for responding, Mike.

    I agree with the redundancy, but it wasn't working without the refreshvariable, so I thought I would throw that in there.

    The multiple values in the Text variable are as follows: "1; 2". I hope this helps. It looks as if the last value is not followed by a semicolon. Does this make any difference? 

    • ri!record.employeeIDs is type Text.
    • There is a space after each semicolon. 
  • Ok, thank you! The space after the semicolon was the issue. I'm not sure how to remove that space before processing the Text variable. Any help?

  • 0
    Certified Lead Developer
    in reply to khabran

    I was going to suggest it would be sufficient to do "split(ri!record.employeeIDs, "; "), i.e. adding a space after the semicolon in your original code.

    Do you have any way of knowing why the employee IDs are coming through in ri!record in this manner?  The part that makes me nervous about this is that even a slight future adjustment to how the variable is formed, would throw off your logic.

    Out of curiosity, can you confirm that "ri!record" is single, and that employeeIDs is also single, and that the employeeID string is being intentionally concatenated together and semicolon-separated?  Because if, for example, ri!record is actually an array, then a different approach would be needed here.

  • Thanks Mike.

    So the "record" is a CDT containing the employeeIDs (which is type Text and not an array). There was no space being added after the semicolon, it was just doing that itself. Upon, removing that space, it is now updating the Employee Names array correctly. I agree with the issues that could arise in the future with these variables and this logic. Unfortunately, there is nothing at this time I can do about it. It was what I was given and I have to make due. I think it would be better to have the Text variable as an array and eliminate the need to split() the variable to access each of the employee IDs. 

  • +1
    Certified Lead Developer
    in reply to khabran

    A secondary suggestion would be, if you're forced to stick with a Text-As-Array setup (under the assumption that the implementation of the array is under your control), would be to use JSON to take the guesswork out of formatting.  That is to say, convert the array of IDs to a JSON string before storing it in the text variable, using a!toJSON(), and use a!fromJSON() to convert it back later when needed.  In this way you're not stuck implementing your own parsing routine for the array-turned-string.