Updating the Records by using Background Script

Arun87
Giga Contributor

Hi,

  We have created a field called First assigned to incident table. Purpose of the field is to capture the first assignee of the ticket. We have created Business rule it is working for new records. We have to update the existing records. Kindly help me how to update the existing records.

 

Thanks in Advance !

1 ACCEPTED SOLUTION

Hi,

example below

if table is audited then you can do this

You can do GlideRecord from following tables,

sys_history_set and sys_history_line

var incident = new GlideRecord('incident');
incident.addQuery('u_first_assigned',''); // give correct field name here
incident.query();

while(incident.next()){

    var history = new GlideRecord('sys_history_set');
    history.addQuery('id',incident.getValue('sys_id'));
    history.query();
    history.next();

    var auditH = new GlideRecord('sys_history_line');
    auditH.addQuery('set',history.getValue('sys_id'));
    auditH.addEncodedQuery("field=assigned_to^newISNOTEMPTY^oldISEMPTY"); // new is not epmty and old is empty
    auditH.query();
    if(auditH.next()){

        incident.u_first_assigned = auditH.new_value; // give correct field name here
        incident.update();

    }

}

If my response helped you please mark it correct to close the question so that it benefits future readers as well.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

14 REPLIES 14

Hi,

I tried with the scripts but it is not working.

Community Alums
Not applicable

try now:

 

 

function fixEmptyFields(){

    var incident_gr = new GlideRecord('incident');

    incident_gr.addNullQuery('u_first_assigned');

    incident_gr.query();

    while(incident_gr.next()){

        incident_gr.setValue('u_first_assigned', incident_gr.sys_created_by);

// Or if not working ok, try with next line

//  incident_gr.setDisplayValue('u_first_assigned', incident_gr.sys_created_by);

        incident_gr.update();
    }
}

 

fixEmptyFields();

Hi,

 

I have tried the above scripts but it is not working.

Hi Arun,

It seems you haven't tried the script which I shared 1 day ago.

I believe I have shared enough information for you to get started.

Please mark my response as correct and helpful to close the thread.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Murali27
Tera Contributor

Hi Arun,

Is your requirement is to update a list of existing records? then please follow below instructions.

  1. Open incidents list view select the records that you want to update with check box.
  2. Right click on column header(list context menu) 
  3. Click  on Update selected 
  4. New form will open, then try to update the fields that required.

 

This will update the all the records you selected.

find_real_file.png