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

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

To generate dynamic values infotable while inserting/updating data table

nbhagtani
12-Amethyst

To generate dynamic values infotable while inserting/updating data table

Am writing service to add or update record in data table and input to my service is "Tag" which will hold value like "Temperature".

My data table has fields like device_name, temperature, flow, pressure, etc (8 other fields).

So at run time i will get to know which field to update of data table but "AddOrUpdateDataTableEntry" service takes "values" in its params and this value is like empty

infotable which gets created like this :

var values = me.CreateValues();

values.DeviceName = deviceName;

values.Temperature = value;

values.Pressure1 = value;

values.Flow = value;

i dont want to check if tag value is "temperature" then values.Temperature = value because i need to write 10 if conditions as i have 10 fields

am unable to append tag value(temperature) to values.

how to create variable for above values at run time?

ACCEPTED SOLUTION

Accepted Solutions
AnnaAn
14-Alexandrite
(To:nbhagtani)

Dear Nisha,

Did you consider to use a for iterator to loop your Data Table fields and match it with your Tag, and then set value accordingly?

Here is my code:

Attached here:

// result: INFOTABLE dataShape: EntityList

var fields = Things["DT1"].GetFieldNames();

// result: INFOTABLE

var values = Things["DT1"].CreateValues();

values.id=6;//this is the primary ID you have to set a value to.

var tableLength = fields.rows.length;

for (var x = 0; x < tableLength; x++) {

var field = fields.rows;

  if(field.name==Tag)

    values[field.name]=value;

  

}

var params = {

sourceType: undefined /* STRING */,

values: values /* INFOTABLE*/,

location: undefined /* LOCATION */,

source: undefined /* STRING */,

tags: undefined /* TAGS */

};

// result: STRING

var id = Things["DT1"].AddOrUpdateDataTableEntry(params);

I set an id for the Data Table static but you could set it with your own dynamic way.

Thanks,

Br,

Anna

View solution in original post

2 REPLIES 2
AnnaAn
14-Alexandrite
(To:nbhagtani)

Dear Nisha,

Did you consider to use a for iterator to loop your Data Table fields and match it with your Tag, and then set value accordingly?

Here is my code:

Attached here:

// result: INFOTABLE dataShape: EntityList

var fields = Things["DT1"].GetFieldNames();

// result: INFOTABLE

var values = Things["DT1"].CreateValues();

values.id=6;//this is the primary ID you have to set a value to.

var tableLength = fields.rows.length;

for (var x = 0; x < tableLength; x++) {

var field = fields.rows;

  if(field.name==Tag)

    values[field.name]=value;

  

}

var params = {

sourceType: undefined /* STRING */,

values: values /* INFOTABLE*/,

location: undefined /* LOCATION */,

source: undefined /* STRING */,

tags: undefined /* TAGS */

};

// result: STRING

var id = Things["DT1"].AddOrUpdateDataTableEntry(params);

I set an id for the Data Table static but you could set it with your own dynamic way.

Thanks,

Br,

Anna

nbhagtani
12-Amethyst
(To:AnnaAn)

Thanks Anna it worked

Announcements


Top Tags