Avatar

Group add broke Geotab database

0

Hi

So, I'm working on a program that requires me to add groups via SDK. I'm quite new at coding, really, but feel as though my code here should have work. In fact, it originally did. However, after modifying it and trying again, it failed (for reasons that became immediately obvious). After this second attempt, I decided to remove the existing groups I'd previously created (just in case it was related to my error, but I don't believe it was as I didn't get an error callback, it just didn't do it, so it was a code error in my estimation). The result of removing the first group I had added was that all of the remaining groups "lost" their parent ("GroupCompanyId"), and ended up in the same level as Company Group. Now, if I try and add groups as children of Company Group in the UI, the new group immediately becomes "lost" as the others, as it seems Company Group can't hold children any more.

Geotab support is working with me to fix the problem, but suggested I come here with my code to learn what may have caused it.

 

Below is my .js add code, to the best of my memory before the modifications. Again, this code functioned, and it wasn't until the following day I tried to remove a group and had this issue.

 

Notes: groupSet.groupPrts is an array of the groups. An example of an array object would be: {name: "All Vehicles", id: ""}

The vairable wtr is used to prevent another function that I'll add later from running until this one is complete.

***As I'm pasting and reviewing this here I'm noticing a clear issue in the entity name. groupSet[cycle].name does not exist, this should be groupSet.groupPrts[cycle].name. Is this perhaps what caused the catastrophic issues with this database? It wasn't like this in the first (successful) addition, as the call was originally split out into its own function and I passed the name in the call. The below issue would be how I ran it today before the database broke.

 

parentAdd: function(){
var wtr = false;
for (var cycle = 0; cycle < groupSet.groupPrts; cycle ++) {
api.call("Add", {
typeName: "Group",
entity: {
name: groupSet[cycle].name,
color: {
"r": 255,
"g": 69,
"b": 0,
"a": 0
},
comments: "",
parent: {
"id": "GroupCompanyId"
},

}
}, function(result) {
console.log(result);
wtr = true;
}, function(e) {
console.log(e);
})
}
}

Jdgridline

Please sign in to leave a comment.

2 comments

0
Avatar

Hello,

We have observed this issue take place when adding/removing/setting groups occur in multiple threads. I would advise against doing any group modifications in parallel. Parallel modifications will be possible in our 1804 release.

Wajih Qazi 0 votes
0
Avatar

Just want to elaborate on what Wajih mentioned. api.call is asynchronous, this means the loop is continuing before the result has been returned and you are effectively making multiple requests in parallel to the server. There are multiple ways to deal with the async nature of Javascript. Callbacks (as illustrated in the api.Call success method, the callback is called once the https request has completed successfully), promises (ex do_a().then(do_this_when_a_completes)) or async/await. There are many resources on the web which can help with this topic.

Another solution could be to use a multicall. You could package all the individual requests up into a single request which will be executed synchronously on the server. There's a good example of this in the zone import example here.

Steve Hansen 0 votes