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

Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X

auto increment primary key in the datatable

xiaoqw
15-Moonstone

auto increment primary key in the datatable

var week = me.GetWeekNumber({ date: when }); // Copied from Stack Overflow, didn't test its correctness
var month = when.getMonth(); // Zero-based
var year = when.getFullYear();
var correctmonth = month +1;
var query = me.CreateValuesWithData({ values: {
value: value,
Kategory: Kategory,
Department:Department,
Line: Line,
SKU : SKU,
KPI_Details: KPI_Details,
Target: Target,
Other_Target : Other_Target,
Other_value : Other_Value,
week: week,
month: correctmonth,
year: year
} });
query.id = me.ID;
var params = {
sourceType: undefined /* STRING */,
values: query /* INFOTABLE*/,
location: undefined /* LOCATION */,
source: undefined /* STRING */,
tags: undefined /* TAGS */
};
if(me.ID !== query.id){
stop();
}
var result = me.AddDataTableEntry(params);

 

clipboard_image_0.png

when I refresh the page, it will auto increment one more empty row in the grid widget. 

 

how to fix this problem? 

14 REPLIES 14
PaiChung
22-Sapphire I
(To:xiaoqw)

What exactly are you trying to accomplish?

Nothing in your code shows any sort of 'auto increment' of a primary key. This isn't something that Thingworx supports (compared to an SQl db).

I recommend if you need to track unique records, assign the Primary fields in the DataShape

if you just need to tag unique records, use GenerateGUID to assign a GUID per record

 

Perhaps if you explain your use case that could help generate proper recommendations.

xiaoqw
15-Moonstone
(To:PaiChung)

Hi @PaiChung,

I just want to make sure everytime I refresh the page, primary key cannot auto increment and once I click "add" button, the new data will show in the grid widget.  Primary key should be in sequence.

Hello,

 

Why don't you increment your primary key directly in the service which adds new record, right before adding it?

 

/ Constantine

xiaoqw
15-Moonstone
(To:Constantine)

Hi, when refresh the page, system will auto generate a new record in the grid widget. If using your method, how to edit the service? Please show me an example, thank you
PaiChung
22-Sapphire I
(To:xiaoqw)

If a page refresh is creating a record, then I would revisit your flow of services.

Sounds like you are trying to generate a placeholder which unfortunately becomes an actual record.

If you are just looking to create records with unique IDs I would just provide input fields and generate the ID upon 'commit'.

If you want to have it in a 'table' You can always generate  dummy infotable with one row and use that for input through an editable grid.

xiaoqw
15-Moonstone
(To:PaiChung)

Hi@PaiChung

“If you are just looking to create records with unique IDs I would just provide input fields and generate the ID upon 'commit'. “Based on your opinion, Could u show me specific example? Like with coding in details
xiaoqw
15-Moonstone
(To:Constantine)

This video is currently being processed. Please try again in a few minutes.
(view in My Videos)

this is reference which I refresh the page.

Hi Mr. Xiao,

 

Actually it seems like each time your mashup is refreshed, your addEntries service is triggered once. While when you refresh the mashup, the inputs are blank, thus you're getting empty records.

 

2 solutions for your issue:

1. Check out how mashup refresh/ loaded event is affecting the service, stop the triggering binding, problem solved.

2. Your service need several input properties, so define some of the input to be "Required", then even if the service is trigger automatically, the service would fail, and you won't get blank records again, just some script errors.

xiaoqw
15-Moonstone
(To:zyuan1)

Hi @zyuan1

sounds like your solution 1 can work, but if I cancel triggering binding, my input cannot be successful. maybe Could u check my coding? is there any changes I need to make? I think main issue is coding.Thank you.

I don't know what's this doing

if(me.ID !== query.id){
stop();

But other parts of your script are very general service input configurations, so I still doubt the error in binding.

 

What do you mean by saying "if I cancel triggering binding, my input cannot be successful" ?

This service should only be triggered by Pressing the Add button, the input parameter bindings should be remained. 

 

While if this doesn't work, I'd suggest you create a TS case since the behavior seems odd.

xiaoqw
15-Moonstone
(To:zyuan1)

1.JPG2.JPG3.JPG

here is my binding. Maybe here is some problem. Please help me to check it again. thank you

here is my latest code. Still have some problem.

var week = me.GetWeekNumber({ date: when }); // Copied from Stack Overflow, didn't test its correctness
var month = when.getMonth(); // Zero-based
var year = when.getFullYear();
var correctmonth = month +1;
id = me.GetDataTableEntryCount();

me.AddDataTableEntries({ values: me.CreateValuesWithData({ values: {
id:id+1,
value: value,
Kategory: Kategory,
Department:Department,
Line: Line,
SKU : SKU,
KPI_Details: KPI_Details,
Target: Target,
Other_Target : Other_Target,
week: week,
month: correctmonth,
year: year
} }) });

 

Cancel the Mashup-loaded binding to Execute AddorUpdateDatatableEntries service, that's what I'm talking about.

 

Shouldn't your service be triggered by the Add button - click event?

xiaoqw
15-Moonstone
(To:zyuan1)

Yes. Sure.i bind it with ‘QueryDatatableEnrties’ and ‘Addrow’ services

But in your 1.JPG screenshot it's not. Fix it and check.

Top Tags