Updating reference fields with 'NULL'

marcelorsc
Kilo Expert

Could anyone share some thoughts on the best way to have a reference field (in my case 'assigned_to' updated with a null value?  

When I run the background script below it seems to be working (Assigned To is blanked out) but am I not using 'NULL' as a string in that case?

Script:

updateAssignedToNull();

function updateAssignedToNull() {

  var gr = new GlideRecord('incident');

  gr.addQuery ('sys_id', 'b462031d2ba50a00345459efe8da15a1'); //sys id of my test incident

  gr.query();

  while (gr.next()) {

  gs.print("Incident is: " + gr.number + " Assigned To before is: " + gr.assigned_to);

  gr.assigned_to = 'NULL';

  gs.print("Incident is: " + gr.number + " Assigned To after is: " + gr.assigned_to);

  gr.update();

  }

}

Output:

*** Script: Incident is: INC120559 Assigned To before is: a49664cec0cda080242596520e8ce6c2 //my user sys id, used to populate assigned_to on my test incident

*** Script: Incident is: INC120559 Assigned To after is: NULL

Thanks,

Marcelo Correia

1 ACCEPTED SOLUTION

john_roberts
Mega Guru

You should be setting the field value to an empty string:


gr.assigned_to = "";


By setting to NULL in quotes the system will see a value and display the reference icon on the field when displayed on a form, but the link will be dead since it doesn't match a reference sys id.


View solution in original post

1 REPLY 1

john_roberts
Mega Guru

You should be setting the field value to an empty string:


gr.assigned_to = "";


By setting to NULL in quotes the system will see a value and display the reference icon on the field when displayed on a form, but the link will be dead since it doesn't match a reference sys id.