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

Community Tip - You can Bookmark boards, posts or articles that you'd like to access again easily! X

Finding the number of fields in a datashape

Janicen
15-Moonstone

Finding the number of fields in a datashape

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.

ACCEPTED SOLUTION

Accepted Solutions
Arun_C
16-Pearl
(To:Janicen)

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;

 

Arun_C_0-1700034154141.png

 

Thanks & Regards,

Arun C

View solution in original post

5 REPLIES 5
Arun_C
16-Pearl
(To:Janicen)

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;

 

Arun_C_0-1700034154141.png

 

Thanks & Regards,

Arun C

Hi @Janicen  & @Arun_C 

 

You can avoid the loop by below code

 

let dataShapeFields = infoTable.dataShape.fields;
var result = Object.keys(dataShapeFields).length;

 

/VR

Janicen
15-Moonstone
(To: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.

Arun_C
16-Pearl
(To:Janicen)

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

Janicen
15-Moonstone
(To:Arun_C)

Very helpful snippets: remove and add field definitions worked. 

Thank you.

Announcements


Top Tags