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.

Copy existing records as new records via UI Action

a99920
Kilo Contributor

I am attempting to create a UI Action that will take the records off of a custom table I have created and if they meet criteria create new records as a copy of the ones that qualified.

I have a complicated switch statement to determine the date that I want to search against but I'm sure that is working properly from other client scripts that I have using that so will skip past determining that in my code. That date variable is called CopyFromDate

....

var userName = g_user.userName;                                                           //currently logged in user

var target = new GlideRecord('time_card');                                     //looking on the time card table

target.addQuery('week_starts_on',CopyFromDate);         //only wanting to copy records whose week_starts_on (date) is the same as CopyFromDate (date)

target.addQuery('user',userName);                                                             //only wanting to copy records where the user is the currently logged in user

target.query();

while (target.next())

{

  var copied= new GlideRecord('time_card');

  copied.initialize();

  copied.u_work_type = target.u_work_type;

  copied.u_application = target.u_application;

  copied.u_project = target.u_project;

  copied.user = target.user;

  copied.state = 'Pending';

  copied.insert();

}

4 REPLIES 4

mike_bush
Kilo Guru

current.insert();


a99920
Kilo Contributor

I added that to my script (and to the post above). Clearly that is necessary and a good catch (thank you!). However, even with that necessary addition I am still getting the message that no records are selected.


I'd be carefully using the variable name "current" as that is typically a system reserved name and may be causing issues.


That's actually a really good point. However, I updated my script (and to the post above) with 'copied' instead of 'current' and am still getting 'No records selected'.