How do I validate the variable on the employee center screen?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2024 01:49 AM
Hi guys, I want you to help me whether my script is good enough or not.
Requirement: My catalog item has 4 variables product_model, exam_name, plan, and reason. This catalog is for the user ( requester) to register assets. Users registering assets at 1st will only fill in variables product_model and exam_name. If they register at 2nd, the variables plan and reason will display and users have to fill in these variables. If users register from 3rd and more, do not allow them to submit the request. The condition here is that variableproduct_model has many categories, and variable exam_name has many exams. We have to determine how many times each user has already registered for this model and this examination and then later if they registered 1, we will display the variables plan and reason if they registered 2, we do not allow them to request any more
Script included:
var RegistrationCountAjax = Class.create();
RegistrationCountAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// Retrieve the registration count for a specific user, model, and exam
getRegistrationCount: function() {
var userSysId = this.getParameter('user');
var model = this.getParameter('model');
var exam = this.getParameter('exam');
var count = this._getRegistrationCount(userSysId, model, exam);
return count.toString(); // Return as string for GlideAjax response
},
// Check if the user has already registered twice or more
checkRegistrationCount: function() {
var userSysId = this.getParameter('user');
var model = this.getParameter('model');
var exam = this.getParameter('exam');
var count = this._getRegistrationCount(userSysId, model, exam);
return count.toString(); // Return as string for GlideAjax response
},
// Helper function to get registration count from the Consumable table
_getRegistrationCount: function(userSysId, model, exam) {
var count = 0;
// Initialize a GlideRecord object for the 'cmdb_consumable' table
var gr = new GlideRecord('alm_consumable');
// Add query conditions
gr.addQuery('assigned_to', userSysId); // Query by User ID This is the name of column in the table
gr.addQuery('model', model); // Query by product model . This is the name of column in the table
gr.addQuery('exam', exam); // Query by exam. This is the name of column in the table
// Execute the query
gr.query();
// Count the number of matching records
while (gr.next()) {
count++;
}
return count;
}
});
OnLoad Script:
function onLoad() {
var userSysId = g_user.userID; // Retrieve the user ID
var model = g_form.getValue('product_model'); // The value of (Product Model)
var exam = g_form.getValue('exam_name'); // The value of (Exam Name)
g_form.setVisible('plan', false);
g_form.setVisible('reason', false);
if (model && exam) {
var ga = new GlideAjax('RegistrationCountAjax');
ga.addParam('sysparm_name', 'getRegistrationCount');
ga.addParam('user', userSysId); // Pass the user's Sys ID
ga.addParam('model', model);
ga.addParam('exam', exam);
ga.getXMLAnswer(function(response) {
var count = parseInt(response); // Parse the count from the response
if (count === 1) {
g_form.setVisible('plan', true);
g_form.setVisible('reason', true);
}
});
}
}
On submit
function onSubmit() {
var userSysId = g_user.userID; // Get the current user's Sys ID
var model = g_form.getValue('product_model'); // Get the value of variable (Product Model)
var exam = g_form.getValue('exam_name'); // Get the value of variable (Exam Name)
if (model && exam) {
var ga = new GlideAjax('RegistrationCountAjax');
ga.addParam('sysparm_name', 'checkRegistrationCount');
ga.addParam('user', userSysId);
ga.addParam('model', model);
ga.addParam('exam', exam);
ga.getXMLAnswer(function(response) {
var count = parseInt(response);
if (count >= 2) {
g_form.addErrorMessage('You have already registered twice for this examination.');
g_form.setAbortAction(true); // Prevent submission
}
});
}
}
But these scripts didn't work. Can you help me figure out the issue that I am having? Thank you a lot