How to set value to reference field using sys_id

peshwen
Giga Contributor

Hi all,

I am having sys_id of company name and i want to set that company name to reference field using sys_id.So how to do it?Please anyone can tell?

25 REPLIES 25

wolfgangb
Giga Contributor

And how would that work if i am writing a server side JavaScript? Say i have a sys_id and would like to fill a reference field with that reference?


Hi Wolfgang,



You just need to assign the sys_id to the reference field.



for instance...


gr.assigned_to = 'ec0e5f5b4f10d60026afb1828110c754';


If you're not running on an onBefore type of script, then make sure you also do an update after the assignment.


gr.update();



Thanks,


Berny


Well I   am within a scripted REST routine and i did exactly that but I end up with an empty reference field.


Hi Wolfgang, would you like to share your code to take a look to it?



A couple of pointers that may be helpful:



a) make sure your script does not have errors


b) be aware of what's the display value of your reference. If it's blank/null then the reference field will appear in blank but it will have an information icon next to the field that will take you to the assigned record


c) in case you're not doing it already, you may want to hardcode a given sys_id which you know is a valid one for your reference field. Make sure that one it's one which display values exist.



I hope this helps.



Thanks,


Berny


Basically its following code:



  // open CIs affected table


  var cis_aff = new GlideRecord("task_ci");


  cis_aff.initialize();


  cis_aff.setValue('ci_item', mapping.getValue('sn_configuration_item'));


  cis_aff.task = srv.sys_id;   // this contains a reference to the incident


  cis_aff.xml = 'test test';


  cis_aff.update();