Skip to main content
7-Bedrock
January 10, 2024
Solved

How to programatically check if ThingShape or ThingTemplate exists ?

  • January 10, 2024
  • 1 reply
  • 987 views

I have a scenario where I am executing a  child service (inputs: ThingShape, Thing and Boolean) from another wrapper service. Now the child service fails if I pass a ThingShape that does not exist, so I am trying to write something like:
If ThingShape Exists :
TS = TS_NAME
Else:
TS = undefined.

How do I achieve this ?

I have checked many OOTB services but none help with this.

TWX v9.3.4

Best answer by JD_10518393

if (!!!ThingShapes[TS]) {
TS = "gggg";
logger.info("ThingShape " + TS + " does not exist.");
} else {

logger.warn("ThingShape " + TS + "exists.");
}

1 reply

JD_105183937-BedrockAuthorAnswer
7-Bedrock
January 10, 2024

if (!!!ThingShapes[TS]) {
TS = "gggg";
logger.info("ThingShape " + TS + " does not exist.");
} else {

logger.warn("ThingShape " + TS + "exists.");
}

Rocko
19-Tanzanite
January 10, 2024

It's correct, but you could simply write

if (ThingShapes[TS])
 logger.warn("ThingShape " + TS + "exists.");
else 
 logger.warn("ThingShape " + TS + " does not exist.");

which is a bit cleaner and easier to read/less surprising (KISS principle).