Skip to main content
12-Amethyst
November 20, 2024
Solved

Auto increment ID key Datatable from Mashup

  • November 20, 2024
  • 3 replies
  • 3900 views

Hello,

I want to add information from Mashup and I want to have an ID incremental from Mashup that is start from 1.

I try to find something but anything use some service. I have some services already and I don't want to do this incremental from service, I want to do it from Mashup.

 

Thank you.

Best answer by tenegabi

I made a service separate that add automatically the number.

The service is only call in Mashup and don't need another specifies connexions except Clicked button trigger and Loaded Trigger from Mashup. 

try{
    var dtValues= Things["AuditItBOS_Schedule"].GetDataTableEntries({
maxItems: 99999 /* NUMBER {"defaultValue":500} */
    });
    
    var dtMaxParams = {
            t: dtValues, // INFOTABLE
            columns: "ID", // STRING
            aggregates: "MAX", // STRING
            groupByColumns: undefined // STRING
          };
        var dtMax = parseInt(Resources["InfoTableFunctions"].Aggregate(dtMaxParams).MAX_ID); //INFOTABLE
 
}
catch(err){
    result = "Service call error ["+me.name + "]: on line " + err.lineNumber + ". Error Name: " + err.name + ". Error Message: " + err.message;
}
 
var result = dtMax+1;

3 replies

14-Alexandrite
November 20, 2024

@tenegabi  WIll GUID work for your requirement ?

tenegabi12-AmethystAuthor
12-Amethyst
November 20, 2024

Hello @Bikash_Panda ,

I don't know what to tell you, I have try already but it create this type of input: 9beb3c03-0af8-4642-82fa-1114dda72e10
I want to create only number and start from 1, it is simple to identify by users and by me. 

Thank you for the answer

14-Alexandrite
November 20, 2024

In that case, you may take a look at,

https://github.com/carlescm/ThingWorxConcurrencyExtension

 

This will help you generate unique number, but each time it will increment the value. So if you increment the value and dont insert to datatable, then your values will not be consecutives though.

16-Pearl
November 20, 2024

Hi Tenegabi.

 

I'm guessing the data will go in a DataTable entity? If that's the case I'm not sure what the best way would be, What I would do is on the Add service's Thing, I would put a property (like LastCreatedId) of type number and persistent, add every time the service adds a record I would look at that value and add 1. But I'm not sure it would be robust against odd scenarios, like concurrency.

 

If the data is stored in a database, there's a much easier way to do it. You can give the Identity setting to a column, this will do it automatically for you. Here's how you can set it with the wizard in MSSQL : 

mstarnaud_0-1732112926630.png

 

tenegabi12-AmethystAuthorAnswer
12-Amethyst
December 6, 2024

I made a service separate that add automatically the number.

The service is only call in Mashup and don't need another specifies connexions except Clicked button trigger and Loaded Trigger from Mashup. 

try{
    var dtValues= Things["AuditItBOS_Schedule"].GetDataTableEntries({
maxItems: 99999 /* NUMBER {"defaultValue":500} */
    });
    
    var dtMaxParams = {
            t: dtValues, // INFOTABLE
            columns: "ID", // STRING
            aggregates: "MAX", // STRING
            groupByColumns: undefined // STRING
          };
        var dtMax = parseInt(Resources["InfoTableFunctions"].Aggregate(dtMaxParams).MAX_ID); //INFOTABLE
 
}
catch(err){
    result = "Service call error ["+me.name + "]: on line " + err.lineNumber + ". Error Name: " + err.name + ". Error Message: " + err.message;
}
 
var result = dtMax+1;
Rocko
19-Tanzanite
December 6, 2024

For starting with "I don't want to do this incremental from service, I want to do it from Mashup." you ended up with a solution that uses a service AND is pretty wasteful with resources, as @Bikash_Panda mentioned. No offense, but that is not a good solution and you will pretty likely have collisions (as Bikash also mentioned), because you created a race condition.

You can try to open that mashup with two machines and click the button to execute the service at the same time. Multithreading means any service can be paused in execution so another thread can be serviced. Your service is not synchronized, therefore two threads could have same dtMax, but you do you.

 

tenegabi12-AmethystAuthor
12-Amethyst
December 9, 2024

Hello @Rocko 

And then witch is the best solution?

First of all I don't want to use a service, I want to make some solution from Mashup, but that was my solution. 

 

Thank you.