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

Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X

Best Way to Code

VC_10938534
11-Garnet

Best Way to Code

Hi Everyone, I am currently running  Thingworx version 9.3.5,
I am currently trying to run a code on replacing X number for a message what would be the best way to do that?
Example below:

let message = "";

switch (number) {

    case 253:

        message = "On Standby";

        break;

ACCEPTED SOLUTION

Accepted Solutions
Velkumar
19-Tanzanite
(To:VC_10938534)

Yes your method is right.

 

But we can optimize it by bit

var messageValue = "Message not found";

// result: INFOTABLE dataShape: ""
let data = Things["DATATABLE"].GetDataTableEntryByKey({
	key: "KEYVALUE" /* STRING */
});

if(data.length > 0){
    messageValue = data.messageField;
}

 

Also refer -Best Practices for Coding in JavaScript (ptc.com)

 

/VR

View solution in original post

4 REPLIES 4
Velkumar
19-Tanzanite
(To:VC_10938534)

Hi @VC_10938534 

 

You can create an DataTable with number as primary key to store this data.

 

In script, you can use GetDataTableEntryByKey service to get message value which will avoid long if or switch condition in code.

 

Also in future if you need to add more number and message, you can add entry directly in DataTable without altering the script.

 

/VR

Im fairly new to coding would this be right?
var params = {
tableName: "YourDataTableName",
key: "YourKeyToSearch"
};


var result = Things["YourDataTableThing"].GetDataTableEntryByKey(params);

if (result !== undefined && result !== null) {
var messageValue = result.messageField;

} else {

Velkumar
19-Tanzanite
(To:VC_10938534)

Yes your method is right.

 

But we can optimize it by bit

var messageValue = "Message not found";

// result: INFOTABLE dataShape: ""
let data = Things["DATATABLE"].GetDataTableEntryByKey({
	key: "KEYVALUE" /* STRING */
});

if(data.length > 0){
    messageValue = data.messageField;
}

 

Also refer -Best Practices for Coding in JavaScript (ptc.com)

 

/VR

olivierlp
Community Manager
(To:VC_10938534)

Hi @VC_10938534
I wanted to follow up with you to see if your question has been answered.
If so, please mark the reply as the Accepted Solution for the benefit of other members who may have the same question.
Of course, if you have more to share on your issue, please let the Community know. 

Thanks,
 

Olivier
Announcements


Top Tags