Question:
You need to update the incident
table to set the state
to "Closed" for all incidents that have been resolved for over 30 days. How would you script this?
var gr = new GlideRecord('incident'); gr.addQuery('state', 'Resolved'); // Query all incidents in the "Resolved" state gr.addQuery('resolved_at', '<=', gs.daysAgo(30)); // Resolved more than 30 days ago gr.query(); while (gr.next()) { gr.state = 7; // Setting state to "Closed" gr.update(); // Update each record } gs.info('Successfully closed incidents resolved more than 30 days ago.');
2. The company policy is that incidents marked as "Critical" should not be deleted. How would you prevent the deletion of such incidents using a Business Rule?
Answer
You can create a Before Delete Business Rule to prevent deletion of "Critical" incidents. Here is the script:
if (current.urgency == 1) { // Check if the urgency is "Critical" gs.addErrorMessage('Critical incidents cannot be deleted.'); current.setAbortAction(true); // Abort the deletion process }
- Trigger: Before Delete
- Table:
Incident
- Condition:
current.urgency == 1
This will stop any user from deleting an incident marked as "Critical."
3. You need to write a Script Include that calculates the number of business days (Monday through Friday) between two given dates. How would you implement this?
var BusinessDaysCalculator = Class.create(); BusinessDaysCalculator.prototype = { initialize: function() {}, getBusinessDays: function(startDate, endDate) { var businessDays = 0; var currentDate = new GlideDateTime(startDate); var endDateTime = new GlideDateTime(endDate); while (currentDate.getDate() <= endDateTime.getDate()) { var dayOfWeek = currentDate.getDayOfWeekLocalTime(); if (dayOfWeek != 6 && dayOfWeek != 7) { // Exclude Saturday (6) and Sunday (7) businessDays++; } currentDate.addDaysLocalTime(1); // Move to the next day } return businessDays; }, type: 'BusinessDaysCalculator' };
To call this Script Include from other scripts:
var calc = new BusinessDaysCalculator(); var businessDays = calc.getBusinessDays('2024-09-01', '2024-09-10'); gs.info('Business days: ' + businessDays);
5. You want to prevent users from submitting an incident form if a specific field (e.g., category
) is not filled in. How would you write a Client Script for this?
Answer:
You can use an OnSubmit Client Script to enforce this:
function onSubmit() { var category = g_form.getValue('category'); // Get the value of the 'category' field if (!category) { // If the field is empty alert('Please fill out the Category field before submitting.'); return false; // Prevent form submission } return true; // Allow form submission if category is filled }
- Trigger: OnSubmit
- Table:
Incident
This ensures that the form will not be submitted unless the category
field is filled.
6. You have a catalog item where you want to auto-populate the location
field based on the user's department. How would you achieve this?
Answer:
You can use an OnChange Catalog Client Script for this:
function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue == '') { return; } var userSysId = g_form.getValue('requested_for'); // Get the 'Requested For' field value var ga = new GlideAjax('UserDetails'); // Call the Script Include ga.addParam('sysparm_name', 'getLocationByDepartment'); ga.addParam('sysparm_userSysId', userSysId); ga.getXMLAnswer(function(response) { var location = response; g_form.setValue('location', location); // Set the 'location' field with the returned value }); }
Script Include to fetch the user's location based on their department:
var UserDetails = Class.create(); UserDetails.prototype = { initialize: function() {}, getLocationByDepartment: function(userSysId) { var userGR = new GlideRecord('sys_user'); if (userGR.get(userSysId)) { var department = userGR.department; var locationGR = new GlideRecord('cmn_location'); locationGR.addQuery('department', department); // Get the location based on department locationGR.query(); if (locationGR.next()) { return locationGR.name; // Return the location name } } return ''; }, type: 'UserDetails' };
This setup auto-populates the location
field based on the user's department when the "Requested For" field changes.