In one of my Esper rules, I need to get a specific (custom) property of a managed object, or a default value if it doesn't exist. So I decided to do this in a js expression. I started like this:
create expression Number js:getTripId(deviceId) [
function getTripIdorOrDefault(managedObject) {
// ...
}
getTripIdorOrDefault(findManagedObjectById(deviceId));
];
When testing however, the output tells me that "findManagedObjectById is not defined". So be it, I rewrite my function so that it takes the managedObject directly and continue like this:
create expression Number js:getTripId(managedObject) [
function getTripIdorOrDefault(managedObject) {
// treatments for nulls...
return managedObject.tripId;
}
getTripIdorOrDefault(managedObject);
];
That I call with:
getTripId(findManagedObjectById(getString(e.source, "value")))
However I get a null value. When I output the managedObject, I can see it's not the case. I then tried to access other "default" properties like text, time and type and get null as well.
So I tried to use the bracket notation myObject['myProperty']
but Cumulocity wont let me save my module (incorrect syntax). I tried to use Object.keys(managedObject)
to check the available properties of my object but it seems the javascript version is older than 1.8.5. And the Cumulocity getString, getNumber, etc. functions are not available in javascript as I understand it.
So, how can I manipulate my device objects in javascript expressions?