How can I modify value in read only field.

coolek
Giga Expert

Hi,

I have one question. I have created Script Actions and in this scipt I need to modify value in field. But this field is change by Data policy (it has set Read only on "false").

If I print into the log value of this field from the script, then value is empty.

Is possible to change this value?

Thank you, so much.

7 REPLIES 7

Chuck Tomasi
Tera Patron

Hi Petr,



You would change the field value with a GlideRecord operation. Something like this:



Standard disclaimer: The following code is untested, requires review and potential modifications.



      var rec = new GlideRecord('incident');  


      rec.addQuery('active', true);


      rec.query();  


 


      while (rec.next()) {  


                  // Do something


                  rec.update();  


      }  



Because GlideRecord defaults to running as "system", it bypasses the security on the record and fields. Your script action (or almost any other server side script) can update the field even if it is read-only.



http://wiki.servicenow.com/index.php?title=Script_Actions


Thank you for your reply. My code looks like similarly as your example.


But in log is not any error, or warning and incident will not change.



updateIncident();



function updateIncident(){


var gr = new GlideRecord('incident');


gr.get(current.sys_id);


gr.setValue('incident_state', 9);


gr.setValue('assigned_to', 'NULL');



gr.update();


}


Try this instead. Since assigned_to is a sys_id, and 'NULL' is a string (not a null value), you can set it to empty like this:



updateIncident();



function updateIncident(){


var gr = new GlideRecord('incident');


gr.get(current.sys_id);


gr.setValue('incident_state', 9);


gr.setValue('assigned_to', ''); // use empty quotes



gr.update();


}


I tried it before and this didn ´t work too.



On the screen is configuration of the Data policy.


find_real_file.png