Client script onsubmit validation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2024 11:21 AM
I want to apply the restriction on submitting the record producer.
The record producer should not allow the submission if the submitter has no more than 10 active submission.
Or successfully the closed case in the current calendar.
How can we restrict this submission.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2024 03:09 PM
The reason it is not working is because the client script provided above includes server-side code. You will need to move the server-side code to a script include and then use GlideAjax to call that script include. The onSubmit client script cannot run server-side code.
Your client script will look like this:
function onSubmit() {
var userId = g_user.userID; // Get the current user ID
var ga = new GlideAjax("script_include_name"); // Update to your script include name
ga.addParam("sysparm_name", 'your_function_name'); // Update to function name from the script include
ga.addParam("sysparm_person", userId);
// Use a synchronous call to prevent form submission until the response is received
var response = ga.getXMLWait();
return allowSubmit(response);
}
function allowSubmit(response) {
// Receive response, format, and post
var answer = response.responseXML.documentElement.getAttribute("answer");
return answer === 'true'; // Return true to allow submission, false to prevent it
}
And your script include code will look something like:
var ScriptIncludeName = Class.create();
ScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
your_function_name: function() {
var pos_sys_id = this.getParameter('sysparm_person');
var gr = new GlideRecord('table'); // Add your table
gr.addQuery('sys_id', pos_sys_id); // Add your query criteria
gr.query();
if (gr.next()) {
return 'true'; // This response will allow submit
} else {
return 'false';
}
}
});
I recommend asking ChatGPT for help if you run into issues with the above. Providing it with the code and explaining what you're trying to do (and where -- in ServiceNow) will help more than you'd think!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2024 08:32 PM
Hi @SUser1234 ,
Please try with following code,
function onSubmit() {
var ga = new GlideAjax('SubmissionRestrictionCheck');
ga.addParam('sysparm_name', 'validateSubmission');
ga.addParam('userID', g_user.userID); // Current user's Sys ID
var isAllowed = false;
ga.getXMLAnswer(function(response) {
isAllowed = response.responseXML.documentElement.getAttribute('answer') === 'true';
if (!isAllowed) {
alert('You cannot submit. You either have more than 10 active submissions or no closed cases this year.');
return false; // Prevent form submission
}
g_form.submit(); // Allow submission if validation passed
});
return false; // Prevent default form submission until GlideAjax completes
}
server side script
var SubmissionRestrictionCheck = Class.create();
SubmissionRestrictionCheck.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateSubmission: function() {
var userID = this.getParameter('userID');
var currentYear = new GlideDateTime().getYear();
// Check active submissions (e.g., Interaction or custom table)
var activeSubmissions = new GlideRecord('interaction'); // Replace with the correct table
activeSubmissions.addQuery('opened_by', userID);
activeSubmissions.addQuery('state', '!=', 'closed'); // Ensure only active records
activeSubmissions.query();
var activeCount = 0;
while (activeSubmissions.next()) {
activeCount++;
}
if (activeCount > 10) {
return 'false'; // Restriction if more than 10 active
}
// Check closed cases in the current calendar year
var closedCases = new GlideRecord('interaction'); // Replace with your table
closedCases.addQuery('opened_by', userID);
closedCases.addQuery('state', 'closed');
closedCases.addQuery('sys_created_on', 'ONORAFTER', currentYear + '-01-01');
closedCases.query();
if (!closedCases.hasNext()) {
return 'false'; // No closed cases this year
}
return 'true'; // Validation passed
}
});
Thank you @SUser1234
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 12:38 PM
If didn't worked
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 02:24 PM
@SUser1234 it's increasingly difficult to help you when you just respond with "it didn't work." What did you try? Did you see errors? Where did you see errors? Provide some context if you want help. Please take a look at this article: 10+ Tips for writing a quality community question
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 02:31 PM
Hi
I tried the same script but it failed for checking 10 records added the condition and modified where it needs to be modified.
It is not checking the 10 records validation and not checking the script for close case in current year