LIst Action Client Script - g_list

maciep
Tera Expert

Question

I created a Declarative Action for Lists implemented as a Client Script. Inside the script, I'm running into some issue with g_list. I think it's supported there, but maybe I'm doing something wrong.

 

If I try to access g_list.getQuery(), it will throw an error if there are no conditions set on the list. It doesn't return null/undefined /etc...it throws an error - "TypeError: Cannot read properties of null (reading 'split')" . Is that expected behavior?

 

If I try to access g_list.getView(), it works until a condition is supplied. So if the list is unfiltered, it will return "sow" for example. But if a fitler is applied to the list, it returns undefined. Is the list view concept different in a workspace than in classic? 

The g_list.isUserList() doesn't seem to ever be true;

I've also had trouble getting my user id from g_user.userID. It just returns null. 

 

All of that is happening in my PDI. I haven't tried in our dev environment yet as I'm still trying to work it all out. So maybe it's related to my PDI somehow?


I can supply the code I'm using, but I'm just trying to understand if g_list and g_user are supposed to work in the next experience or are they being deprecated there?

 

Context

I built the regular UI action already. It all works. But I know users will want this in their various workspaces too.

 

The UI action is to export all records from a list. The default Export limits the number of records that can be exported. We don't want to increase that limit across the board. We want to give certain users on certain tables the ability to download all rows as a csv. 

 

The UI action uses a client-callable script include that takes the query from the list and fields from the view (personalized or not) to get the records, and saves them to the attachment table. Once complete, the UI Action hits the attachment url to download it.

 

I'm trying to replicate that in a workspace. I knew it wouldn't be as easy here but hopefully won't need to get much more convoluted.

1 ACCEPTED SOLUTION

maciep
Tera Expert

It turns out the g_list does work, so that's good. The condition is an empty string when there is none. The userID is being returned as expected as well.

 

The list/view look don't really work...at least not like in the classic list. But I'm guessing that's more because lists are just different in workspaces? The main need for that info is to get the columns/fields that should be exported.

 

So instead, I'm getting the url with top.location.href and parsing the list-id out of it. And that is the sys_id of the record in sys_ux_list. Those records contain the associated columns.

 

And if it's a personallized list, there will be a record in the sys_user_preference table with the name="

workspace.list.columnOrder.<list-id>" and the user=<user-id>. 
 
With those changes, I'm able to get the actions to work in both workspaces and classic view.

View solution in original post

6 REPLIES 6

Vishal Jaswal
Tera Sage

Hello @maciep 

I believe you should update your Declarative Action client script logic to leverage the action payload instead of relying on g_list. and g_user.

// props = properties/context of the list component (like table info)
// event = details about what triggered this action (like selected records)
(function(props, event) {
 
    // This is where I am suggesting to not rely on g_list.getTableName() in Workspace
    var table = event.table || props.table;    // get the table name from the event first else fall back to props

    // selected sys_ids records
    var sysIds = event.sys_id;

     var query = '';

   
  //get the full list query using g_list by wrapping it in try/catch because g_list.getQuery() is already throwingan error
   // so when no filter is applied (instead of returning empty string)
    try {
        query = g_list.getQuery() || '';  
    } catch (e) {
        // If it errors out, then fall back to building a query from selected sys_ids
        //get records where sys_id is IN this comma-separated list
        query = 'sys_idIN' + sysIds;
    }

    var ga = new GlideAjax('YourExportScriptInclude');
    ga.addParam('sysparm_name', 'exportRecords'); //GA Function
    ga.addParam('sysparm_table', table);  // Pass the table name 
    ga.addParam('sysparm_query', query);  // Pass the query to fetch the records in context
   ga.getXMLAnswer(function(answer) {
      
        var result = JSON.parse(answer);   // Parse received JSON string response into an object

        // If created CSV as an attachment, returns its sys_id
        if (result.attachment_sys_id) {
            // Open the attachment URL in a new tab to begin download
            window.open('/sys_attachment.do?sys_id=' + result.attachment_sys_id);
        }
    });

// Execute the function immediately, passing in props and event
})(props, event);


 


Hope that helps!

Thanks Vishal, but I don't think I have access to props/event objects with a Client Script? I'm using onClick() currently (like in a UI action).

 

I'm also not requiring records to be selected. I'm exporting all records currently in the list, filtered or not. That's the goal at least.

 

And I know I could look into uxf actions...and I'm afraid I might have to (they couldn't have made all of this more convoluted if they tried). I know there are "definitive guides" and lots of videos, it's still a mess of tables and mappings and do I have to go into UI Builder? It's all quite annoying.

 

But for now, I'm really just trying to understand if g_list is supposed to work on not here in a DA Client. All of the docs make it sound like it should, but we all know how terrible the docs are too.

maciep
Tera Expert

Ok, this is not happening in our dev environment. So maybe this is just an issue with my PDI. They're both on Zurich, but guessing my PDI was zbooted to Zurich, but we upgraded to it. Maybe different patch levels too.

Hello @maciep 

You can try to release PDI instance and then request for new PDI and try again.


Hope that helps!