|
| 1 | +The GlideSystem User object has a useful function, which is called getMyGroups. This gives back the sys_ids of current user's group. But the functionality behaves differently depending where it is called from. |
| 2 | + |
| 3 | +If the function is called from Global scope a Java object (com.glide.collections.StringList) is returned: |
| 4 | + |
| 5 | +``` Javascript |
| 6 | +var currentUserGroups = gs.getUser().getMyGroups(); |
| 7 | +gs.info(currentUserGroups); |
| 8 | +gs.info('Object type: ' + Object.prototype.toString.call(currentUserGroups)); |
| 9 | +``` |
| 10 | + |
| 11 | +Result: |
| 12 | +``` Text |
| 13 | +*** Script: [723aa84f5ba02200502f6ede91f91aea, cfcbad03d711110050f5edcb9e61038f] |
| 14 | +*** Script: Object type: [object JavaObject] |
| 15 | +``` |
| 16 | +When the function is called from Application scope, the type will be a Javascript Array object: |
| 17 | +``` Text |
| 18 | +x_149822_va_code_p: 723aa84f5ba02200502f6ede91f91aea,cfcbad03d711110050f5edcb9e61038f |
| 19 | +x_149822_va_code_p: Object type: [object Array] |
| 20 | +``` |
| 21 | +The main problem here is that the StringList class behaves differently like a generic JS Array. For example you cant get an element from the collection based on its index (currentUserGroups[0]). |
| 22 | + |
| 23 | +``` Text |
| 24 | +Javascript compiler exception: Java class "com.glide.collections.StringList" has no public instance field or method named "0". (null.null.script; line 8) in: |
| 25 | +var currentUserGroups = gs.getUser().getMyGroups(); |
| 26 | +``` |
| 27 | +The solution below gives a generic way, how this function can be called from both type of Applications: |
| 28 | + |
| 29 | +``` Javascript |
| 30 | +var currentUserGroups = gs.getUser().getMyGroups(); |
| 31 | + |
| 32 | +if (Object.prototype.toString.call(currentUserGroups).match(/^\[object\s(.*)\]$/)[1] == "JavaObject") { |
| 33 | + var arrayUtil = new global.ArrayUtil(); |
| 34 | + currentUserGroups = arrayUtil.convertArray(currentUserGroups); |
| 35 | +} |
| 36 | + |
| 37 | +gs.info(currentUserGroups); |
| 38 | +gs.info('Object type: ' + Object.prototype.toString.call(currentUserGroups)); |
| 39 | +``` |
| 40 | + |
| 41 | +Global: |
| 42 | +``` Text |
| 43 | +*** Script: 723aa84f5ba02200502f6ede91f91aea,cfcbad03d711110050f5edcb9e61038f |
| 44 | +*** Script: Object type: [object Array] |
| 45 | +``` |
| 46 | + |
| 47 | +Scoped app: |
| 48 | +``` Text |
| 49 | +x_149822_va_code_p: 723aa84f5ba02200502f6ede91f91aea,cfcbad03d711110050f5edcb9e61038f |
| 50 | +x_149822_va_code_p: Object type: [object Array] |
| 51 | +``` |
| 52 | +So with this simple solution the collection of groups can be handled as a JS Array in both cases. |
0 commit comments