Scheduled Script - Check For Duplicates???
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-20-2012 11:05 AM
Hi Everyone, is it possible to check for duplicate records in a scheduled script job? I am almost to the end of this time card project - there are just a few loose ends that need to be tied. Heres the scheduled script...
var string = "roles=itil^department=f8411c2f78a75c00910063ad31c0bf9c^active=true"; var GG = new GlideRecord('time_card'); var HH = new GlideRecord('sys_user'); HH.addEncodedQuery(string); HH.query(); while(HH.next()) { GG.initialize(); GG.week_starts_on.setValue(gs.nowNoTZ()); GG.user = HH.name.toString(); GG.u_total_hours = 0; GG.state = 'Pending'; GG.insert(); }
Its creating a timecard every Sunday for 256 users. In order to prevent the system from creating duplicates (if a user created one ahead of time), I have the following business rule...
var gr = new GlideRecord("time_card"); gr.addQuery("week_starts_on",current.week_starts_on); gr.addQuery("user",current.user); gr.addQuery("sys_id","!=",current.sys_id); gr.query(); if (gr.next()) { current.setAbortAction(true); }
This business rule is causing unexpected behaivor. If I run the script with it off, 256 are created. When it is on, if I manually create a card, then run the script, it wont create a duplicate card; however, it only creates 255. It skips a user. The user seems to be random each time. I am not sure what the exact issue is. I was hoping I could perform the check in the script. Any guidance is greately appreciated!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-27-2012 06:29 AM
I was wondering how you managed to have what look like "out of the box" fields (no "u_") on your time card fields!
I didn't find an existing time_card table on the demo site. So every field I added has a "u_" in front of it.
One other suggestion I have: Instead of using the text value of the user's name, save the user's sys_id on the time card. An object reference is almost always preferable if available.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-27-2012 11:11 AM
So, I finished combining the business rule into the script. Heres the updated code that is doing what I want it to....
CreateTimeCards();
function CreateTimeCards()
{
var GG;
var FF;
var DD;
var HH;
var string = "active=true^department=f8411c2f78a75c00910063ad31c0bf9c^roles=itil";
HH = new GlideRecord('sys_user');
HH.addEncodedQuery(string);
HH.query();
var x = 1;
while(HH.next())
{
GG = new GlideRecord('time_card');
GG.addQuery('week_starts_on',gs.nowNoTZ());
GG.addQuery('user',HH.sys_id);
GG.query();
if (!GG.next())
{
GG = new GlideRecord('time_card');
GG.initialize();
GG.setWorkflow(false);
GG.week_starts_on.setValue(gs.nowNoTZ());
GG.user = HH.name.toString();
GG.u_total_hours = 0;
GG.state = 'Pending';
sid = GG.insert();
gs.log('GBC: Creating record for ' + x + ': ' +HH.name.toString() + ', response = ' + sid);
x++;
FF = new GlideRecord("u_m2m_time_card");
DD = new GlideRecord("u_administrative_tasks");
DD.query();
while (DD.next())
{
FF.initialize();
FF.u_time_card = GG.sys_id;
FF.u_state = 'Pending';
FF.u_task = DD.sys_id.toString();
FF.insert();
}
}
}
}
So heres what I am thinking, since this script performs what two of my other business rules were doing, I was debating getting rid of it. Then I thought, what if someone manually creates a card? I still need a business rule to populate those cards. I guess this is where I am seeking advice. Should I just leave the business rule that populates manually created cards? Or instead, should I perform that client side? I have duplicate card checking for manually created cards being done on the client in combination with an AJAX class and the same goes for checking the week starts on day. Maybe I should just do everything on the client. What do you think?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-27-2012 12:09 PM
I think all that stuff that you are doing after you insert the time card record should probably be done in an "After Insert" business rule. That way it will work both on the cards inserted by the script, or by cards inserted manually.
I would avoid doing anything on the client side that you don't have to, and then only to update the display for the human looking at the screen. Client side stuff doesn't work when you are inserting with a script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-28-2012 05:54 AM
That is how I originally approcahed the situation. I do have an After Insert business rule that was populating the cards whether it was created by the system or a user. What was weird was that I would occasionally get a card that was blank. This was before I had the duplicate check in the script and was doing that in a business rule as well. Maybe that had some influence.