0

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Gaetan L.
  • 649
  • 6
  • 20

1 Answers1

0

You cannot use the functions like findManagedObjectById from within expression because you are basically leaving the esper context for executing the expression and then the function is no longer known.

If you want to change the object within the expression I would query it before and then give it to the expression in the parameters.

It could be that when passing it to an expression it becomes a Map (not sure about that need to test myself)

getString(e.source, "value") doesn't look right. What is e. If e is EventCreated than you should use e.event.source.value without any function

TyrManuZ
  • 2,039
  • 1
  • 14
  • 23
  • getTripId(findManagedObjectById(getString(e.source, "value"))) works correctly, I can display my managed object in my function and I can see the fields I wanna access, but I cannot access them. – Gaetan L. Nov 02 '16 at 08:36