How to create a UI Action that populates a List Collector without saving the record?

NowNinja727
Giga Contributor
Hi everyone,

I’m trying to create a UI Action on a form that will automatically populate certain values inside a List Collector field when the button is clicked.
 
The part I’m struggling with is this:
I want the List Collector to be updated on the form only, but I do not want the record to save when the UI Action is clicked.
 
I’ve tried different combinations of client/server UI Actions, return false, and avoiding current.update(), but either:
  • the list collector updates only in the database (requires a save), or
  • the form reloads and loses the populated values, or
  • nothing happens at all.
Has anyone successfully built a UI Action that:
  1. Populates a List Collector field client‑side,
  2. Leaves changes visible on the form,
  3. But does not save or submit the form automatically?
Any guidance or examples would really help. Thanks!
4 REPLIES 4

Ankur Bawiskar
Tera Patron

@NowNinja727 
why this requirement? you want just value to change but not to be saved?

You can use g_form.setValue() in client side UI action and see if that works

If your UI action is server side then after setting the value using current object, unless the values are saved to database it won't reflect

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Hi @Ankur Bawiskar , thanks for replying.
 
The intention behind this UI action is just to help end users. We have a list collector field for divisions, and there are about 14 “standard” divisions that are used in most cases. Instead of users manually searching and adding these one by one in the list collector, I want them to be able to click a button and have those 14 divisions populated for them.
 
The important part is that this should only update the form, not automatically save the record. The user should still decide whether to Save/Update after reviewing the values.
 
Based on your suggestion, I tried a client-side UI action with client = true and an onClick(g_form) function that:
  • reads the current values from the list collector with g_form.getValue('<list_collector_name>')
  • merges in the 14 division sys_ids
  • calls g_form.setValue('<list_collector_name>', finalValue)
  • returns false to prevent the form submit
I tested this both on a new record and on an existing record, but nothing changes in the list collector when I click the button (no error, no values added).
 
Separately, I have a server-side UI action script that uses current.<field> and current.update(). That one works fine on existing records – it adds all 14 divisions and saves the record – but that’s exactly what I’m trying to avoid, because we don’t want an automatic save.
 
Do you have any other alternative suggestion which can work for me? I'm open for suggestions

Thanks again for your help.

Hi @NowNinja727 ,

 

Make sure you are providing comma-separated list of sys_ids in finalValue in string format

 

function AddToWatchlist(){
    g_form.setValue("watch_list", "6816f79cc0a8016401c5a33be04be441,432c7325931721100143ad44246a4db5,62826bf03710200044e0bfc8bcbe5df1");
}

 

Thanks

Anand

Medi C
Giga Sage

Hi @NowNinja727,

Please ensure the following:

  • Your script include is "Client Callable"
  • The function on your script include should query and return a string of sys ids (comma separated) (No update required)
    Sample Script:
        getRecords: function() {
            // Retrieve the parameter
            var parm = this.getParameter('sysparm_parametername');
    
            // If no parameter is provided, return an empty string
            if (!parm) {
                return "";
            }
            
            var results = [];
    
            // Query the table
            var gr = new GlideRecord("YOUR_TABLE");
    
            // Retrieve the records
            gr.addQuery("COLUMN=" + parm);
            gr.query();
            while(gr.next()){
                results.push(gr.getValue("sys_id"))
            }
            
            return results.join()
        },​
  • Your UI Action has "Client" checked and the OnClick field has the name of the function to be called.
    Example: OnClick()
    Here is a sample function:
    function onClick(){
        var param = g_form.getValue("FIELD");
    	if(!param){
    		return;
    	}
    
        var ga = new GlideAjax('YOUR_SCRIPT_INCLUDE');
        ga.addParam('sysparm_name', 'getRecords');
        ga.addParam('sysparm_parametername', param);
        ga.getXML(updateLC);
    }
    
    function updateLC(){
        var answer = response.responseXML.documentElement.getAttribute("answer");
    	if(answer){
    		g_form.setValue(answer);
    	}
    }​

If this does not help, please share more details / some screenshots to check further.


Thanks & Best regards,
Medi