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-03-2024 08:44 PM
Approach:
You need to create a mechanism to check:
- The count of active submissions by the user (i.e., the number of active records that the user has).
- The success status of cases that were closed in the current calendar year.
If either condition is violated (more than 10 active submissions or no successfully closed cases), the Record Producer should not be submitted.
Here’s how you can achieve this:
Solution : Using a Client Script
You can use a Client Script to intercept the submission and validate the conditions before the record is submitted. A before submit Client Script can help perform the check and prevent submission if the criteria are not met.
Steps to implement:
Create a Client Script:
- Navigate to Record Producer → Find your Record Producer in the list.
- Under the Related Links section, click on New and select Client Script.
- Set the Type to onSubmit (so the script runs when the form is submitted).
Write the Validation Logic: The script should check:
- The number of active submissions (active records) the user has.
- Whether the user has closed successfully completed cases within the current calendar year.
function onSubmit() {
var userId = g_user.userID; // Get the current user ID
// Query for the number of active submissions by the user
var activeSubmissionCount = 0;
var gr = new GlideRecord('your_submission_table'); // Replace with your table for submissions
gr.addQuery('submitter', userId); // Filter by user ID
gr.addQuery('active', true); // Filter for active submissions (you might need to replace with the correct field for active status)
gr.query();
while (gr.next()) {
activeSubmissionCount++;
}
// If user has more than 10 active submissions, prevent submission
if (activeSubmissionCount > 10) {
alert("You cannot submit more than 10 active submissions.");
return false; // Prevent submission
}
// Check if the user has successfully closed cases in the current calendar year
var successClosedCase = false;
var currentYear = new GlideDateTime().getYear(); // Get the current year
var caseGR = new GlideRecord('incident'); // Replace with your table for cases (e.g., 'incident')
caseGR.addQuery('opened_by', userId); // Filter by user ID
caseGR.addQuery('state', 'Closed'); // Filter by closed state
caseGR.addQuery('closed_at', '>=', currentYear + '-01-01'); // Filter for cases closed in the current calendar year
caseGR.query();
if (caseGR.next()) {
successClosedCase = true;
}
// If no closed cases found for the current year, prevent submission
if (!successClosedCase) {
alert("You must have at least one successfully closed case in the current calendar year.");
return false; // Prevent submission
}
return true; // Allow submission
}
✔️ If this solves your issue, please mark it as Correct.
✔️ If you found it helpful, please mark it as Helpful.
—
Shubham Jain
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2024 01:37 PM
It didn't worked
I modified all the lines which needs to be modified
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2024 01:40 PM
It didn't worked
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2024 01:46 PM
It didn't worked