Need to iterate through ui_checkbox elements in jelly ui_form

MM16
Giga Contributor

I am trying to create a UI Page to display inventory items and allow a user to check out several of them. My UI Page displays the items with checkboxes and on submit I want to iterate through them and identify what was checked. How do I iterate through these elements to find what was selected in the Processing Script of the UI Page (in order to create a request listing them and mark them unavailable in inventory)? I have used following for the checkboxes in order to retrieve the item sys_id for every one that was checked (which will have a value of true):

'<g:ui_checkbox name="check'+ grItem.sys_id + '" value=""/>'

Also, is there a way in the Processing Script to enumerate all values passed from the UI Page, as in ASP server variables and form elements?

3 REPLIES 3

brnunn
Tera Contributor

Did you figure this one out? I need to do something similar


jeffrey_leboeuf
ServiceNow Employee
ServiceNow Employee

There may be a better way to do this, but this just worked for me.



Run your query to gather your dynamic list of sys_ids.



Then using a <j:while> or however you want to loop, build your dynamic checkbox list:



<g:ui_checkbox name="${gr.sys_id}" />



Now, in the processing script, run that same query again so you'd get the same list of sys_ids, and loop through the results, you can get the value (true/false) using:



request.getParameter(gr.sys_id);



So essentially, it would look like in your processing script:



while (gr.next()) {


  gs.log("checkbox value is: " + request.getParameter(gr.sys_id));


}



Hope this makes sense, hope this helps, maybe someone has a better way.


Hi Jeffrey,



I am trying to achieve something similar. As suggested by you I   am looping my checkbox list and able to get the list of sys_id using request.getParameter but I am not able to set values further. Can you pls guide me here as I am a beginner in SNOW.



<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


<g:evaluate var="appID" object="true" jelly="true">


var appID;


  var gr = new GlideRecord('sys_app');


  gr.addQuery('scope','x_14777_nous_tpp');


  gr.query();


  while(gr.next()){


  appID = gr.sys_id;


  }


  appID;


</g:evaluate>


<g:evaluate var="gr_cat_item" object="true" jelly="true">


  var catItemId;


  var ecString;


  var gr2 = new GlideRecord('sc_cat_item_producer');


  gr2.addQuery('sys_scope', appID);


  gr2.query();


  while(gr2.next()){


  catItemId = gr2.sys_id;


  }


  ecString = "cat_item.sys_id="+catItemId;



</g:evaluate>



<g:evaluate var="gr_item_vars" object="true" jelly="true">



  var gr3 = new GlideRecord('item_option_new');


  gr3.addEncodedQuery(ecString);


  gr3.addNotNullQuery('name');


  gr3.addQuery('type','NOT IN','12,20,24,19,11');


  gr3.query();


  gr3;


</g:evaluate>



<g:ui_form>


<table style="border-collapse: separate; border-spacing: 5px 5px;">



  <j:while test="${gr3.next()}">


  <j:set var="gr3name" value="${gr3.name}" />


  <g:evaluate var="jvar_isChecked">


  var isChecked = gs.getProperty('x_14777_nous_tpp.show_'+gr3.name);


  isChecked;


  </g:evaluate>


  <tr>


  <td>



  <g:ui_checkbox name="${gr3.sys_id}" value=" ${isChecked}"/>



  <td>$[SP]<b>${gr3.question_text} ${gs.getProperty('x_14777_nous_tpp.show_'+gr3.name)}</b></td>


  </tr>


  </j:while>


  <tr><td colspan="2" height="15px"></td></tr>


<tr>


  <td colspan="2">


  <g:dialog_buttons_ok_cancel ok="return true" cancel="return false"/>



  </td>



  </tr>


  </table>


</g:ui_form>


</j:jelly>


--------------


Processing Script


var appID;


var gr = new GlideRecord('sys_app');


gr.addQuery('scope','x_14777_nous_tpp');


gr.query();


while(gr.next()){


  appID = gr.sys_id;


}




var gr3 = new GlideRecord('sys_properties');


gr3.addQuery('sys_scope', appID);


gr3.query();


while(gr3.next()) {


  gs.debug("checkbox value is: " + request.getParameter(gr3.sys_id));




// if(value==true){


// gs.setProperty("x_14777_nous_tpp.show_"+gr3.name, "true" );


// }


// else{


// gs.setProperty("x_14777_nous_tpp.show_"+gr3.name, "false" );


// }


}