how to insert in list collector

Akki1
Tera Contributor

Hi,

I have a list collector variable and I want to auto populate based on a script include which returns comma separated  sys_id of the records to be inserted. How can I use it in client script and update the list collector?  

6 REPLIES 6

Kartik Choudha1
Tera Guru

Hi There,

 

You need to do DOM manipulation to get the list collector auto populated.

you can refer below sample code of mine and change it according to your requirement like table name and all. Below code is used to auto populate the list collector based on change of one of the other field, That's why the code is in the OnChange Client Script.

 

To be specific to your question - Refer the Client Script getOpportunity() callback function which is splitting the comma separated sys_ids returned from Script include. Followed by setSlushBucket() function to set the list collector

Let me know in case of any doubt and if it helps mark it to helpful/correct.

 

Script include method - 

getOpportunities: function() {
		var opportunities = [];
		var opportunityGroup = this.getParameter('sysparm_opportunityGroup');
		var gr = new GlideRecord('table_name');
		gr.addEncodedQuery('field_name='+opportunityGroup.toString());
		gr.query();
		while(gr.next()){
			opportunities.push(gr.field_name.toString());
		}
		return opportunities.toString();
	}

Client Script - Focus on glide Ajax call, Enable 'Isolate Script' field of client script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }

    if (newValue == '') {
        removeValues();
    } else {
        removeValues();
        var left, right, l_o;
        //Glide Ajax Call
        var ga = new GlideAjax('Script_include_name');
        ga.addParam('sysparm_name', 'getOpportunities');
        ga.addParam('sysparm_opportunityGroup', g_form.getValue('field_name'));
        ga.getXML(getOpportunity);

        function getOpportunity(response) {
            var data = response.responseXML.documentElement.getAttribute("answer");
            var ids = data.split(',');
            setSlushBucket('u_opportunity', 'sys_idIN' + ids.toString());


            g_form.setDisplay('u_opportunity', false);
            setCollectorFilter();

            function setCollectorFilter() {
                //get the current showing list from 'available' and 'selected' slush bucket      
                left = gel('slush_bucket_name' + '_select_0');
                right = gel('slush_bucket_name' + '_select_1');
                l_o = left.options;
                //condition to check - available slush bucket should have the records according to new filter set.

                if (l_o.length != ids.length) {
                    setTimeout(setCollectorFilter, 100); // setting timeout untill the available slush bucket get refreshed.
                    return;
                } else { // when 'available' slush bucket is refreshed - control will come to else block where we are setting the necessary records in 'selected' slush bucket
                    g_form.setValue('field_name', ids.toString());

                    setSlushBucket('u_opportunity', 'field_name=10f2246fdb11909064cb5eea4b96194b'); //reset the fiter to reset the available slush bucket

                    g_form.setDisplay('u_opportunity', true);
                }

            }


        }
    }


}

function removeValues() {
    g_form.setValue('u_opportunity', '');
}

//Below function will set the filter of the list collectors account to the query passed in the param
function setSlushBucket(collectorName, sqlQuery) {
    window[collectorName + 'g_filter'].reset();
    window[collectorName + 'g_filter'].setQuery(sqlQuery);
    window[collectorName + 'acRequest'](null);

}

 

Regards,

Kartik

@Kartik Choudha1 Thanks but I have some questions

What is u_opportunity Is it list collector variable?

And Why are you doing setDisplay false?

And I already have some filter on list collector applied on variable i see you are removing it and then resetting it I want it to function only if those sys_id records fulfil that filter as well. Or is it not possible?

Hi @Akki1 

 

That's possible. Above code was written as per my requirement you can ignore the unnecessary code as per your requirement.

 

I used to clear the auto populate values in list collector if any new value gets selected on other field that's why removeValues() function is there.

Why are you doing setDisplay false?SetDisplay - True/False is for better user experience. When List collector is auto populating that time the particular list collector gets loaded. So, to hide that from user I have used SetDisplay.

What is u_opportunity Is it list collector variable? - Yes, it's list collector variable

 

You can focus on below code snippet -

 

 

var left, right, l_o;
        var ga = new GlideAjax('apiAjaxCBA');
        ga.addParam('sysparm_name', 'getOpportunities');
        ga.addParam('sysparm_opportunityGroup', g_form.getValue('u_opportunity_group'));
        ga.getXML(getOpportunity);

        function getOpportunity(response) {
            var data = response.responseXML.documentElement.getAttribute("answer");
            var ids = data.split(',');
            setSlushBucket('u_opportunity', 'sys_idIN' + ids.toString());


            
            setCollectorFilter();

            function setCollectorFilter() {
                //get the current showing list from 'available' and 'selected' slush bucket      
                left = gel('u_opportunity' + '_select_0');
                right = gel('u_opportunity' + '_select_1');
                l_o = left.options;
                //condition to check - available slush bucket should have the records according to new filter set.

                if (l_o.length != ids.length) {
                    setTimeout(setCollectorFilter, 100); // setting timeout untill the available slush bucket get refreshed.
                    return;
                } else { // when 'available' slush bucket is refreshed - control will come to else block where we are setting the necessary records in 'selected' slush bucket
                    g_form.setValue('u_opportunity', ids.toString());

                    setSlushBucket('u_opportunity', 'contract_model=10f2246fdb11909064cb5eea4b96194b'); //reset the fiter to reset the available left slush bucket. the left slush bucket will be refreshed to its previous list again

                    g_form.setDisplay('u_opportunity', true);
                }

            }


        }
    }


}

//Below function will set the filter of the list collectors account to the query passed in the param
function setSlushBucket(collectorName, sqlQuery) {
    window[collectorName + 'g_filter'].reset();
    window[collectorName + 'g_filter'].setQuery(sqlQuery);
    window[collectorName + 'acRequest'](null);

}

 

 

Kindly mark the answer helpful or correct if it helps a bit.

Hi @Akki1 

 

Hope answer might help you!

 

Note - To make it work, you need to enable 'Isolate script' field on this client script.

Meaning it will allow to perform DOM manipulation code written.

 

Thanks