|
| 1 | +var UpdateSetUtilCustom = Class.create(); |
| 2 | +UpdateSetUtilCustom.prototype = { |
| 3 | + initialize: function() {}, |
| 4 | + |
| 5 | + fixScopeBatch: function(grParent) { |
| 6 | + var grUpdate = new GlideRecord('sys_update_xml'); |
| 7 | + grUpdate.addEncodedQuery('update_set.parent.parent=' + grParent.getValue('sys_id') + '^ORupdate_set.parent=' + grParent.getValue('sys_id') + '^ORupdate_set=' + grParent.getValue('sys_id') + '^ORupdate_set.base_update_set=' + grParent.getValue('sys_id')); |
| 8 | + grUpdate.query(); |
| 9 | + |
| 10 | + var count = 0; |
| 11 | + var newUpdateSets = {}; |
| 12 | + |
| 13 | + while (grUpdate.next()) { |
| 14 | + if (!grUpdate.getValue('application')) { // No app, should be in a global update set |
| 15 | + if (grUpdate.update_set.application != 'global' && grUpdate.update_set.application.scope != 'global') { |
| 16 | + count++; |
| 17 | + |
| 18 | + if (!newUpdateSets['global']) { |
| 19 | + newUpdateSets['global'] = { |
| 20 | + 'name': 'Global', |
| 21 | + 'updates': [] |
| 22 | + }; |
| 23 | + } |
| 24 | + newUpdateSets['global']['updates'].push(grUpdate.getValue('sys_id')); |
| 25 | + } |
| 26 | + } |
| 27 | + // We don't have the same scope for each update. |
| 28 | + else if (grUpdate.application != grUpdate.update_set.application) { |
| 29 | + count++; |
| 30 | + |
| 31 | + if (!newUpdateSets[grUpdate.getValue('application')]) { |
| 32 | + newUpdateSets[grUpdate.getValue('application')] = { |
| 33 | + 'name': grUpdate.getDisplayValue('application'), |
| 34 | + 'updates': [] |
| 35 | + }; |
| 36 | + } |
| 37 | + newUpdateSets[grUpdate.getValue('application')]['updates'].push(grUpdate.getValue('sys_id')); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + var parentName = grParent.getValue('name'); |
| 42 | + var keys = Object.keys(newUpdateSets); |
| 43 | + for (i in keys) { |
| 44 | + var updates = newUpdateSets[keys[i]]['updates']; |
| 45 | + // Create new update set in the correct scope. |
| 46 | + var grNewSet = GlideRecord('sys_update_set'); |
| 47 | + grNewSet.initialize(); |
| 48 | + grNewSet.setValue('application', keys[i]); |
| 49 | + grNewSet.setValue('name', parentName + ' - ' + newUpdateSets[keys[i]]['name']); |
| 50 | + grNewSet.setValue('parent', grParent.getValue('sys_id')); |
| 51 | + var newSetSysId = grNewSet.insert(); |
| 52 | + |
| 53 | + for (ii in updates) { |
| 54 | + // Get each update and set the new update set. |
| 55 | + var updateSysId = updates[ii]; |
| 56 | + var grUpdate = new GlideRecord('sys_update_xml'); |
| 57 | + if (grUpdate.get(updateSysId)) { |
| 58 | + grUpdate.setValue('update_set', newSetSysId); |
| 59 | + grUpdate.update(); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + if (count > 0) { |
| 64 | + return count + ' updates were in the wrong scope. ' + keys.length + ' new update sets were created and associated with this parent update set.'; |
| 65 | + } |
| 66 | + return 'No update scope issues were found.'; |
| 67 | + }, |
| 68 | + |
| 69 | + checkForScopeConflict: function(grParent) { |
| 70 | + var application = grParent.getValue('application'); |
| 71 | + var query = 'update_set=' + grParent.getValue('sys_id') + '^application!=' + application + '^ORapplication=NULL'; |
| 72 | + if (grParent.application == 'global' || grParent.application.scope == 'global') { |
| 73 | + query = 'update_set=' + grParent.getValue('sys_id') + '^application!=' + application + '^applicationISNOTEMPTY^application.scope!=global'; |
| 74 | + } |
| 75 | + |
| 76 | + var grUpdate = new GlideRecord('sys_update_xml'); |
| 77 | + grUpdate.addEncodedQuery(query); |
| 78 | + grUpdate.setLimit(1); |
| 79 | + grUpdate.query(); |
| 80 | + |
| 81 | + if (grUpdate.hasNext()) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + return false; |
| 85 | + }, |
| 86 | + |
| 87 | + type: 'UpdateSetUtilCustom' |
| 88 | +}; |
0 commit comments