Create catalog tasks based on the attachment

Merza Lyn
Mega Guru

We have catalog item Employee information Change Request.

And on the request, there is a attachment for the Employee information that needs to be update.

Currently, our catalog item only creates one catalog task for AD user data change.

How can we add more catalog tasks based on the number of users in the attachment?

Example there 22 users in the attachment, the catalog task for AD user data change should also 22.

 

6 REPLIES 6

SasiChanthati
Giga Guru

Based on what you provided i am understanding that, you're looking to generate multiple catalog tasks dynamically based on the number of users listed in an attachment to a catalog item. Since you're using a catalog item (`Employee Information Change Request`), and the attachment contains details for each user that needs updates, here's a structured way to approach it.

Goal Recap:

- Catalog item receives an attachment (likely a CSV or Excel file) listing users.
- Based on the number of rows (users) in the attachment, create one catalog task per user under the request item (RITM).
 Each task is for `AD user data change`.

Solution Outline:

1. Use a Catalog Item Flow or Script Include to Trigger After Submission
2. Parse the Attachment (CSV/Excel)
3. Loop Through the Parsed Data
4. Create a Catalog Task for Each User

Step-by-Step Setup

Step 1: Add an Attachment Field to the Catalog Item

You probably already have this, but make sure your catalog item allows attachments:
- Go to your catalog item.
- Ensure the "Attachments" tab is enabled (`Add Attachments` = true).

Step 2: Create a Flow (or Business Rule/Script Include) to Run After RITM Submission

You can do this using Flow Designer or by using Script Include triggered by a Scripted Flow or Business Rule. Below is the script approach.

Step 3: Script to Parse CSV and Create Tasks

Here’s a sample script to put in a Script Include or Business Rule (after insert on sc_req_item):

(function executeRule(current, gsn, gs) {
// Get the attachments on the RITM
var grAttachment = new GlideRecord('sys_attachment');
grAttachment.addQuery('table_name', 'sc_req_item');
grAttachment.addQuery('table_sys_id', current.sys_id);
grAttachment.query();

while (grAttachment.next()) {
if (grAttachment.file_name.endsWith('.csv')) {
var attachment = new GlideSysAttachment();
var csvData = attachment.getContentStream(grAttachment);
var csvParser = new GlideCSVParser();
csvParser.parse(csvData);

while (csvParser.next()) {
var userName = csvParser.getValue('username'); // adjust to your CSV column
var fullName = csvParser.getValue('full_name'); // optional

// Create a catalog task
var task = new GlideRecord('sc_task');
task.initialize();
task.request_item = current.sys_id;
task.short_description = 'AD User Data Change for: ' + userName;
task.description = 'Process AD change for user: ' + fullName + ' (' + userName + ')';
task.assignment_group = '<your_assignment_group_sys_id>'; // Optional
task.insert();
}
}
}

})(current, gsn, gs);

Important Notes:

Replace `username`, `full_name` in the script with actual column headers from your CSV.
- Set `assignment_group` in the task as needed.
- If the file is Excel (.xlsx), you'll need to use the Import Set API or Data Streamer with additional logic.

Testing Tip:

Test your logic with a smaller CSV (2–3 rows) to ensure:
- The attachment is being read
- Tasks are being created under the right RITM

Ankur Bawiskar
Tera Patron
Tera Patron

@Merza Lyn 

so based on rows in the attached excel you want those many catalog tasks?

If yes then in your flow you will have to create flow variable of type Integer, use GlideExcelParser and determine the number of rows

Then use For Each based on that count and create tasks

check this link

Increment integer flow variable in each loop until variable reach 5 not working with below approach 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Merza Lyn 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Merza Lyn 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader