Skip to content

Commit 48e315a

Browse files
authored
Dynamic catalog task creation (#2173)
* Create dynamic_catalog_task.js * Create README.md * Update README.md
1 parent a1f7d83 commit 48e315a

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
**Dynamic Catalog Task Generator**
2+
3+
This Script Include provides a flexible, maintainable way to create one or more Service Catalog Tasks (sc_task) on a Request Item (sc_req_item). Instead of relying on complex, branching logic within a single Workflow or Flow, this script determines which tasks to create based on the value selected by the user in a single variable on the catalog form.
4+
5+
6+
**Centralizes Task Logic**: Keeps all task definitions (short descriptions, assignment groups, order) in one easy-to-read script.
7+
8+
**Improves Maintainability**: You only update this single script when task requirements change, not a sprawling visual flow.
9+
10+
**Increases Flow Reusability**: The core Flow/Workflow remains simple, focused only on calling this generator.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
var CatalogTaskGenerator = Class.create();
2+
CatalogTaskGenerator.prototype = {
3+
initialize: function() {
4+
this.taskTable = 'sc_task';
5+
},
6+
generateTasks: function(ritmSysId, variableName) {
7+
var ritmGR = new GlideRecord('sc_req_item');
8+
if (!ritmGR.get(ritmSysId)) {
9+
gs.error('RITM not found for sys_id: ' + ritmSysId);
10+
return;
11+
}
12+
var taskType = ritmGR.variables[variableName].toString();
13+
switch (taskType) {
14+
case 'Laptop':
15+
this._createTask(ritmGR, 'Assign Laptop Asset', 'Hardware Fulfillment', 10);
16+
this._createTask(ritmGR, 'Install Core Software', 'Software Team', 20);
17+
break;
18+
case 'Desktop':
19+
this._createTask(ritmGR, 'Deploy Desktop Image', 'Infrastructure Team', 10);
20+
this._createTask(ritmGR, 'Setup Docking Station', 'Hardware Fulfillment', 20);
21+
break;
22+
case 'Mobile Phone':
23+
this._createTask(ritmGR, 'Order SIM Card', 'Telecom Team', 10);
24+
this._createTask(ritmGR, 'Configure MDM Profile', 'Mobile Support', 20);
25+
break;
26+
// add Cases as Catalog Tasks Required....
27+
default:
28+
gs.info('CatalogTaskGenerator: No specific tasks defined for ' + variableName + ' value: ' + taskType);
29+
break;
30+
}
31+
},
32+
33+
_createTask: function(ritmGR, shortDesc, assignmentGroupName, order) {
34+
var taskGR = new GlideRecord(this.taskTable);
35+
taskGR.initialize();
36+
taskGR.request_item = ritmGR.sys_id;
37+
taskGR.request = ritmGR.request;
38+
taskGR.short_description = shortDesc;
39+
taskGR.order = order;
40+
var groupGR = new GlideRecord('sys_user_group');
41+
if (groupGR.get('name', assignmentGroupName)) {
42+
taskGR.assignment_group = groupGR.sys_id;
43+
} else {
44+
gs.warn('CatalogTaskGenerator: Assignment Group not found: ' + assignmentGroupName);
45+
}
46+
47+
taskGR.insert();
48+
},
49+
50+
type: 'CatalogTaskGenerator'
51+
};

0 commit comments

Comments
 (0)