|
| 1 | +api.controller=function($scope, $http, spUtil, $rootScope, $timeout, spModal) { |
| 2 | + /* widget controller */ |
| 3 | + var c = this; |
| 4 | + |
| 5 | + // Initialize scope variables |
| 6 | + $scope.tablename = { |
| 7 | + displayValue: '', |
| 8 | + value: '', |
| 9 | + name: 'tablename' |
| 10 | + }; |
| 11 | + $scope.record = { |
| 12 | + displayValue: '', |
| 13 | + value: '', |
| 14 | + name: 'record' |
| 15 | + }; |
| 16 | + $scope.selectedTable = ''; |
| 17 | + $scope.TableSysId = ''; |
| 18 | + |
| 19 | + // Handle field changes (table/record) |
| 20 | + $scope.$on("field.change", function(evt, parms) { |
| 21 | + if (parms.field.name === 'tablename') { |
| 22 | + // Get sys_id of selected table → fetch actual table name & label |
| 23 | + var sysId = parms.newValue; |
| 24 | + var url = '/api/now/table/sys_db_object/' + sysId + '?sysparm_fields=name,label'; |
| 25 | + $http.get(url).then(function(res) { |
| 26 | + if (res.data.result) { |
| 27 | + $scope.selectedTable = res.data.result.name; |
| 28 | + $scope.selectedTableLabel = res.data.result.label; |
| 29 | + c.getDisplayField($scope.selectedTable, sysId); // fetch display field |
| 30 | + } |
| 31 | + }); |
| 32 | + } else if (parms.field.name === 'record') { |
| 33 | + // Save selected record sys_id |
| 34 | + $scope.TableSysId = parms.newValue; |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + // Get display field for a table (recursive if needed) |
| 39 | + c.getDisplayField = function(tableName, tablesysId) { |
| 40 | + var url = '/api/now/table/sys_dictionary' + |
| 41 | + '?sysparm_query=name=' + tableName + '^display=true' + |
| 42 | + '&sysparm_fields=element' + |
| 43 | + '&sysparm_limit=1'; |
| 44 | + |
| 45 | + $http.get(url).then(function(response) { |
| 46 | + if (response.data.result && response.data.result.length > 0) { |
| 47 | + // Found display field |
| 48 | + $scope.recorddisplayValue = response.data.result[0].element; |
| 49 | + } else { |
| 50 | + // Check parent table |
| 51 | + var parentsysIdUrl = '/api/now/table/sys_db_object/' + tablesysId + '?sysparm_fields=super_class'; |
| 52 | + $http.get(parentsysIdUrl).then(function(parentRes) { |
| 53 | + var parentTable = parentRes.data.result.super_class.value; |
| 54 | + |
| 55 | + if (!parentTable) { |
| 56 | + // No parent - fallback checks |
| 57 | + var nameCheckUrl = '/api/now/table/sys_dictionary' + |
| 58 | + '?sysparm_query=name=' + tableName + '^element=name' + |
| 59 | + '&sysparm_fields=element&sysparm_limit=1'; |
| 60 | + |
| 61 | + $http.get(nameCheckUrl).then(function(nameRes) { |
| 62 | + if (nameRes.status == 200) { |
| 63 | + $scope.recorddisplayValue = 'name'; |
| 64 | + } else { |
| 65 | + var numberCheckUrl = '/api/now/table/sys_dictionary' + |
| 66 | + '?sysparm_query=name=' + tableName + '^element=number' + |
| 67 | + '&sysparm_fields=element&sysparm_limit=1'; |
| 68 | + |
| 69 | + $http.get(numberCheckUrl).then(function(numberRes) { |
| 70 | + if (numberRes.status == 200) { |
| 71 | + $scope.recorddisplayValue = 'number'; |
| 72 | + } else { |
| 73 | + $scope.recorddisplayValue = 'sys_id'; // Final fallback |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + } else { |
| 80 | + // Parent exists - recurse |
| 81 | + var parentNameUrl = '/api/now/table/sys_db_object/' + parentTable + '?sysparm_fields=name'; |
| 82 | + $http.get(parentNameUrl).then(function(parentResname) { |
| 83 | + var parentTableName = parentResname.data.result.name; |
| 84 | + c.getDisplayField(parentTableName, parentTable); // recursive lookup |
| 85 | + }); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + }, function(error) { |
| 90 | + spModal.alert("Error fetching display field: " + error); |
| 91 | + }); |
| 92 | + }; |
| 93 | + |
| 94 | +}; |
0 commit comments