Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X
Hi all
In relation to this article: https://community.ptc.com/t5/IoT-Tips/DataShape-Nuances-Tips-Tricks/ta-p/817583?search-action-id=107104588871&search-result-uid=817583,
How can one find the number of fields in a datashape?
I have tried this:
let result = infotableData.DataShapeName.fields.length;
However this is the error I keep getting: TypeError: Cannot read property "fields" from undefined.
Thank you.
Solved! Go to Solution.
Hi @Janicen ,
Im not sure why "let result = infotableData.DataShapeName.fields.length;" is not working.
But you can try this below code to find the field count from infotable.
let dataShapeFields = infotableData.dataShape.fields;
var fieldCount = 0;
for (let fieldName in dataShapeFields) {
fieldCount++;
}
result = fieldCount;
Thanks & Regards,
Arun C
Hi @Janicen ,
Im not sure why "let result = infotableData.DataShapeName.fields.length;" is not working.
But you can try this below code to find the field count from infotable.
let dataShapeFields = infotableData.dataShape.fields;
var fieldCount = 0;
for (let fieldName in dataShapeFields) {
fieldCount++;
}
result = fieldCount;
Thanks & Regards,
Arun C
In addition to my question above, 1. Is it possible to delete a datashape using a service/snippet? 2. Can I add a field definition to a datashape dynamically.
PS this data shape is not linked to a table, therefore something like this: infotableData.AddField({ name: ""New Field, baseType: "NUMBER" }); will not work as it will only store the "New Field" temporarily when executing the service and not in the actually datashape entity.
I have created a datashape dynamically using a service:
let params2 = {
name: "DB_name" /* STRING */,//Change name
description: "undefined" /* STRING */,
projectName: undefined /* PROJECTNAME */,
fields: My_Fields /* INFOTABLE */,
tags: undefined /* TAGS */
};
// no return
Resources["EntityServices"].CreateDataShape(params2);
In certain scenarios I would like to add or remove fields from this datashape. If this is not possible then I would rather delete the entire DS and recreate it but this time the new field will be include in the "My_Fields" table.
Thank you.
Hi @Janicen ,
You can add or remove the fields based on the below 'Datashape' services of AddFieldDefinition and RemoveFieldDefinition-
DataShapes["<<YOURDATASHAPENAME>>"].AddFieldDefinition({
name: undefined /* STRING */,
description: undefined /* STRING {"defaultValue":""} */,
type: undefined /* BASETYPENAME */,
ordinal: undefined /* INTEGER {"defaultValue":0} */,
primaryKey: undefined /* BOOLEAN {"defaultValue":false} */,
dataShape: undefined /* DATASHAPENAME */
});
DataShapes["<<YOURDATASHAPENAME>>"].RemoveFieldDefinition({
name: undefined /* STRING */
});
If you want to delete the entire datashape entity please find this article - https://www.ptc.com/en/support/article/CS237875
Thanks & Regards,
Arun C
Very helpful snippets: remove and add field definitions worked.
Thank you.