The CreatorCon Call for Content is officially open! Get started here.

Clear/Set value of g:ui_reference field in ui page

gowthamanbalu
Tera Contributor

Hi Guys,

        In the below UI page,

                        1. I want a function wherein, if shared check box is enabled, Assigned to reference field should be cleared. I tried with the below client script, the value of assigned to field is changing to null but the same is not reflecting in the UI form.

                        2. If assignee is populated, based on assignee filed value, cost center field should auto populated. Here also, cost center value is changing and i can see the value when I give alert but the same is not reflecting in the UI form.

UI Page:

<?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:ui_form>

  <style>

li {

  display:inline;

  padding: 5px;

}

  </style>

  <header>

  <h3 style="color:blue;">ASSIGN ASSET FROM STOCK</h3>

  </header><hr></hr>

  <table>

  <tr id="dialogbuttons">

  <td>

  <label for="${jvar_ci_query}" onclick="" dir="">${gs.getMessage('In Stock Asset to be Assigned')}

  <span class="required">*</span></label>

  </td>

  <td>

  <g:ui_reference id="assetToBeReturned" name="QUERY:u_asset_state!=1^assigned_toISEMPTY^u_is_shared=false" table="alm_asset"/>

  </td></tr>

<tr id="dialogbuttons">

<td>

  <label for="${jvar_ci_query}" name="assignto" id="assignto" onclick="" dir="">${gs.getMessage('Assign to')}

  <span class="required">*</span></label>

  </td>

  <td><div id="assigndiv">

  <g:ui_reference name="assignedto" id="assignedto" table="sys_user" onchange="clrasgn();"/>

  </div></td></tr>

  <tr id="dialogbuttons">

<td>

  <label for="${jvar_ci_query}" onclick="" dir="">${gs.getMessage('Cost Center')}

  <span class="required">*</span></label>

  </td>

  <td>

  <g:ui_reference name="costcenter" id="costcenter" table="cmn_cost_center"/>

  </td></tr>

  <tr id="dialogbuttons">

<td>

  <label for="${jvar_ci_query}" onclick="" dir="">${gs.getMessage('Location ')}

  <span class="required">*</span></label>

  </td>

  <td>

  <g:ui_reference name="location" id="location" table="cmn_location"/>

  </td></tr>

  <tr id="dialogbuttons">

<td>

  <label for="${jvar_ci_query}" onclick="" dir="">${gs.getMessage('Shared')}

  <span class="required">*</span></label>

  </td>

  <td>

  <input type="checkbox" name="shared" id="shared" onclick="shardType();" />

  </td></tr>

  <tr id="dialogbuttons">

<td>

  <label for="my_choice" id="my_choice" style="display: none">Shared Type

  <span class="required">*</span></label>

  <select name="my_substate" id="my_substate" onclick="optionSelect();" style="display:none">

  <option selected = "selected">--None--</option>

              <option> Branch </option>

  <option> Call Center </option>

  <option> Workstation Scanner </option>

  <option> Other </option>

</select>

  </td></tr>

  <tr id="dialogbuttons">

<td>

  <label for="${jvar_ci_query}" name="brachname" id="brachname" onclick="" dir="" style="display: none">${gs.getMessage('Branch')}

  <span class="required">*</span></label>

  </td>

  <td><div id="branchdiv" style ="display: none">

  <g:ui_reference name="branch" table="u_extra_location" />

  </div></td></tr>

  <tr id="dialogbuttons">

<td>

  <label for="${jvar_ci_query}" id="cubelab" onclick="" dir="" style="display: none">${gs.getMessage('Cube Number')}

  <span class="required">*</span></label>

  </td>

  <td>

  <input type="text" name="cube" id="cube" style="display: none"/>

  </td></tr>

  <tr id="dialogbuttons">

<td>

  <label for="${jvar_ci_query}" id="otherlab" onclick="" dir="" style="display: none">${gs.getMessage('Other')}

  <span class="required">*</span></label>

  </td>

  <td>

  <input type="text" name="other" id="other" style="display: none"/>

  </td></tr>

<td>

  <g2:dialog_button onclick="return onSubmit();" name="assignasset" id="assignasset">${gs.getMessage('Assign Asset')}</g2:dialog_button>

  </td>

  </table>

  </g:ui_form>

</j:jelly>

Client Script:

function clrasgn(){

shardType();

}

function shardType(){

  var sh= document.getElementById("shared").checked;

  if (sh == true){

  //var temp = '';

  //document.getElementsByName("assignedto").style.visibility = "hidden";

  document.getElementById("my_substate").style.display = "block";

  document.getElementById("my_choice").style.display = "block";

  document.getElementById("my_substate").value = "--None--";

  //alert(gel('assignedto').value);

  //document.getElementById("assignedto").value="";

  //document.getElementById("assigndiv").style.display = "none";

  //document.getElementById("assignto").style.display = "none";

  //document.getElementById("assignedto").innerHTML = temp;

  gel('assignedto').value="";

  alert(gel('assignedto').value);

  //function checkShrd(){

  //var asn = gel('assignedto').value;

  // alert(asn);

  //if (asn != ''){

  //alert('test');

  //var temp='';

  //gel('assignedto').value= temp;

  //var query= gel('assignedto').value;

  //alert(query);

  //return query;

  //}

  //return null;

  //alert(asn);

  //document.getElementById("assignedto").value="";

  //g:ui_reference.setdisplayalue('assignedto','');

  //alert(gel('assignedto').value);

  }

  //asn.value="";

  //alert(gel('assignedto').value);

  //return asn.value;

  else if (sh == false){

  document.getElementById("my_substate").style.display = "none";

  document.getElementById("my_choice").style.display = "none";

  document.getElementById("assigndiv").style.display = "block";

  document.getElementById("assignto").style.display = "block";

  //document.getElementById("assignedto").style.visibility = "visible";

  document.getElementById("brachname").style.display = "none";

  document.getElementById("branchdiv").style.display = "none";

  document.getElementById("cube").style.display = "none";

  document.getElementById("cubelab").style.display = "none";

  document.getElementById("other").style.display = "none";

  document.getElementById("otherlab").style.display = "none";

  }

}

var sh= document.getElementById("shared").checked;

document.getElementsByName('my_substate').onchange = function() {

  if (this.value=='--None--') {

  alert('Please Select Shared Type !');

  document.getElementById("brachname").style.display = "none";

  document.getElementById("branchdiv").style.display = "none";

  document.getElementById("cube").style.display = "none";

  document.getElementById("cubelab").style.display = "none";

  document.getElementById("other").style.display = "none";

  document.getElementById("otherlab").style.display = "none";

  }

  if (this.value == 'Branch' &&   this.value !='Call Center' && this.value !='Workstation Scanner' && this.value!='Other' ) {

  document.getElementById("brachname").style.display = "block";

  document.getElementById("branchdiv").style.display = "block";

  document.getElementById("cube").style.display = "none";

  document.getElementById("cubelab").style.display = "none";

  document.getElementById("other").style.display = "none";

  document.getElementById("otherlab").style.display = "none";

  }

  if ((this.value == 'Call Center' ||   this.value == 'Workstation Scanner') && this.value != 'Branch' && this.value != 'Other') {

  document.getElementById("brachname").style.display = "none";

  document.getElementById("branchdiv").style.display = "none";

  document.getElementById("cube").style.display = "block";

  document.getElementById("cubelab").style.display = "block";

  document.getElementById("other").style.display = "none";

  document.getElementById("otherlab").style.display = "none";

  }

  if (this.value == 'Other' && this.value !='Call Center' && this.value !='Workstation Scanner' && this.value!='Branch' ) {

  document.getElementById("brachname").style.display = "none";

  document.getElementById("branchdiv").style.display = "none";

  document.getElementById("cube").style.display = "none";

  document.getElementById("cubelab").style.display = "none";

  document.getElementById("other").style.display = "block";

  document.getElementById("otherlab").style.display = "block";

  }}

  //function costcal(){

document.getElementsByName('assignedto')[0].onchange = function() {

  //var asn = document.getElementsById('assignedto').value;

  if (this.value!='') {

  //var asn = document.getElementsById('assignedto').value;

  var asn = gel('assignedto').value;

  //alert('inside'+asn);

  var assignto = new GlideRecord('sys_user');

  assignto.addQuery('sys_id',asn);

  assignto.query();

  if (assignto.next()){

  //alert(assignto.name);

  var cst = assignto.u_cost_center;

  //alert(assignto.u_cost_center);

  //alert(cst);

  if (cst != ''){

  var costcen = new GlideRecord('cmn_cost_center');

  costcen.addQuery('name','STARTSWITH',cst);

  costcen.query();

  if (costcen.next()){

  //var cstname = document.getElementById("costcenter");

  //cstname = costcen.name;

  //alert(costcen.name);

  //alert(cstname);

  //var cstn = gel('costcenter').value;

  //cstn.setvalue = costcen.name;

  gel('costcenter').value = costcen.name;

  document.getElementById("costcenter").value = costcen.name;

  alert(costcen.name);

  //costcen.update();

  //return cstn;

  //document.getElementById("costcenter").value = costcen.name;

  //g:ui_form.setValue('costcenter',costcen.name);

  }}}}}

  function onSubmit(){

  var ast = gel("QUERY:u_asset_state!=1^assigned_toISEMPTY^u_is_shared=false").value;

  //alert(ast);

  var asgn = gel('assignedto').value;

  //alert(asgn);

  var cstcen = gel('costcenter').value;

  var loc = gel('location').value;

  var shr= document.getElementById("shared").checked;

  //var shrdtype = gel('sharedtype').value;

  //var brn = gel('branch').value;

  //var cu = gel('cube').value;

  //var olab = gel('otherlab').value;

  //var otr = gel('other').value;

  if (ast == '') {

  alert('Please Asset Detail');

  return false;}

  if (asgn == '' && shr == false) {

  alert('Please Assignee/Shard Detail');

  return false;}

  if (cstcen == '') {

  alert('Please cost Detail');

  return false;}

  if (loc == '') {

  alert('Please location Detail');

  return false;}

  if (shr == true){

  var shrdtype = gel('my_substate').value;

  //alert(shrdtype);

  if (shrdtype == '--None--'){

  alert('Please select shard type');

  return false;

  }

  if (shrdtype == 'Branch'){

  var brn = gel('branch').value;

  if (brn == '') {

  alert('Please Branch Detail');

  return false;}}

  if (shrdtype == 'Call Center' || shrdtype == 'Workstation Scanner'){

  var cu = gel('cube').value;

  if (cu == '' ) {

  alert('Please Cube Detail');

  return false;}}

  if (shrdtype == 'Other'){

  var otr = gel('other').value;

  if (otr == '') {

  alert('Please Other Detail');

  return false;}}}

  return true;

  }

13 REPLIES 13

Kalaiarasan Pus
Giga Sage

gel('assignedto').value = sys id of record;


gel('sys_display.assignedto').value = display value of record;


Hello Kalaiarasan,



If I clear the display value and sys_id of a ui_reference field with the following code:


gel('sys_display.reference_field').value = "";


gel('reference_field').value = "";



Both values are cleared, as expected. However, the "i" symbol next to the field remains visible. Opening it allows the user to create a new record for the reference field table. Should I be setting the sys_id value to something other than an empty string in order to disable the "i" icon?



I tried null and undefined. For both, the page says "Record not found". This is better, but not quite optimal.


Hey Trevor, did you ever figure out how to get rid of that "i" icon?



Thanks!


Hello David,



Reviewing this, it doesn't appear that I ever did determine a solution. However, I just looked at it again and have figured it out. To hide the reference icon, use the following line:


gel('<field name>LINK').style = 'visibility: hidden; display: none;'



For example:


gel('companyLINK').style = 'visibility: hidden; display: none;'



I noticed a third HTML element, display_hidden.<field_name>, that continues to hold the reference value after clearing it with the code used above. It is cleared when removing the value from the field using the UI. I did not notice any effects from leaving it alone. However, it can be cleared with gel('display_hidden.<field_name>').value = '';. This would result in the following code to completely clear a reference field:


gel('user').value = '';


gel('sys_display.user').value = '';


gel('display_hidden.user').value = ''; // optional


gel('userLINK').style = 'visibility: hidden; display: none;'



Let me know if this works for you.



- Trevor Muhl