Skip to main content
16-Pearl
March 5, 2024
Solved

DataTable entry update service query

  • March 5, 2024
  • 1 reply
  • 860 views

I have only one entry in my datatable with 4 fields. One of the field is primary key for that datashape. Now my question is i want to update this entry but that primary key value would also be updated. Now the confusion is how can i do that? I have the service UpdateDataTableEntry and AddOrUpdateDataTableEntry but these both services creates a new entry because the value of the primary key changes too. what is the best solution in this regard should i delete the entry first and then add the new one or how you can see this problem?

currently i set another primary key and just updates the value with this hardcoded primary key. 

 

const serviceName = "AddOrUpdateWaxingCan";
const LOG_PREFIX = me.name + " :: " + serviceName + " :: ";
logger.info(LOG_PREFIX + "Start Service");


let waxingDatatableEntries = Things["QGate.WaxingCan.DT"].GetDataTableEntryCount();
let dataTable = Things["QGate.WaxingCan.DT"].GetDataTableEntries({
 maxItems: waxingDatatableEntries 
});

 let dateNow = Date.now();
 // Erstellen eines neuen Eintrags für deine DataTable
 let values = Things["QGate.WaxingCan.DT"].CreateValues();
 values.chargeExpireDate = chargeExpireDate;
 values.picture = picture;
 values.chargeNumber = chargeNumber;
 values.shiftExpireDate = dateNow * 1000 + me.shiftExpireTime;
 values.primary =1;

 
 try { 
 
 Things["QGate.WaxingCan.DT"].AddOrUpdateDataTableEntry({values: values});
 result = dataTable;
 } catch (error) {
 logger.error(LOG_PREFIX + "Error updating entry in DataTable: " + error.message);
 }

 

 

Best answer by AdamK_93

Hi Jamal, 

 

If I'm not wrong, primary key values cannot be updated. It should serve the purpose of a unique record. So immutable.

Additionally the service you invoke works just fine: 

 

AddOrUpdateDataTableEntry Service:

  • Use the AddOrUpdateDataTableEntry service to update an existing entry in your DataTable.
  • If the primary key value changes, this service will automatically update the corresponding entry.

As a workaround I cannot see any solution other than what you have mentioned. 

1 reply

AdamK_9313-AquamarineAnswer
13-Aquamarine
March 5, 2024

Hi Jamal, 

 

If I'm not wrong, primary key values cannot be updated. It should serve the purpose of a unique record. So immutable.

Additionally the service you invoke works just fine: 

 

AddOrUpdateDataTableEntry Service:

  • Use the AddOrUpdateDataTableEntry service to update an existing entry in your DataTable.
  • If the primary key value changes, this service will automatically update the corresponding entry.

As a workaround I cannot see any solution other than what you have mentioned.