5 Replies Latest reply on Dec 8, 2017 4:20 AM by ahawaldar-2 RSS
    jjuran 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!

      • Re: Adding rows to an infotable
        carlesc Heavyweight Champ

        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;
          • Re: Adding rows to an infotable
            carlesc Heavyweight Champ

            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.

            • Re: Adding rows to an infotable
              ahawaldar-2 Explorer

              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});

               

              }