Scheduled job to create record

Luis Nunez
Tera Expert

Hello,

Need a little help:

We have a boolean field for locations (18)   on the user table, if any of these fields are true, a scheduled job should run and query these and create a record for each true field on a separate table (Tape location). I actually managed to get this working with the following scheduled script:

AddTapeLocationLon();

function AddTapeLocationLon() {

  var gr = new GlideRecord('sys_user');

  var gtarget = new GlideRecord('u_tape_location');

  gr.addQuery('u_loc_lon', true);

  gr.query();

  while (gr.next()) {

  gtarget.u_tape_access = "London";

  gtarget.u_employee_name = gr.sys_id;

  gtarget.u_employee_number = gr.employee_number;

  gtarget.insert();

  gs.log("New location inserted: " + gtarget.u_number + " - " + gtarget.u_employee_name.name + " - " + gtarget.u_tape_access, "NUN");

  }

}

The issue is, that every time I run this job a new record gets created for an already existing entry.

I need to add a condition to this job in order to avoid this.

That would be, if any user has u_loc_lon set to true on the user table and matches a record on the Tape location table where the the user exists and has u_tape_access = "London". Of course, as always your help is much appreciated.

PS. I have 18 locations, so on this job, I need to create 18 different functions, one for each location, would there be a way around this, to wrap all into one function?

Cheers,

Luis

1 ACCEPTED SOLUTION

Got it ... you need to move the GlideRecord statement in while loop.... Try following code



AddTapeLocationLon();


function AddTapeLocationLon() {



  var gr = new GlideRecord('sys_user');


  gr.addQuery('u_loc_lon', true);


  gr.query();


  while (gr.next()) {


  var gtarget = new GlideRecord('u_tape_location');


  gtarget.addQuery('u_employee_name',gr.sys_id);


  gtarget.addQuery('u_tape_access','London');


  gtarget.query();


  if(!gtarget.hasNext()){


  gtarget.u_tape_access = "London";


  gtarget.u_employee_name = gr.sys_id;


  gtarget.u_employee_number = gr.employee_number;


  gtarget.insert();


  gs.log("New location inserted: " + gtarget.u_number + " - " + gtarget.u_employee_name.name + " - " + gtarget.u_tape_access, "NUN");


  }


}


}


View solution in original post

14 REPLIES 14

Gurpreet07
Mega Sage

Try following



AddTapeLocationLon();  


function AddTapeLocationLon() {  


 


  var gr = new GlideRecord('sys_user');  


  var gtarget = new GlideRecord('u_tape_location');  


  gr.addQuery('u_loc_lon', true);  


  gr.query();  


  while (gr.next()) {  


  gtarget.addQuery('u_employee_name',gr.sys_id);


  gtarget.addQuery('u_tape_access','London');


  if(!gtarget.hasNext()){


  gtarget.u_tape_access = "London";  


  gtarget.u_employee_name = gr.sys_id;  


  gtarget.u_employee_number = gr.employee_number;  


  gtarget.insert();  


  gs.log("New location inserted: " + gtarget.u_number + " - " + gtarget.u_employee_name.name + " - " + gtarget.u_tape_access, "NUN");  


  }  


}


}  


Hi Gurpreet,


This worked... I had to amend it... you had missed the gtarget.query(); line...


Thanks much for the help...



Cheers,


Luis


Hey Gurpreet,


If you have time, would you please be so kind to maybe help on this other post, its about the same thing except its instead of creating a record, deleting one... same parameters, same tables.



Scheduled job to delete record



Thanks again for your help!



Cheers,


Luis


Hey Gurpreet,


After further testing I found that the last record queried is being created multiple times.


3 users for location london found


3 records are created, 1 for userA, 1 for userB, one for userC.


When the job runs again, a new record for userC is created


If I add a new user to location london: new record for userC will be created and record for userD will be created.


On the next run, records for user C stop being created and a new record for userD is created... and so on...


Any ideas?



Cheers,


Luis


How you are calling scheduled job from user   table ? Can't you use business rule in there ? Please share your current code and also the calling code for this scheduled job.