Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X
Dear Community,
I have been running into an issue today and I am calling for help. TL;DR is at the end. I attached an export of a demo thing (DemoJsonParameterToEvent), I will refer to it in the following. For quick reference here is the code of the service:
logger.debug("ParameterJson: "+ParameterJson); logger.debug("ParameterJson.FieldNumber: "+ParameterJson.FieldNumber); logger.debug("Type of ParameterJson: "+(typeof ParameterJson)); var objectToCompare = { FieldNumber: ParameterJson.FieldNumber, FieldString: ParameterJson.FieldString }; logger.debug("objectToCompare: "+objectToCompare); logger.debug("objectToCompare.FieldNumber: "+objectToCompare.FieldNumber); logger.debug("Type of objectToCompare: "+(typeof objectToCompare)); var parsedParameter = JSON.parse(ParameterJson); logger.debug("parsedParameter: "+parsedParameter); logger.debug("parsedParameter.FieldNumber: "+parsedParameter.FieldNumber); logger.debug("Type of parsedParameter: "+(typeof parsedParameter)); if (UseParameterForEvent) { me.DemoEvent(ParameterJson); } else { me.DemoEvent(objectToCompare); me.DemoEvent(parsedParameter); }
DemoJsonParameterToEvent has one event (DemoEvent) which is supposed to be fired by a service (FireEvent). The service has an input parameter (ParameterJson), it shall be passed to the event. For easy testing I also added a boolean parameter (UseParameterForEvent).
The service starts with logging ParameterJson, its attribute FieldNumber and its type. The access to its attributes is working, ParameterJson is working like an object here. Looking at the ScriptLog it confirms the type object BUT unlike a "full" object it also shows the original JSON ({"FieldNumber":"Number321","FieldString":"String123"}) instead of [object Object]. This indicates ParameterJson could be just a string.
Next I create a new object (objectToCompare) with identical attributes and values and log this, too. Then I create a new object (parsedParameter) by parsing the parameter. The log has no surprise this time. Here again ParameterJson acts like a JSON string as parsing works fine and returns the expected result.
Finally I call the event, depending on UseParameterForEvent I call it (true) once with the original ParameterJson or (false) twice with the two objects created in the service. The false case works perfectly fine, the true case does not. The error message is:
[message: Execution error in service script [FireEvent] :: Invalid parameter type for event : [DemoEvent] on DemoJsonParameterToEvent]
TL;DR: I want to pass a parameter of a service to an event but it throws an error (line 19). My ultimate questions are:
1. Why?
2. What precise type has ParameterJson?
3. Is there a better way (than parsing or copying) to pass the object to the event?
(4. Is this documented somewhere and I just googled the wrong terms?)
Thanks
Benny
Solved! Go to Solution.
So I talked to my PTC consultant and he could me help here. From what I understood that JSON is basically a string and can only be interpreted as an object by Javascript. What I called a "full" object is a so called literal, I found some information here and here.
Leasson learned: Do not confuse this and if you really need an object (e. g. to pass it to an event), parse it first.
So I talked to my PTC consultant and he could me help here. From what I understood that JSON is basically a string and can only be interpreted as an object by Javascript. What I called a "full" object is a so called literal, I found some information here and here.
Leasson learned: Do not confuse this and if you really need an object (e. g. to pass it to an event), parse it first.