Make record producer variables readonly after submit in custom app scope

kikiran
Kilo Contributor

I am trying to make variables readonly after submitting in a custom application. I tried using UI policies, client scripts and also scratchpad to make them readonly. But nothing works.

Here is the client script where g_scratchpad is populated in a BR.

function onLoad(){

  if(g_form.getValue('record_producer') == 'xxxxxxxxxxxxxxxxxxxxx'){

//   g_form.setVariablesReadonly('true');

  g_scratchpad.variable_names.forEach(function(v) {

  var field_name = "variables." + v;

  g_form.setReadOnly(field_name, 'true');

  });

  }

}

I am not sure of the proper way to achieve this.

Any assistance that can be offered would be much appreciated.

Thanks,

Kiran

4 REPLIES 4

Omar Ashqar
Tera Contributor

Hi kiranmayi,



Have you tried setting a ACL(to read only) and with a condition.


Harneet Sital
Mega Sage
Mega Sage

Hi Kiranmayi,



The variable editor on the RITM form brings in the variables from the catalog item just raised [Variable Editor is like a single field that contains all the variables from the catalog item]. As UI policies can be only applied on the variable available on a particular form, in this case that is not feasible.


One way to make the variable editor Read-Only is below:



Client Script


Name: VariableEditor Read-Only


Type: onLoad


Table: Custom_table_name


Script:



function onLoad() {  


  //assuming the requested for field is being shown on the item. if not you may have to use a display business rule of getreference  


  var reqFor = g_form.getValue('request.requested_for');  


  if (reqFor == g_user.userID) {  


          //Get the 'Variables' section  


          var ve = $('variable_map').up('table');  


          //Disable all elements within with a class of 'cat_item_option'  


          ve.select('.cat_item_option', '.slushselectmtm', '.questionsetreference').each(function(elmt){  


              elmt.disabled = true;  


          });  


          //Remove any reference or calendar icons  


          ve.select('img[src*=reference_list.gifx]', 'img[src*=small_calendar.gifx]').each(function(img){  


              img.hide();  


          });  


          //Hide list collector icons  


          ve.select('img[src*=arrow]').each(function(img){  


              img.up('table').hide();  


          });  


  }  


}  


Hi Harneet,


when recommending this script, could you please also mention that it will not be supported by ServiceNow? It uses direct DOM manipulation, which we actively discourage and do not support. Please see ServiceNow KB: Date Variable values change when saving a record that includes the Variable Editor, i...   for more information.


kikiran
Kilo Contributor

Hi all, thanks for your responses. Unfortunately , because the record producer is in a scoped app, we could not use any of these.


We could not even use the ACLs as there are multiple record producers on the same table and they all needed to be editable after submit.



We had to Create a UI Macro in Global scope and a Formatter also in global.


Here is the macro that worked for us:



<?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:requires name="scripts/js_includes_catalog.js" includes="true"/>


  <g:requires name="styles/${new CatalogCssSelector().getVariableCss()}" includes="true"/>


      <g2:render_component componentName="com.glideapp.questionset.DefaultQuestionEditor" />


  <g2:evaluate var="jvar_sefu">


  //gets the variables in the backend ui policy for the record producer used to create the enquiry


  var recProducerID = current.record_producer.toString();



  var sefu = new xxxUtils();


  var out1 = sefu.getUIPolicies(recProducerID, 'UI_Policy_Readonly');


  out = JSON.stringify(out1);



  out;


  </g2:evaluate>


  <div id="ui_policy_data" data-sefu="$[jvar_sefu]"></div>




  <script>


  $j(document).ready( function(){


  var sefu = ($j('#ui_policy_data').data('sefu'));


  for(var i = 0; i ${AMP}lt; sefu.length; i++){



  //checks if variable is a checkbox


  //$j(hidevar).prop("readonly", true);


  if (sefu[i].qtype == '7') {


  var hidevar = '#ni\\.ni\\.QS' + sefu[i].sys_id;


  $j(hidevar).attr("disabled", "disabled");


  } else if (sefu[i].qtype == '4'){


  var hidevar = 'ni\\.QS' + sefu[i].sys_id;


  $j("[name='"+hidevar+"']").attr("disabled", "disabled");


  } else if (sefu[i].qtype == '8'){


  var hidevar = '#sys_display\\.ni\\.QS' + sefu[i].sys_id;


  $j(hidevar).attr("disabled", true);


  $j(hidevar).next().css("display", "none");


  } else {


  //make all other types of variables readonly


  var hidevar = '#ni\\.QS' + sefu[i].sys_id;


  $j(hidevar).prop("readonly", true);


  }


  }


  });


  </script>


</j:jelly>