Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X
Hello,
I am having JSON property in Thing which contains history of changes. I am trying to sort that JSON property and fetch the first entry. I am trying to fetch this using derive fields snippet but my expression is containing comma and thingworx is considering characters after comma as new expression. How to handle this? Tried using encoded characters, still it didn't work.
Is there a way to write function and call in derive fields expression?
-thanks,
Abhiram
You can do something like "My expression part 1 " + "'special character to include'" + "My expression part 2"
@PaiChung Hello, Thank you for your reply.
I have tried that still it didn't work, have a look below please:
var allAssets = ThingShapes["Factory.Assetshape"].QueryImplementingThingsWithData(params);
var params = {
types: "STRING" /* STRING */,
t: allAssets /* INFOTABLE */,
columns: "LastUpdate" /* STRING */,
expressions: "JSON.parse(Things[name].History).sort(function (a" + "','" + "b) {return b.Timestamp.localeCompare(a.Timestamp);})[0].Timestamp" /* STRING */
};
// result: INFOTABLE
result = Resources["InfoTableFunctions"].DeriveFields(params);
Any other way to handle this?
Thanks!
If ... you can do it all with just one value being passed in, you can call another service in your expression.
If I'm passing single expression it's working fine but if I combine with other expression it's not taking the name.
Working:
expressions: "Things['Factory.Generic'].GetAssetLastUpdate({thingName: name});" /* STRING */
Combined with other expression it's not working:
expressions: "BookValueDate.toISOString()ReplacementDate.toISOString(),Things['Factory.Generic'].GetAssetLastUpdate({thingName: name})" /* STRING */
I still haven't found a way around that myself unfortunately. It's the comma that is the issue unfortunately.
So at this point you probably just have to loop through the records and build the table row by row.
@PaiChung Thanks for checking, I'm already looping through records. Was looking how to handle this in expression.