Action returning array but need to format better
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2024 12:06 PM
I have an action that returns the list of records that a user is assigned to and are active=true. What I'm trying to figure out is how to return the list of items without the [ ] around the object.
Currently this is an example of what I'm getting:
["INC000000", "TODO000123", "PRJTASK00013"]
Desired result would be and ordered list of items without the "," and [ ]
INC000000
PRJTASK00013
TODO000123
How can I accomplish this. I'm using the Action to produce and email to the manager that will show a user that left the company which tasks need to be reassigned. I have that part I just need to know how to get the Desired Result
What I'm doing and Recreation Steps
I have a script step in an action that is querying through the task table with the constraints of active is true and assigned to is sys_id of the user.
the action output is String which is returning what appears to be an array. Here is the output I'm getting.
Am I doing something wrong in my Action I wonder?
Steps to reproduce the Action:
- Create a new action in global scope
- set the Inputs as follows
- Add a Script Step and setup as follows
- Paste this in the Script
(function execute(inputs, outputs) {
var username = inputs.username;
var taskNumbers = [];
var gr = new GlideRecord('task'); // Create a new GlideRecord object for the task table
gr.addQuery('assigned_to', username); // Filter by user ID
gr.addQuery('active', true); // Filter for active tasks
gr.query(); // Execute the query
while (gr.next()) {
taskNumbers.push(gr.getValue('number')); // Collect the task numbers
}
//output
outputs.tasklist = taskNumbers;
})(inputs, outputs);
- Outputs for Script step
- Outputs
Exit Edit Mode then Give it the Tasklist
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2024 06:44 PM
Hi @Community Alums
Any particular reason for creating a custom action for this requirement ? You could have made use of Lookup Records action. Refer below screenshots -
Output -
Thanks and Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2024 07:44 PM - edited 11-18-2024 07:46 PM
As Amit Verma suggests, you should look into using regular flow actions if you can. To answer your question though, you should create a string in your code with the format you desire, and then set that as your output. An array doesn't contain any of the formatting information, so instead you should form a string. One way to do that with your existing code is to use the array method .join() to create a string where all your array elements (task numbers) are joined by a new line character:
outputs.tasklist = taskNumbers.join("\n");
to have your numbers appear in the order you want, use `gr.orderBy("number");` before you execute the query.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2024 11:54 PM
Hello @Community Alums ,
Instead of using array try using string & join using "\n".
var username = inputs.username;
var taskNumbers = "";
var gr = new GlideRecord('task'); // Create a new GlideRecord object for the task table
gr.addQuery('assigned_to', username); // Filter by user ID
gr.addQuery('active', true); // Filter for active tasks
gr.query(); // Execute the query
while (gr.next()) {
taskNumbers = taskNumbers + gr.getValue('number') + "\n"; // Collect the task numbers
}
//output
outputs.tasklist = taskNumbers;
Regards,
----
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.