How can I create a task for selected rows from a custom table?

Kavita Deepak
Tera Contributor

I am working on a project where I have created a custom table in ServiceNow. The table has about 8 columns. I have also created a UI Action (button) under the list called "Submit Changes". The idea is, when a user who has access to this table selects certain rows of the table, and clicks on Submit Changes, a task will be created for our applications support team with the data from the table. Lets say 4 rows were selected and submitted, the task will need to show the 4 rows in the task. 

 

In my UI Action just as a test to see if it will create a task for each row, I added the following code but this doesnt create a task at all

 

var gr = new GlideRecord('u_custom_tablename');
 var selectedRows = gr.getMultipleSelection();
   
    if (selectedRows.length > 0) {
        for (var i = 0; i < selectedRows.length; i++) {
            var row = selectedRows[i];
            var newTask = new GlideRecord('task');
           
            // Populate task fields with data from selected row
            newTask.setValue('short_description', 'Test'); 
              
            // Create the task record
            newTask.insert();
        }
       
        // Optional: Refresh the list view
        g_list.refresh();
       
        // Optional: Display a success message
        alert("Changes submitted successfully!");
    } else {
        alert("Please select rows from the table to generate tasks.");
    }
 
Can someone point me to the right direction on how to achieve my ask?
2 REPLIES 2

anvesh_v
Giga Guru

 Could you please elaborate . The idea is, when a user who has access to this table selects certain rows of the table  

If you using list layout  and selecting the rows you need to use the client script in UI action 

var checkedList = g_list.getChecked();

Use the script include to pass the sys id and create the task accordingly 

 

 




Lets say the table has 5 rows of data. If I select rows 1 and 2, then one task will need to be generated with a description "Task to XX" and in the task, I would like to display rows 1 and 2.