Skip to main content
1-Visitor
April 11, 2017
Solved

Why? ThingworxInfoTableAdapter cannot be cast to com.thingworx.types.InfoTable

  • April 11, 2017
  • 1 reply
  • 2886 views

Here is quick outline of my service. When I run this inside of Thinkworx as a service, I get 4 data rows. Perfect. So now I want to return json and below are my attempts (logged). Any ideas on why this error occurs? I have tried every json-converter function I can find.

var DetailedLogging = true;

logger.warn("here is my input value....." + lastDTSCheck);

try {

    var devices = Things['ATS.Org'].ListDevices();

    var device;

    var result = null;

    result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({

       infoTableName : "InfoTable",

        dataShapeName : "OrgDateCheck"

    });

    var cnt = 0;

    for each(device in devices.rows) {

        device = Things[device.EntityName];

       

        if (device.GetLastModifiedDate() > lastDTSCheck) {

            cnt = cnt + 1;

            logger.warn("My counter is now at: " + cnt);

           if (result == null) {

               result = Things[device.name].GetPropertyValues();

               result.name = device.name;

           } else {

               result.AddRow(Things[device.name].GetPropertyValues().rows[0]);

               result.rows[result.getRowCount() - 1].name = device.name;

           }

        }

    }

    //logger.warn("debugging the output? ..... " + result);

    //logger.warn("debugging the output 2 as json string" + result.ToJSON() );

    //logger.warn("debugging the output 3 as stringify: " + JSON.stringify(result) );

    //logger.warn("debugging the output 4 as toString: " + result.toString() );

    //result = JSON.stringify(result);

    result.ToJSON();

} catch (err) {

  logger.warn(me.name + ".MEB() [err]: " + err + ", " + err.lineNumber);

}

Best answer by jkaczynski

Hello Michael Boyle​,

Have you tried to replace


result.ToJSON();

with

result = result.ToJSON();

? In your code you do not assign the JSON result to the result variable, so the InfoTable (not JSON) is returned. Then just set the output base type to JSON and it should return JSON with InfoTable data.

Regards,

J.

1 reply

1-Visitor
April 11, 2017

Hello Michael Boyle​,

Have you tried to replace


result.ToJSON();

with

result = result.ToJSON();

? In your code you do not assign the JSON result to the result variable, so the InfoTable (not JSON) is returned. Then just set the output base type to JSON and it should return JSON with InfoTable data.

Regards,

J.

mboyle1-VisitorAuthor
1-Visitor
April 11, 2017

Thanks -- that did solve my problem with getting json data. Alas, it also created a new one: now the service returns ALL of the data and not just the 4 items I was expecting! It is always something!