- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2014 05:00 AM
Hi, I am trying to restrict number of users to be selected in list collector to the value of Quantity under cart of catalog item.
I Understand the fields under cart of catalog item doesn't come under catalog variables.
I tried using below script provided by SNCguru, When I am changing the value of maxOptions to 5 then it's working but while I am using "gel('quantity')" i's not working. Please suggest me for any correction.
function onChange(control, oldValue, newValue, isLoading) {
//Limit the number of selected options in a list collector
//Specify the max options and variable name below
var maxOptions = gel('quantity');
var varName = 'users';
var leftBucket = gel(varName + '_select_0');
var rightBucket = gel(varName + '_select_1');
var selectedOptions = rightBucket.options;
if(selectedOptions.length > maxOptions){
//Move any options with IDs greater than maxOptions back to left bucket
var selectedIDs = new Array();
var index = 0;
for(var i = maxOptions; i < selectedOptions.length; i++){
selectedIDs[index] = i;
index++;
}
//Move options and sort the left bucket
moveSelectedOptions(selectedIDs, rightBucket, leftBucket, '--None--');
sortSelect(leftBucket);
alert('You cannot select more than ' + maxOptions + ' options.')
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2014 10:29 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2014 06:32 AM
Hi,
try the below code to get the quantity value.
var e = document.getElementById("quantity");
var quantity = e.options[e.selectedIndex].value;
alert(quantity);
Hope it's useful for you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2014 10:29 PM
gel('quantity').value; worked for me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2017 04:22 PM
For those looking for a version that will work with both Service Portal and native UI:
You'll need to create a Script Include called "ListCollectorAJAX" -- be sure to check "Client Callable":
var ListCollectorAJAX = Class.create(); ListCollectorAJAX.prototype = Object.extendsObject(AbstractAjaxProcessor, { getDisplayValues: function() { var records = []; var ids = this.getParameter("sysparm_ids"); var tableName = this.getParameter("sysparm_tablename"); if(ids == "") return ""; var theIDs = ids.split(","); for(var i=0; i<theIDs.length; i++) { var rec = new GlideRecord(tableName); if(rec.get("sys_id", theIDs[i])) { records.push(rec.getDisplayValue()); } } var toReturn = {}; toReturn.ids = ids; toReturn.display = records.join(","); toReturn.varname = this.getParameter("sysparm_varname"); toReturn.maxoptions = this.getParameter("sysparm_maxoptions"); toReturn.tablelabel = this.getParameter("sysparm_tablelabel"); return JSON.stringify(toReturn); }, type: 'ListCollectorAJAX' });
Client Script then becomes:
function onChange(control, oldValue, newValue, isLoading) { //Limit the number of selected options in a list collector //Specify the max options and variable name below var maxOptions = 20; var varName = "variablename_here"; var tableName = "sys_user_group"; var tableLabel = "Projects"; var records = String(newValue).split(","); try { var leftBucket = gel(varName + '_select_0'); var rightBucket = gel(varName + '_select_1'); var selectedOptions = rightBucket.options; if(selectedOptions.length > maxOptions){ //Move any options with IDs greater than maxOptions back to left bucket var selectedIDs = []; var index = 0; for(var i = maxOptions; i < selectedOptions.length; i++){ selectedIDs[index] = i; index++; } //Move options and sort the left bucket moveSelectedOptions(selectedIDs, rightBucket, leftBucket, '--None--'); sortSelect(leftBucket); alert('You cannot select more than ' + maxOptions + ' '+tableLabel+'.'); } } catch(e) { if(records.length > maxOptions) { records.splice(-1); //Set it back to the previous options? var ga = new GlideAjax("ListCollectorAJAX"); ga.addParam("sysparm_name", "getDisplayValues"); ga.addParam("sysparm_varname", varName); ga.addParam("sysparm_ids", records); ga.addParam("sysparm_tablename", tableName); ga.addParam("sysparm_tablelabel", tableLabel); ga.addParam("sysparm_maxoptions", maxOptions); ga.getXMLAnswer(function(answer) { try{ answer = answer.evalJSON(); } catch(e){ answer = JSON.parse(answer); } g_form.setValue(answer.varname, answer.ids, answer.display); alert('You cannot select more than ' + answer.maxoptions + ' '+answer.tablelabel+'.'); }); } } }