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

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Adding rows to an infotable

jjuran
1-Newbie

Adding rows to an infotable

Hey there,

In the Thingworx scripts, how can you add a row to an infotable that is already existing? Is see there is an AddRow option and I try to add that to my me.thisInfo object, but it requires a "row" object as an argument and I do not know how to set that opject up. I tried a few ways and it accepted it but with no addition to the Infotable.

Within the infotable the data shape just has the field definition of "Names" Just to keep it simple for now.

Thanks for any help!

6 REPLIES 6

For what you say, should be that easy:

me.thisInfo.AddRow({ Names: "whatever Name"});

But for the record, in order to Add Row to a Property Infotable, better you Clone Infotable, modify it and then set it again the property:

  1. var clonedData = Resources["InfotableFunctions"].Clone( { t1: me.thisInfo });
  2. clonedData.AddRow({ Names: "whatever Name" });
  3. me.thisInfo = clonedData;

Apart of the previous explanation, you may read this post: Re: Update Single InfoTable Row with a Service and what Pai Chung says about writing on infotable properties, you can simplify my code with:

  1. var clonedData = me.thisInfo;
  2. clonedData.AddRow({ Names: "whatever Name" });
  3. me.thisInfo = clonedData;

But it's important that you copy and set again the infotable property, otherwise it won't be persisted.

Thanks for the help! One more add on,

If I wanted to add a row with also an Integer id value it would be like

AddRow({Names: "Name", id: 1});

or now is that wrong? It doesn't seem to want to work for me not sure why. Thanks!

Also now when I add a row it adds one, but it is blank. Or set to the default if I set a default. Not sure why. Even when using a simple datashape with just a name field.

Hi, you added id field to the corresponding DataShape?

kpatel-21
6-Contributor
(To:CarlesColl)

i am trying to add multiple rows into infotable at the time of creating service so would it be possible to add multiple row.

 

here is my code snippet and error details

 

var result ;

// adding one row
result = Things[me.Part_1_Summery].GetPropertyValues();

 

var params = {
t1: result /* INFOTABLE */
};

// result: INFOTABLE
var cloantable = Resources["InfoTableFunctions"].Clone({t1:me.result});
cloantable = Things[me.Part_2_Summary].GetPropertyValues();
me.result = cloantable;

 

Error:  Erro : Wrapped java.lang.Exception: Property : [result] not found on Base_Machine_1 Cause: Property : [result] not found on Base_Machine_1

Hi Carles Coll​,

I have a similar use case, If there is an entry in infotable add a new row else create a new infotable then add a new row. To give an idea here is my use case.

1) I have a thing by name Test.

2) Test Thing has a property by name "data" of type infotable which has datashape ThingPredictor.apmsolutiondefectclassifier_Condition_207.InputParamsdataDataShape. This datashape has fields like _B1, _B2, _B3, _B4, _B5, _B6.


Below is the code but I am getting TypeError: Cannot call method "Clone" of null (predictNewScore#35)


var length = me.data.getRowCount();

logger.warn(" length:"+length);

if(0==length) {

logger.warn(" Empty data creating a new infotable:");

// Create the infotable that will be sent into SetPropertyValues()

var props = Resources["InfoTableFunctions"].CreateInfoTable({ infoTableName: undefined });

// Set up the fields on the Infotable to match the properties on the Thing that you want to set

// Each field takes the name of the property (case sensitive) and the Base Type for the property.

props.AddField({ name: 'data', baseType: 'INFOTABLE' });

var params = { 

     infoTableName : "InfoTable", 

     dataShapeName : "ThingPredictor.apmsolutiondefectclassifier_Condition_207.InputParamsdataDataShape" 

     }; 

var resultdata = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

var newEntry = new Object();

newEntry._B1 =B1;

newEntry._B2 =B2;

newEntry._B3 = B3;

newEntry._B4 =B4;

newEntry._B5 =B5;

newEntry._B6 = B6;

resultdata.AddRow(newEntry);

// Create a row and set the values for the fields to be updated. 

    // Note, SetPropertyValues only processes the first row in the infotable!

    var row = new Object();

    row.data = resultdata;

    props.AddRow(row);

    // Run the SetPropertyValues service

    me.SetPropertyValues({ values: props });

} else {

     logger.warn(" Not an Empty data:");

     var clonedData = Resources["InfotableFunctions"].Clone( { infoTableName: me.data });

     logger.warn(" Cloning done:");

     var params = { 

     infoTableName : "InfoTable", 

     dataShapeName : "ThingPredictor.apmsolutiondefectclassifier_Condition_207.InputParamsdataDataShape" 

       }; 

     var resultdata = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

     var newEntry = new Object();

     newEntry._B1 =B1;

     newEntry._B2 =B2;

     newEntry._B3 = B3;

     newEntry._B4 =B4;

     newEntry._B5 =B5;

     newEntry._B6 = B6;

     resultdata.AddRow(newEntry);

// Create a row and set the values for the fields to be updated. 

    // Note, SetPropertyValues only processes the first row in the infotable!

    var row = new Object();

    row.data = resultdata;

clonedData.AddRow(row);

logger.warn(" New row is added invoking serproperty service:");

// Run the SetPropertyValues service

   me.SetPropertyValues({ values: clonedData});

}



Top Tags