Skip to Content

Service Catalog Auto-Create Fulfillment Tasks

Automatically create fulfillment tasks for specific catalog items when requests are submitted, enabling multi-step approval and delivery workflows.

Table: sc_req_item When: after
#service-catalog #fulfillment #automation #tasks #workflow #after #insert #sc_req_item #dependencies

Script Code

JavaScript
1(function executeRule(current, previous /*null when async*/) {
2
3  // Configuration: Define catalog items that require fulfillment tasks
4  var catalogTaskConfig = {
5    // Catalog item sys_id or name: array of task configurations
6    'laptop_request': [
7      {
8        short_description: 'Hardware Procurement - Order Laptop',
9        assignment_group: 'Hardware Procurement',
10        order: 1,
11        estimated_hours: 24,
12        description: 'Order laptop hardware based on user specifications'
13      },
14      {
15        short_description: 'IT Setup - Configure Laptop',
16        assignment_group: 'Desktop Support',
17        order: 2,
18        estimated_hours: 4,
19        description: 'Install OS, applications, and configure per company standards'
20      },
21      {
22        short_description: 'Security Setup - Install Security Software',
23        assignment_group: 'IT Security',
24        order: 3,
25        estimated_hours: 2,
26        description: 'Install antivirus, encryption, and security policies'
27      },
28      {
29        short_description: 'Delivery - Ship to User',
30        assignment_group: 'Facilities',
31        order: 4,
32        estimated_hours: 8,
33        description: 'Package and ship laptop to user location'
34      }
35    ],
36    'new_user_onboarding': [
37      {
38        short_description: 'HR - Setup Employee Record',
39        assignment_group: 'Human Resources',
40        order: 1,
41        estimated_hours: 2,
42        description: 'Create employee record and setup benefits'
43      },
44      {
45        short_description: 'IT - Create User Accounts',
46        assignment_group: 'Identity Management',
47        order: 2,
48        estimated_hours: 1,
49        description: 'Create AD account, email, and assign groups'
50      },
51      {
52        short_description: 'IT - Assign Equipment',
53        assignment_group: 'Desktop Support',
54        order: 3,
55        estimated_hours: 4,
56        description: 'Prepare and assign laptop, phone, and accessories'
57      },
58      {
59        short_description: 'Security - Setup Access Cards',
60        assignment_group: 'Physical Security',
61        order: 4,
62        estimated_hours: 1,
63        description: 'Create badge and setup building access'
64      }
65    ],
66    'application_access_request': [
67      {
68        short_description: 'Security Review - Access Justification',
69        assignment_group: 'IT Security',
70        order: 1,
71        estimated_hours: 2,
72        description: 'Review access request and business justification'
73      },
74      {
75        short_description: 'Manager Approval - Business Need',
76        assignment_group: 'Management',
77        order: 2,
78        estimated_hours: 24,
79        description: 'Manager approval for application access'
80      },
81      {
82        short_description: 'IT - Grant Access',
83        assignment_group: 'Identity Management',
84        order: 3,
85        estimated_hours: 1,
86        description: 'Create account and assign appropriate permissions'
87      }
88    ]
89  };
90
91  try {
92    // Get the catalog item
93    var catalogItem = current.cat_item.getRefRecord();
94    
95    if (!catalogItem) {
96      gs.warn('Service Catalog BR: No catalog item found for request item ' + current.number);
97      return;
98    }
99
100    var catalogItemName = catalogItem.name.toString();
101    var catalogItemSysId = catalogItem.sys_id.toString();
102    
103    // Check if this catalog item has configured tasks
104    var taskConfigs = catalogTaskConfig[catalogItemName] || catalogTaskConfig[catalogItemSysId];
105    
106    if (!taskConfigs) {
107      gs.info('Service Catalog BR: No task configuration found for catalog item: ' + catalogItemName);
108      return;
109    }
110
111    // Get variables from the request for task customization
112    var variables = {};
113    var grVariable = new GlideRecord('sc_item_option_mtom');
114    grVariable.addQuery('request_item', current.sys_id);
115    grVariable.query();
116    
117    while (grVariable.next()) {
118      var varName = grVariable.sc_item_option.item_option_new.name.toString();
119      var varValue = grVariable.sc_item_option.value.toString();
120      variables[varName] = varValue;
121    }
122
123    // Create fulfillment tasks
124    var tasksCreated = 0;
125    var previousTaskId = null;
126    
127    taskConfigs.forEach(function(taskConfig) {
128      var grTask = new GlideRecord('sc_task');
129      grTask.initialize();
130      
131      // Link to request item
132      grTask.request_item = current.sys_id;
133      grTask.parent = current.sys_id;
134      
135      // Basic task information
136      grTask.short_description = taskConfig.short_description;
137      grTask.description = taskConfig.description || '';
138      grTask.order = taskConfig.order || 1;
139      
140      // Assignment
141      if (taskConfig.assignment_group) {
142        var grGroup = new GlideRecord('sys_user_group');
143        if (grGroup.get('name', taskConfig.assignment_group)) {
144          grTask.assignment_group = grGroup.sys_id;
145        } else {
146          gs.warn('Service Catalog BR: Assignment group not found: ' + taskConfig.assignment_group);
147        }
148      }
149      
150      // Copy relevant fields from request item
151      grTask.requested_for = current.requested_for;
152      grTask.opened_by = current.opened_by;
153      grTask.priority = current.priority;
154      grTask.urgency = current.urgency;
155      grTask.impact = current.impact;
156      
157      // State and timing
158      grTask.state = '1';  // Open
159      grTask.active = true;
160      
161      // Calculate due date based on estimated hours
162      if (taskConfig.estimated_hours) {
163        var dueDate = new GlideDateTime();
164        dueDate.addSeconds(taskConfig.estimated_hours * 3600);
165        grTask.due_date = dueDate;
166        grTask.work_notes = 'Estimated effort: ' + taskConfig.estimated_hours + ' hours';
167      }
168      
169      // Set dependency on previous task for sequential execution
170      if (taskConfig.order > 1 && previousTaskId) {
171        grTask.depends_on = previousTaskId;
172        grTask.state = '-5';  // Pending (waiting for dependency)
173      }
174      
175      // Customize task description with variable values
176      var customDescription = grTask.description;
177      
178      // Replace placeholders with actual variable values
179      // Example: Replace {{laptop_type}} with actual value
180      for (var varName in variables) {
181        var placeholder = '{{' + varName + '}}';
182        customDescription = customDescription.replace(new RegExp(placeholder, 'g'), variables[varName]);
183      }
184      
185      grTask.description = customDescription;
186      
187      // Insert the task
188      var taskId = grTask.insert();
189      
190      if (taskId) {
191        tasksCreated++;
192        previousTaskId = taskId;
193        
194        gs.info('Service Catalog BR: Created task ' + grTask.number + 
195               ' for request item ' + current.number);
196        
197        // Send notification to assignment group
198        if (taskConfig.send_notification !== false) {  // Default to true
199          gs.eventQueue('sc_task.assigned', grTask, grTask.assignment_group, taskId);
200        }
201      }
202    });
203    
204    if (tasksCreated > 0) {
205      // Update request item with task count
206      current.work_notes = 'Auto-created ' + tasksCreated + ' fulfillment tasks for ' + catalogItemName;
207      
208      // Optional: Set request item state to work in progress
209      if (current.state.toString() === '1') {  // If still Open
210        current.state = '2';  // Work in Progress
211      }
212      
213      current.update();
214      
215      gs.info('Service Catalog BR: Successfully created ' + tasksCreated + 
216             ' fulfillment tasks for request item ' + current.number);
217      
218      // Optional: Send summary notification to requester
219      gs.eventQueue('sc_request.fulfillment_started', current, current.requested_for, current.sys_id);
220    }
221    
222  } catch (e) {
223    gs.error('Service Catalog Auto-Fulfillment BR error for ' + current.number + ': ' + e.message);
224    
225    // Add error note to request item
226    current.work_notes = 'Error creating fulfillment tasks: ' + e.message;
227    current.update();
228  }
229
230})(current, previous);

How to Use

1. Create an after Business Rule on sc_req_item table 2. Check "Insert" checkbox only 3. Configure catalogTaskConfig object with your catalog items and required tasks 4. Create assignment groups referenced in the configuration 5. Customize task fields and dependencies as needed 6. Set up notifications for task assignments (sc_task.assigned event) 7. Test with catalog item requests 8. Monitor task creation and dependencies 9. Adjust estimated hours and assignment groups based on actual fulfillment times 10. Consider creating custom fields for additional task metadata

Explore More Scripts

Browse our complete library of ServiceNow scripts

View All Scripts