- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2015 03:14 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2015 04:53 AM
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");
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2015 01:04 AM
Scheduled job just runs daily at a specific time.
Code:
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');
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");
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2015 01:44 AM
Logic should work fine .. you could try by replacing the condition to getRowCount
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');
gtarget.query();
if(gtarget.getRowCount() == 0){
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");
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2015 04:22 AM
with getRowCount does something different. Only creates a record for the first match it finds.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2015 04:53 AM
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");
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2015 05:23 AM
Perfect!!!
Thank you!