GlideRecord Help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2024 10:32 AM
Hello Team
I am trying to write a script include & client script to validate if the student registers twice or not. If they register twice, they should get a rejection. I found a sample of the script includes and client script. The script looks simple because everything is on one table. But my data structure is different. I have a custom table for registration with the userid field, a custom table on the course with the course id field, and a custom table for enrollment. I'm trying to track on the custom enrollment table to see if the userid and course id are the same. If it's the same, they will get a duplicate message error. Based on the code below, how do you query other tables within the enrollment table?
Thanks for your help.
Script Includes
-----------------------------------------------------------------
var CourseRegistrationUtils = Class.create();
CourseRegistrationUtils.prototype = {
initialize: function() {
},
// Function to check if the user has already registered for the course
isAlreadyRegistered: function(userId, courseId) {
var courseRegistration = new GlideRecord('course_registration');
courseRegistration.addQuery('user', userId);
courseRegistration.addQuery('course', courseId);
courseRegistration.query();
return courseRegistration.hasNext();
},
type: 'CourseRegistrationUtils'
};
-------------------------------------------------------------------------------------
Client Script
var userId = gs.getUserID();
var courseId = g_form.getValue('course'); // Assuming 'course' is the field name for course selection
var courseUtils = new CourseRegistrationUtils();
if (courseUtils.isAlreadyRegistered(userId, courseId)) {
alert('You have already registered for this course.');
g_form.setAbortAction(true); // Abort form submission
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2024 10:46 AM
Hi there,
Why not going for no code Flow?
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2024 09:18 PM
That is a great idea. If I use the flow what would be the trigger? Because I want the user to click Submit button, and then get the error. I don't know if you can incorporate the client script or not into the flow. But that is a great idea to start.
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2024 09:37 PM - edited ‎02-28-2024 09:38 PM
HI @huyjor The code you have shared is a server side script include which cannot be called on client script,
also you have to use GlideAjax call in your client script to retrieve the response from client callable script include.
here is an steps to start with nice documentation from @Shawn Dowler
here is the course link to learn script include and glideajax
Harish

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2024 09:45 PM
Hi @huyjor
I believe you need to go with GlideAjax calls, through which you can access the client callable script include and fetch the required information from database.
Refer to below docs by SN:
https://developer.servicenow.com/dev.do#!/reference/api/utah/client/c_GlideAjaxAPI
https://www.basicoservicenowlearning.in/2019/12/glideajax-in-servicenow.html
Aman Kumar