community
cancel
Showing results for 
Search instead for 
Did you mean: 

Dev Space

Customize & extend the power of Alteryx. SDKs, APIs, custom tools, and more!
SOLVED

Cannot output data from custom HTML/JS tool

Highlighted

Hey Everyone,

 

Despite my best efforts, I can't seem to get a very basic javascript tool that I made to function.  I worked off of the example provided here: https://inspiringingenuity.net/2015/12/03/htmlsdk/

 

When I run the tool, I receive the error: "Error parsing json: Expected '0' fields from JavaScript but received '1' fields."  The goal of the tool is to accept a single row and field from the input connection, concatenate a value specified in a textbox, and then output that new concatenated value to a single a row and field on the output connection.  I think the error means that I've successfully passed my data through the engine and to the output connection, but that the output connection was not prepared to accept the data.  I can't post a zip file of the project folder but I'm happy to provide any additional information or code that would be useful.  The following code is from the engine file and from what I gather handles setting up the meta data on the output connection and then actually preparing the data for output.

 

 Alteryx.Plugin.PI_Init = function(config)
    {
      UserSubmittedText = config.Configuration["FieldTextBox1"];
    };

    var InputRecordInfo;
    var OutputRecordInfo;
   
    Alteryx.Plugin.II_Init = function(metaInfo)
    {
      InputRecordInfo = metaInfo.RecordInfo;
      OutputRecordInfo = {};
      
      var NewField = {};
      NewField.name = "Letters";
      NewField.type = "V_String";
      NewField.size = 100;
      
      OutputRecordInfo.Field.push(NewField);
      Alteryx.Engine.SendMessage.RecordInfo("Output", OutputRecordInfo)
    };

    Alteryx.Plugin.II_PushRecords = function(data)
    {
      var myRecords = [];
      var myRow = [];
      var theString = data.Records[0][0];
      
      theString = theString + " " + UserSubmittedText;
        Alteryx.Engine.SendMessage.Info(theString);
      myRow.push(theString);
      myRecords.push(myRow);
      Alteryx.Engine.SendMessage.PushRecords("Output", myRecords);
    };

This seems so basic that I feel foolish for asking.  However, I feel less foolish asking than wasting any more time debugging it further.  If anyone out there has any ideas, I would certainly appreciate it.  It doesn't seem very well documented, but as soon as I can figure out the API's hooks and bridge the gap to javascript, there's soooo much that can be done with a little programming!  Thanks in advance gents and ladies!

Well it was a simple problem.  I forgot some basic JavaScript on arrays and objects.  It turns out if you take this:

    Alteryx.Plugin.II_Init = function(metaInfo)
    {
      InputRecordInfo = metaInfo.RecordInfo;
      OutputRecordInfo = {};
      
      var NewField = {};
      NewField.name = "Letters";
      NewField.type = "V_String";
      NewField.size = 100;
      
      OutputRecordInfo.Field.push(NewField);
      Alteryx.Engine.SendMessage.RecordInfo("Output", OutputRecordInfo)
    };

All you need to do is initialize OutputRecordInfo.Field as an array, so that you get:

    Alteryx.Plugin.II_Init = function(metaInfo)
    {
      InputRecordInfo = metaInfo.RecordInfo;
      OutputRecordInfo = {};
      OutputRecordInfo.Field = [];
      
      var NewField = {};
      NewField.name = "Letters";
      NewField.type = "V_String";
      NewField.size = 100;
      
      OutputRecordInfo.Field.push(NewField);
      Alteryx.Engine.SendMessage.RecordInfo("Output", OutputRecordInfo)
    };

It works fine.  Facepalm, hope this helps someone else trying to build their first tool!  Solution will go to first person that posts a decent javascript joke.

ACE Emeritus
ACE Emeritus

I believe you can give yourself the solution: you earned it.  But here's a bad joke anyway: "Don't call me.  I'll callback you."