Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Script to copy dates from one field to another

danenglish
Giga Expert

Hi all.   I'm frustrated after a couple of hours of trying to do something that I would normally consider simple.

I need a script to copy all of the dates from one field in the planned_task table to another.   There is a field called start_date that is full of data (and only on the planned_task table) that I want to copy to the field called due_date (which is extended from the task table), because the previous administrator of the system created the start_date field to replace due_date for some reason, but I want to use due_date so it rolls up to the task table.

I am fine on the business rule side with updating this information as records are created/updated, but I need a script to go through the current records and copy all of this data to the proper field.

All help is always greatly appreciated!  

1 ACCEPTED SOLUTION

Hi Dan,



You can copy below in your fix script and run fix script.



var ptask = new GlideRecord('planned_task');


ptask.addEncodedQuery('numberISNOTEMPTY');


ptask.query();


while(ptask.next()){



ptask.due_date = ptask.start_date;


ptask.update();



}


View solution in original post

6 REPLIES 6

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Dan,



You can create a fix script or execute one-time script via background script to copy the field for existing records.


Sample script:


var gr = new GlideRecord('incident');


gr.addQuery("active=true^sys_created_on<javascript:gs.daysAgoStart(0)");


gr.query();


while(gr.next())


  {


  gr.setValue('short_description',   gr.u_test);


  gr.setWorkflow(false);


  gr.update();



}


Reference:


Fix Scripts - ServiceNow Wiki


http://wiki.servicenow.com/index.php?title=GlideRecord#gsc.tab=0


Background Scripts — ServiceNow Elite


danenglish
Giga Expert

Thanks all, I'll give these a try in my dev environment today and see how it goes!