- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-11-2025 12:44 AM
Good afternoon.
I have a Catalog Form, where new users details will be entered. Then a Scheduled Job runs in the background and submits.
The requirement is: this form should only create RITM/REQTASK for new users. But what is happening is, if the existing user is given, then also it is creating RITM but not REQTASK. A workflow is also running in the background.
Example: Assume a person's profile got created (sys_created_on) in 2016 and his start date (u_start_date) is 2024 will be mentioned on his sys_user profile. But the point is, he is an existing user, but then too, for him, if we submit the form, RITM is getting created. Please check the below code and help me out.
The script is below:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-11-2025 06:25 AM
try this
answer = ifScript();
function ifScript() {
var date;
var user = '105d95b80f3f52007b3a3e7ce1050edc';
var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id", user);
gr.query();
if (gr.next()) {
date = gr.sys_created_on;
gs.info("User " + gr.name + " User creation date " + date);
}
var nowTime = new GlideDateTime(date).getDate();
var dateField = new GlideDateTime('2025-03-11 11:11:03').getDate();
var c_date = new GlideDate();
var dur1 = GlideDate.subtract(nowTime, c_date).getDayPart();
if (dur1 == 0) {
gs.info("Start Date of field " + dateField);
gs.info("Creation date trimming time " + nowTime);
var dur = GlideDateTime.subtract(nowTime, dateField);
var days = dur.getDayPart();
gs.info("Real time " + days);
if (days >= -3 && days <= 28) {
return 'yes';
}
}
return 'no';
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-11-2025 01:34 AM
Greetings!!
Here, you need to identify a field that indicates whether a user is new or not. You can either create a new field or flag to capture [ Not recommended to create new field] whether the user logged in before the start date. If not (which is always true), then consider the user as new and run a job accordingly.
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-11-2025 01:47 AM
try this
the isNewUser variable is used to check if the user's sys_created_on date is today. If the user is new, the script proceeds to create the RITM and update the u_new_user_notification_sent field.
This should ensure that RITMs are only created for new users.
var userGR = new GlideRecord('sys_user');
userGR.addEncodedQuery("manager!=915dd5b80f3f52007b3a3e7ce1050e61");
userGR.addQuery('u_new_user_notification_sent', false);
userGR.addNotNullQuery('manager');
userGR.addNotNullQuery('u_start_date');
userGR.query();
while (userGR.next()) {
// Check if the user is new
var isNewUser = (userGR.sys_created_on >= gs.beginningOfToday());
if (isNewUser) {
var cart = new Cart();
var item = cart.addItem('0cbbe496db880c10db945a48dc96193a'); // Catalog form sys_id
cart.setVariable(item, 'name', userGR.name);
cart.setVariable(item, 'support_group', userGR.u_support_group);
cart.setVariable(item, 'requested_for', userGR.sys_id);
cart.setVariable(item, 'location', userGR.location);
cart.setVariable(item, 'division', userGR.u_division);
cart.setVariable(item, 'email', userGR.email);
cart.setVariable(item, 'start_date', userGR.u_start_date);
cart.update();
var rc = cart.placeOrder();
userGR.setValue('u_new_user_notification_sent', true);
userGR.update();
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-11-2025 04:43 AM
Ankur, I found a similar condition in worklfow script. The code is below. Please check, why for existing users, it is creating RITM/REQTASK.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-11-2025 06:24 AM
Did my script worked which you have written in scheduled job?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader