Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Scheduled Job creating multiple RITMs though query returns only 2 records

maliksneha9
Mega Sage

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!

4 REPLIES 4

Ankur Bawiskar
Tera Patron

@maliksneha9 

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 

55.png

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Still, not working.

 

However, I observed it is setting Requested for as empty for all the RITM's it is creating uneccessarily.

@maliksneha9 

it means it's generating duplicate one

I still think it should work with that link.

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Tanushree Maiti
Kilo Patron

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);
}

}
}

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: