script needed

Saib1
Tera Guru

HI All,

there was an issue happened and for the few incident the caller id got blank . when i see the incident history the caller id is blank. Now need to update the caller id on the incident from history table 

 

Saib1_0-1725475136979.png

 

I need a script for the  condition on the incident history table .

 

need to find out the incident which has the caller id is empty and take the Caller Old with latest updated time and insert in caller id field .

 

Do you have the script

7 REPLIES 7

Brad Bowman
Kilo Patron
Kilo Patron

This script will do that:

var incGR = new GlideRecord('incident');
incGR.addNullQuery('caller_id');
incGR.query();
while (incGR.next()) { //loops through ALL incidents with a blank Caller
	var histGR = new GlideRecord('sys_history_line');
	histGR.addQuery('set.id', incGR.getValue('sys_id'));
	histGR.addQuery('field', 'caller_id');
	histGR.addNotNullQuery('old_value');
	histGR.orderByDesc('sys_created_on');
	histGR.setLimit(1);
	histGR.query();
	if (histGR.next()) { //returns the most recent record which has an old value for Caller
		incGR.setValue('caller_id', histGR.getValue('old_value'));
		incGR.setWorkflow(false); //do not run Business Rules on update
		incGR.autoSysFields(false); //do not update sys_updated_on, sys_updated_by,... fields
		incGR.update();
	}
}

Hi   - when i run this script i am getting below error .@Brad Bowman i placed gs.print and did not enter into the if loop

 

i need to add the below query

incGR.addEncodedQuery('u_markerLIKEpb_mig^caller_id.sys_idISEMPTY^task_forISEMPTY^stateIN1,2,3,6');
on the script

 

Saib1_0-1725613834894.png

 

What is line 13 on your script?  Since my script ran fine for me, that could point to bad data retrieved on the history_line, or something in the encoded query.  If you haven't done so, remove the incGR.addNullQuery line, and manually filter an incident list per your encoded query criteria to copy the query to the script.  This probably isn't impacting the results, but it should just be ...caller_idISEMPTY... in an encoded query.

 

caller_idISEMPTY^active=true

@Brad Bowman  

 

var incGR = new GlideRecord('incident');
incGR.addNullQuery('caller_id');
incGR.addEncodedQuery('u_markerLIKEpb_mig^caller_id.sys_idISEMPTY^task_forISEMPTY^stateIN1,2,3,6');
//incGR.addQuery('number','INC4489634');
incGR.setLimit(50);
incGR.query();
while(incGR._next())
{
    var histGR = new GlideRecord('sys_history_line');
    var incSysID = incGR.getValue('sys_id');
    histGR.addQuery('set.id', incGR.getValue('sys_id'));
    histGR.addQuery('field', 'caller_id');
    histGR.addNotNullQuery('old_value');
    histGR.orderByDesc('sys_created_on');
    histGR.setLimit(1);
    gs.print("Line 13");
    histGR.query();
    while (histGR.next()) {
        var oldValue = histGR.getValue('old_value');
        if(oldValue)
        {
        incGR.setValue('caller_id', oldValue);
        incGR.setValue('task_for', oldValue);
        incGR.setWorkflow(false);
        incGR.autoSysFields(false);
        incGR.update();
        }
    }
    }
 
 
Please find the script which i am getting that error