Scheduled Job creating multiple RITMs though query returns only 2 records
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hi everyone,
I am facing an issue with a Scheduled Job that is creating multiple RITMs, even though the query on my custom table returns only 2 records.
Use case
I have a scheduled job that queries the u_surveyor_regulator table to identify contingent workers whose access is ending today, and then submits a catalog item to revoke access.
Query
var gr = new GlideRecord('u_surveyor_regulator');
gr.addQuery('u_active', true);
gr.addQuery('u_worker_creation_status', 'created');
gr.addQuery('u_worker_type', '4');
gr.addEncodedQuery('u_effective_end_dateONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
gr.query();
When I test this in Background Scripts / Script Debugger, it returns exactly 2 records.
Processing logic
while(gr.next()){
var effectiveEndDate = gr.u_effective_end_date.getDisplayValue();
var empNum = gr.u_employee_number;
var user = new GlideRecord('sys_user');
user.addNotNullQuery('employee_number');
user.addQuery('employee_number', empNum);
user.setLimit(1);
user.query();
if(user.next()){
submitCatalogItem(user.sys_id.toString(), effectiveEndDate);
}
}
Catalog submission (using Cart API)
function submitCatalogItem(userSysId, effectiveEndDate){
if (!userSysId) {
gs.error("Invalid userSysId, skipping");
return;
}
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('b2f4a19d3b417e184f138a8a25e45a38');
cart.setVariable(item, 'effective_date', effectiveEndDate);
cart.setVariable(item, 'contingent_worker', userSysId);
var rc = cart.placeOrder();
gs.info("Request Number: " + rc.number);
}
Issue
- Instead of 2 RITMs, multiple RITMs are getting created
- This happens when the script runs as a Scheduled Job
- In Background Script, behavior is correct
Additional info
I also tried using sn_sc.CartJS() API instead of Cart, but the issue persists (multiple RITMs created)
Any guidance or suggestions would be really helpful.
Thanks in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
this KB talks about this
Duplicate RITM records when created via a script within a Scheduled Job
try this script from below post which generates unique cart
Duplicate RITMs are created while submitting catalog item using cart- API
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Still, not working.
However, I observed it is setting Requested for as empty for all the RITM's it is creating uneccessarily.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
it means it's generating duplicate one
I still think it should work with that link.
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hi @maliksneha9
1. Check the schedule job running time/ frequency.
2. Before creating the RITM , \check whether RITM is already created or not .
Here is modified code //not tested
var gr = new GlideRecord('u_surveyor_regulator');
gr.addQuery('u_active', true);
gr.addQuery('u_worker_creation_status', 'created');
gr.addQuery('u_worker_type', '4');
gr.addEncodedQuery('u_effective_end_dateONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
gr.query();
while(gr.next()){
var effectiveEndDate = gr.u_effective_end_date.getDisplayValue();
var empNum = gr.u_employee_number;
var user = new GlideRecord('sys_user');
user.addNotNullQuery('employee_number');
user.addQuery('employee_number', empNum);
user.setLimit(1);
user.query();
if(user.next()){
var existingRITMRecord = new GlideRecord('sc_req_item');
existingRITMRecord.addQuery('contingent_worker', user.sys_id.toString());
existingRITMRecord.addQuery('effective_date', effectiveEndDate);
existingRITMRecord.query();
if (!existingRITMRecord.hasNext()) {
submitCatalogItem(user.sys_id.toString(), effectiveEndDate);
}
}
}
