Visual Builder

Get Involved. Join the Conversation.

Topic

    Karthick S
    How to return array values in JS function in VBCS?
    Topic posted June 29, 2019 by Karthick SRed Ribbon: 250+ Points, tagged Action Flows, Custom Code 
    262 Views, 17 Comments
    Title:
    How to return array values in JS function in VBCS?
    Content:

    Hi Team,

    I am trying to return array in Javascript and split those array values and populate in text fields.
    But the array values which i return in JS couldnt able to get from the call module function result.
    Please find the sample code which i have tried.
    Kindly help us to do.

     

    Thanks,

    Karthick

    Version:
    19.1.3
    Code Snippet:

    Comment

     

    • John Graves

      You can use JSON to pass any structure you like.

      In your example, "arg1" isn't used, but it would look something like this:

      PageModule.prototype.arrayCheck = function(arg1) {
        // arg1 not used
      
        var returnValues = {};
        returnValues.V_filename="Karthick";
        returnValues.V_filesize="2mb";
        returnValues.V_filetype="pdf";
        return returnValues;
      }
      

      You would then use the dot notation to access these values:

      var val = arrayCheck("not reaully used");
      console.log("Filename: "+val.V_filename);
      console.log("Filesize: "+val.V_filesize);
      console.log("Fieltype: "+val.V_filetype);

       

      • Karthick S

        Hi John,

        Thanks for the update.

        As you said i have tried then how to receive those array values (v_filename,v_file_type and v_file_size) in the action chain.
        .I need  to receive those array values and assign those array values in a separate page variable.
        Could you please help me on this.

        • John Graves

          Karthick,

          You use the term "array", but I think you just mean "Multiple Values".  Either way, once you make the call to the module function, you can assign the results to the page variables.  You just won't see it directly in the mapper unless you setup a datatype and define the return type.  But you don't need to do all this, you can just use the dot notation.  Note the screenshot.

          If you have issues still, I'm happy to send you a sample project.

          -John

          • Karthick S

            Hi John,

            Thanks for your response.

            yes exactly i just want to receive multiple values from a JS function and assign those values into page variables.

            But still am not achieve the solution for this.

            Kindly help me on this.

            It would be helpful if you send me sample project for this.

            • John Graves

              Sure...

              https://www.dropbox.com/s/87ybdjdhe6mo60f/VariablePassingTest.zip?dl=0

              This is a simple example.  There are three fields on the main page; which are tied to the three page variables.  When you press the button, it runs an action chain that returns three values (as the sample) and assigns these three to the page variables.

              • Karthick S

                Hi John,

                Thanks for your Update.

                It is very much helpful for me.

                But now i have a different scenario to implement the same thing using json object.

                I need to return json object with multiple values and assigns those json values to the variables.

                Example:

                [
                  {
                    "V_filename": "Karthick",
                    "V_filesize": "2mb",
                    "V_filetype": "pdf"
                  },
                  {
                    "V_filename": "David",
                    "V_filesize": "3mb",
                    "V_filetype": "txt"
                  },
                  {
                    "V_filename": "Dinesh",
                    "V_filesize": "1mb",
                    "V_filetype": "doc"
                  }
                ]

                 

                 

                • John Graves

                  This is getting closer to Shay's blog example...

                   

                  That might work and just reference it using returnValue[1].V_filename.  Or, you can name the array and use the returnValue.myValues[1].V_filename syntax.

                  {
                    "myValues": [
                      {
                        "V_filename": "Karthick",
                        "V_filesize": "2mb",
                        "V_filetype": "pdf"
                      },
                      {
                        "V_filename": "David",
                        "V_filesize": "3mb",
                        "V_filetype": "txt"
                      },
                      {
                        "V_filename": "Dinesh",
                        "V_filesize": "1mb",
                        "V_filetype": "doc"
                      }
                    ]
                  }
                  
                  • Karthick S

                    Hi John,

                    I tried like this

                     PageModule.prototype.multiValueSample = function(arg1) {
                        // arg1 not used
                        
                        
                        
                        var returnValues = {};
                       
                        returnValues[0].V_filename = "Karthick";
                        returnValues[0].V_filesize = "2mb";
                        returnValues[0].V_filetype = "pdf";
                        
                        returnValues[1].V_filename = "David";
                        returnValues[1].V_filesize = "1mb";
                        returnValues[1].V_filetype = "txt";
                        console.log("Return values: check");
                        console.log("Return values: "+JSON.stringify(returnValues));
                        return returnValues;
                      }

                     

                    But i got error in the console like

                    error running custom action: multiValueSample(undefined) TypeError: Cannot set property 'V_filename' of undefined
                        at PageModule.multiValueSample (main-arraytest-page.js:11)
                        at CallModuleAction.perform (callModuleFunctionAction.js:90)
                        at action.js:40

                     

                    Kindly help us to resolve this.

                    • John Graves

                      Yes.  This will cause an error.  Have you used JSON in Javascript before? This is not valid syntax.

                      When you defined "returnValues", it is an object "{}".  You can't then address it as an array returnValues[0].  Also, you need to use the push operation when adding items dynamically to the aray.

                      PageModule.prototype.multiValueSample = function() {
                        var returnValues = [];
                      
                        returnValues.push(
                         { "V_filename": "Karthick",
                           "V_filesize": "2mb",
                           "V_filetype": "pdf" }
                        );
                        returnValues.push(
                         { "V_filename": "David",
                           "V_filesize": "1mb",
                           "V_filetype": "txt" }
                        );
                      
                        return returnValues;
                      }
                      
                      

                       

      • Karthick S

        Hi shay,

        Thanks for your response.

        This blog is not relevant for my requirement.

        Kindly help on this.

        • John Graves

          Karthick,

          Shay's blog entry deals with passing and receiving true arrays (e.g. value[0],value[1],value[2],etc.)

          Again, I think you are just wanting to receive multiple values.

          -John

    • Karthick S

      Hi Shay,

      am getting error while return array.

      Cannot assign a primitive value to a target with 'object' type.

      Kindly assist us.

      Thanks,

      Karthick