GlideRecord Help

huyjor
Tera Contributor

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
}

4 REPLIES 4

Mark Roethof
Tera Patron
Tera Patron

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

LinkedIn

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 

Harish KM
Kilo Patron
Kilo Patron

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 

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2...

 

here is the course link to learn script include and glideajax

https://developer.servicenow.com/dev.do#!/learn/learning-plans/utah/new_to_servicenow/app_store_lear...

Regards
Harish

Aman Kumar S
Kilo Patron

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

 

Best Regards
Aman Kumar