Visual Builder

Get Involved. Join the Conversation.

Topic

    Rob Raftery
    Problem Passing an Array to a Function
    Topic posted September 19, 2019 by Rob RafteryRed Ribbon: 250+ Points 
    22 Views, 3 Comments
    Title:
    Problem Passing an Array to a Function
    Content:

    I have an array of strings variable defined as:

            "searchCriteriaValues": {
                "type": "string[]"
            }

    I am passing the array to a function as part of an action chain (see image)

    The function below is accepting the array.  However, in the very first step where I am displaying "searchCriteria.length", I am not getting back the number of entries in the array.  I am getting back the length of the contents of the first element in the array.  For example, if my array contains {"yellow", "blue"}, instead of getting back 2, I am getting back 6 for the length of the color yellow.

    Ideally, I don't even want to pass an array of strings.  I need to pass an array of an object, but I was having the same issue, so I parred it down to simply pass an array of strings.  I still can't seem to get it to work.

    Any idea what I am doing wrong?

      FlowModule.prototype.searchCriteriaValidator = function(searchCriteria) {
        return [{
          validate: (searchCriteria) => {
              console.log('HERE??? ' + searchCriteria.length);
              for (var x = 0; x < searchCriteria.length; x++) {
                console.log('HERE???'+ '|' + x + '|' + searchCriteria[x]);
                if (searchCriteria[x]) {
                  console.log(x + '|' + searchCriteria[x]);
                  return true;    
                }
              }

              throw new Error('You must provide at least one search criteria');
          },
        }];
      };

    Image:

    Comment

     

    • Shay Shmeltzer

      See if switching your assignment from "Static Content" to "Expression" (lower right side of your picture) helps.

    • Rob Raftery

      It actually starts off as "Expression" when I first make the assignment.  However, when I go back in it shows it as "Static Content":

      [
       "{{ $page.variables.searchCriteriaValues }}"
      ]

      If I change it, again, to "Expression" it looks even worse:

      [
       "{{ [\n \"{{ $page.variables.searchCriteriaValues }}\"\n] }}"
      ]

      It doesn't want to stay as an "Expression".

      • Shay Shmeltzer

        Try removing the whole value in the field an doing it from scratch. Should look like the image below.

        Or worse case scenario - you can edit the json file directly to something like this:

                "callModuleFunction1": {
                  "module": "vb/action/builtin/callModuleFunctionAction",
                  "parameters": {
                    "module": "[[ $page.functions ]]",
                    "functionName": "sayHi",
                    "params": [
                      "{{ $page.variables.myarray }}"
                    ]
                  },

        • Rob Raftery

          Hi Shay,

          Sorry, I should have been more clear in my original post.  I AM trying to pass an array of strings to a function.  However, the function is being used in the validator attribute for an input text component.  The variable $variables.SearchCriteria is my array of strings.  Below is the code assigned to the validator attribute for my input text component.  The function code is also attached below.

          {{ $flow.functions.searchCriteriaValidator($variables.SearchCriteria) }}

          Through some testing and debugging, it seems like functions used in the validator attribute are reserving the first parameter for the components Value property in the Data section.  If I change the validator definition to

          {{ $flow.functions.searchCriteriaValidator("",$variables.searchCriteriaValues) }}

          I am able to successfully parse the array of strings because they come into the validator function appropriately.  Notice how I am simply passing "" as the first parameter.  The validator function doesn’t seem to care about the first parameter.  Even though I am passing "" as the first parameter, the validator function still knows to use whatever is stored in the components Value property.

          Is this normal behavior for a validator?  We haven't seen this documented anywhere.  This is just the behavior I am seeing during my testing.

            FlowModule.prototype.searchCriteriaValidator = function(parm, array_parm) {
              return [{
                validate: (parm) => {
                    console.log('HERE??? ' + array_parm.length);
                    console.log('HERE??? ' + array_parm);
                    for (var x = 0; x < array_parm.length; x++) {
                      console.log('HERE???'+ '|' + x + '|' + array_parm[x]);
                      console.log('HERE???'+ '|' + x + '|' + array_parm);
                      if (array_parm[x]) {
                        console.log(x + '|' + array_parm[x]);
                        return true;    
                      }
                    }

                    throw new Error('You must provide at least one search criteria');
                }
              }];
            };