Action returning array but need to format better

Community Alums
Not applicable

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:

  1. Create a new action in global scope
  2.  set the Inputs as follows
    Inputs.png
  3. Add a Script Step and setup as follows
    ScriptStepVar.png
  4. 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 
    ScriptStepOutput.png
  • Outputs
    Action Outputs.png
    Exit Edit Mode then Give it the Tasklist
    Output mapped.png
3 REPLIES 3

Amit Verma
Kilo Patron
Kilo Patron

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 -

 

AmitVerma_0-1731984245512.png

 

Output -

AmitVerma_1-1731984268756.png

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Nick Parsons
Mega Sage

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.

 

Ranjane_Omkar
Kilo Sage

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.