SCRIPTING BR

 

1. Auto-Create Problem When Incident Reaches Certain Priority

Q: How to automatically create a problem record when incident priority is Critical (1)?

if (current.priority == 1 && current.state == 2) { // In Progress var pr = new GlideRecord('problem'); pr.initialize(); pr.short_description = current.short_description; pr.incident = current.sys_id; pr.insert(); }

Steps:

  • After Update BR on incident

  • Condition: priority = 1 and state = In Progress

  • Script: above

Tables: incident.priority, problem.incident


2. Link Problem to All Related Incidents

Q: How to link multiple incidents to a problem automatically?

var gr = new GlideRecord('incident'); gr.addQuery('category', current.category); gr.addQuery('state', '!=', 7); // Not Closed gr.query(); while(gr.next()){ gr.problem_id = current.sys_id; // current = problem gr.update(); }

Steps:

  • After Insert BR on problem

  • Condition: category-specific

  • Script: above

Tables: incident.problem_id, problem.sys_id


3. Prevent Closing Problem If Linked Incidents Are Open

Q: How to block problem closure if any linked incident is still active?

var gr = new GlideRecord('incident'); gr.addQuery('problem_id', current.sys_id); gr.addQuery('state', '!=', 7); // Not Closed gr.query(); if (gr.hasNext()) { gs.addErrorMessage('Cannot close problem while linked incidents are still open.'); current.setAbortAction(true); }

Steps:

  • Before Update BR on problem

  • Condition: state changes to Closed

  • Script: above

Tables: incident.problem_id, incident.state, problem.state


4. Auto Update Problem Priority Based on Highest Incident Priority

Q: How to adjust problem priority based on linked incidents?

var gr = new GlideRecord('incident'); gr.addQuery('problem_id', current.sys_id); gr.query(); var highest = 5; // Lowest priority while(gr.next()){ if (parseInt(gr.priority) < highest) highest = parseInt(gr.priority); } current.priority = highest;

Steps:

  • Before Update BR on problem

  • Condition: linked incidents exist

  • Script: above

Tables: problem.priority, incident.problem_id, incident.priority


5. Notify Problem Manager When a Critical Incident Links

Q: How to notify problem manager automatically?

if (current.priority == 1 && current.problem_id) { gs.eventQueue('problem.notify_manager', current, current.assigned_to, current.problem_id); }

Steps:

  • After Update BR on incident

  • Condition: priority = 1 and linked to problem

  • Script: above

Tables: incident.problem_id, incident.priority


6. Auto-Reopen Problem If Linked Incident Reopens

Q: Reopen a problem if any linked incident is reopened

if (current.state == 2 && current.problem_id) { // Incident reopened var pr = new GlideRecord('problem'); if (pr.get(current.problem_id)) { pr.state = 2; // In Progress pr.update(); } }

Steps:

  • After Update BR on incident

  • Condition: state changes to Reopened

  • Script: above

Tables: incident.problem_id, problem.state, incident.state


7. Auto Close Problem When All Linked Incidents Are Closed

Q: Automatically close problem if all linked incidents are closed

var gr = new GlideRecord('incident'); gr.addQuery('problem_id', current.sys_id); gr.addQuery('state', '!=', 7); // Not Closed gr.query(); if (!gr.hasNext()) { current.state = 7; // Close Problem }

Steps:

  • Before Update BR on problem

  • Condition: linked incidents

  • Script: above

Tables: problem.state, incident.problem_id, incident.state


8. Auto Assign Problem to Incident Assignment Group

Q: How to assign problem to the same group as the incident?

if (current.incident) { var inc = new GlideRecord('incident'); if (inc.get(current.incident)) { current.assignment_group = inc.assignment_group; } }

Steps:

  • Before Insert BR on problem

  • Script: above

Tables: problem.assignment_group, incident.assignment_group


9. Prevent Incident Deletion If Linked to Problem

Q: How to block deletion of incidents linked to a problem?

if (current.problem_id) { gs.addErrorMessage('Cannot delete incident linked to a problem.'); current.setAbortAction(true); }

Steps:

  • Before Delete BR on incident

  • Script: above

Tables: incident.problem_id


10. Auto Add Incident Notes to Problem Work Notes

Q: Propagate work notes from incident to linked problem

if (current.problem_id && current.work_notes.changes()) { var pr = new GlideRecord('problem'); if (pr.get(current.problem_id)) { pr.work_notes += '\nIncident Notes: ' + current.work_notes; pr.update(); } }

Steps:

  • After Update BR on incident

  • Condition: work_notes changes and linked to problem

  • Script: above

Tables: incident.work_notes, problem.work_notes


==================================================================================

11. Auto-Assign Incident to Logged-In User if Assignment Group Empty

Q: How to auto-assign incident to the current user if no group is selected?

if (!current.assignment_group) { current.assigned_to = gs.getUserID(); }

Steps:

  • Before Insert BR on incident

  • Condition: assignment_group is empty

  • Script: above

Tables: incident.assignment_group, incident.assigned_to


12. Prevent Low Priority Incidents During Business Hours

Q: How to block incidents with priority 5 from being created during office hours?

var hour = gs.nowDateTime().getHourOfDay(); if (current.priority == 5 && hour >= 9 && hour <= 18) { gs.addErrorMessage('Low priority incidents cannot be created during business hours.'); current.setAbortAction(true); }

Steps:

  • Before Insert BR on incident

  • Script: above

  • Checks business hours

Tables: incident.priority


13. Update Parent Change When Child Task is Updated

Q: Propagate child task state to parent change?

var parent = new GlideRecord('change_request'); if (parent.get(current.parent)) { parent.state = current.state; parent.update(); }

Steps:

  • Before Update BR on task

  • Condition: parent exists

  • Script: above

Tables: task.parent, change_request.state


14. Auto-Create Task for Critical Incident

Q: How to auto-generate a task for high-priority incidents?

if (current.priority == 1) { var taskGR = new GlideRecord('task'); taskGR.initialize(); taskGR.short_description = 'Critical Incident Task'; taskGR.incident = current.sys_id; taskGR.insert(); }

Steps:

  • After Insert BR on incident

  • Condition: priority = 1

Tables: task.short_description, task.incident


15. Notify Support Group When Incident is Reopened

Q: How to notify assigned group when incident is reopened?

if (current.state.changes() && current.state == 2) { // Reopened = 2 gs.eventQueue('incident.reopened', current, current.assignment_group); }

Steps:

  • After Update BR

  • Condition: state changes to Reopened

Tables: incident.state, incident.assignment_group


16. Prevent Duplicate Incidents for Same CI

Q: How to block duplicate incidents for same CI?

var gr = new GlideRecord('incident'); gr.addQuery('cmdb_ci', current.cmdb_ci); gr.addQuery('active', true); gr.query(); if (gr.next()) { gs.addErrorMessage('An active incident already exists for this CI.'); current.setAbortAction(true); }

Steps:

  • Before Insert BR on incident

  • Script: above

Tables: incident.cmdb_ci, incident.active


17. Auto Update Impact Based on Priority

Q: How to adjust impact when priority changes?

if (current.priority == 1) current.impact = 1; // High if (current.priority == 2) current.impact = 2; // Medium if (current.priority == 3) current.impact = 3; // Low

Steps:

  • Before Update BR on incident

  • Condition: priority changes

Tables: incident.priority, incident.impact


18. Close Child Tasks When Parent Incident is Closed

Q: Auto-close tasks when parent incident is closed

var gr = new GlideRecord('task'); gr.addQuery('parent', current.sys_id); gr.query(); while(gr.next()){ gr.state = 7; // Closed gr.update(); }

Steps:

  • After Update BR on incident

  • Condition: state changes to Closed

Tables: task.parent, task.state


19. Restrict Incident Assignment to Self Only if VIP User

Q: How to restrict assignment changes for VIP users?

var vipUsers = ['user1_sysid', 'user2_sysid']; if (vipUsers.indexOf(current.caller_id.toString()) != -1 && current.assigned_to != current.caller_id) { gs.addErrorMessage('VIP incidents can only be assigned to the caller.'); current.setAbortAction(true); }

Steps:

  • Before Update BR on incident

  • Script: above

Tables: incident.assigned_to, incident.caller_id


20. Auto Set Incident State to In Progress When Assigned

Q: How to automatically move state to In Progress when assignment is done?

if (current.assigned_to.changes() && current.assigned_to) { current.state = 2; // In Progress }

Steps:

  • Before Update BR on incident

  • Condition: assigned_to changes

Tables: incident.assigned_to, incident.state


=======================================================================


. Auto-assign Incident based on Category

Q: How to automatically assign incidents based on category?

A: Create a before insert business rule on incident table.

if (current.category == 'Network') { current.assignment_group = 'Network Support'; }

Steps:

  1. Navigate to Business Rules > New

  2. Table: incident

  3. When: Before Insert

  4. Condition: category == 'Network'

  5. Script: Add above snippet

Table/Backend Name: incident.assignment_group


2. Prevent Closing Incident if Work Notes are Empty

Q: How to prevent closing if work notes are empty?

A: Before Update BR on incident

if (current.state == 7 && !current.work_notes) { // 7 = Closed gs.addErrorMessage('Please fill work notes before closing.'); current.setAbortAction(true); }

Steps:

  • BR type: Before Update

  • Condition: state changes to Closed

  • Script: above

Table: incident.work_notes


3. Update Related Tasks When Incident Priority Changes

Q: How to propagate priority change to related tasks?

var gr = new GlideRecord('task'); gr.addQuery('parent', current.sys_id); gr.query(); while(gr.next()){ gr.priority = current.priority; gr.update(); }

Steps:

  • Before Update BR on incident

  • Condition: priority changes

  • Script: above

Tables: incident.priority, task.parent


4. Auto Close Resolved Incidents after 7 Days

Q: Automatically close resolved incidents after 7 days

if (current.state == 6) { // Resolved var resolvedDate = current.resolved_at.getGlideObject(); if (resolvedDate.addDays(7) <= gs.nowDateTime()) { current.state = 7; // Closed } }

Steps:

  • Scheduled job or BR Before Update

  • Condition: state=Resolved

Tables: incident.resolved_at, incident.state


5. Prevent Incident Assignment to Inactive Groups

Q: How to avoid assigning incidents to inactive groups?

var grp = new GlideRecord('sys_user_group'); if (grp.get(current.assignment_group) && grp.active != true) { gs.addErrorMessage('Cannot assign to inactive group.'); current.setAbortAction(true); }

Steps:

  • Before Insert/Update BR

  • Script: above

Tables: incident.assignment_group, sys_user_group.active


6. Notify Manager if Priority is High

Q: How to notify manager when priority is high?

if (current.priority == 1) { gs.eventQueue('incident.high_priority', current, current.assigned_to, current.assignment_group); }

Steps:

  • BR: After Insert/Update

  • Condition: priority = 1

  • Event: incident.high_priority

Tables: incident.priority, incident.assigned_to


7. Auto Set SLA Based on Category

if (current.category == 'Hardware') { current.sla = 'SLA_Hardware_24H'; }

Steps:

  • Before Insert BR

  • Table: incident

Tables: incident.sla, incident.category


8. Prevent Incident Creation if CI is Under Maintenance

var ci = new GlideRecord('cmdb_ci'); if (ci.get(current.cmdb_ci) && ci.maintenance_mode == true) { gs.addErrorMessage('Cannot create incident for CI under maintenance.'); current.setAbortAction(true); }

Tables: incident.cmdb_ci, cmdb_ci.maintenance_mode


9. Auto Populate Short Description Based on Category

if (current.category == 'Software') { current.short_description = 'Software Issue reported'; }

Tables: incident.short_description, incident.category


10. Reopen Incident if Work Notes Added After Closure

if (current.state == 7 && current.work_notes.changes()) { current.state = 2; // In Progress }

Tables: incident.state, incident.work_notes



=================================================================================


FLOW DESIGNER BASED


1. Auto-Assign Incident Based on Category

Use case: Automatically assign incidents to a specific group based on the selected category.

  • Table: incident

  • Flow Trigger: Record created or updated (Incident table)

  • Flow Action: Script step

  • Script Example:

// Inputs: current (incident record) (function execute(inputs, outputs) { var category = inputs.current.category.toString(); switch(category) { case 'Network': inputs.current.assignment_group = 'Network Support'; break; case 'Software': inputs.current.assignment_group = 'Application Support'; break; case 'Hardware': inputs.current.assignment_group = 'Hardware Support'; break; default: inputs.current.assignment_group = 'Service Desk'; } })(inputs, outputs);
  • Commonly used: Daily for auto-assignment of tickets.


2. Notify Manager When High Priority Incident is Created

Use case: Send a notification to the manager if a P1/P2 incident is created.

  • Table: incident

  • Flow Trigger: Record created or updated

  • Flow Action: Script step

(function execute(inputs, outputs) { var priority = inputs.current.priority.toString(); if (priority == '1' || priority == '2') { outputs.manager_email = inputs.current.u_manager_email; // custom manager field } })(inputs, outputs);
  • Flow Next Action: Use output manager_email in Send Email action.


3. Update Related Problem When Incident is Resolved

Use case: If all linked incidents to a problem are resolved, automatically close the problem.

  • Tables: incident, problem

  • Flow Trigger: Incident updated

  • Flow Action: Script step

(function execute(inputs, outputs) { var problemSysId = inputs.current.problem_id; if (problemSysId) { var gr = new GlideRecord('incident'); gr.addQuery('problem_id', problemSysId); gr.addQuery('state', '!=', 7); // not Resolved/Closed gr.query(); if (!gr.hasNext()) { var problemGR = new GlideRecord('problem'); if (problemGR.get(problemSysId)) { problemGR.state = 7; // Closed problemGR.update(); } } } })(inputs, outputs);
  • Daily use: Keeps problem management clean.


4. Calculate SLA Breach Date Dynamically

Use case: When incident priority changes, update SLA breach date.

  • Table: incident

  • Flow Trigger: Record updated

  • Flow Action: Script step

(function execute(inputs, outputs) { var priority = inputs.current.priority.toString(); var breachDate = new GlideDateTime(); switch(priority) { case '1': breachDate.addHours(4); // P1 SLA 4 hours break; case '2': breachDate.addHours(8); // P2 SLA 8 hours break; case '3': breachDate.addHours(24); // P3 SLA 24 hours break; default: breachDate.addHours(48); } inputs.current.u_sla_breach_date = breachDate; // custom SLA field })(inputs, outputs);
  • Common: SLA management in ITSM.


5. Auto-Populate Short Description Based on Category

Use case: Auto-fill short_description field to standardize incident logging.

  • Table: incident

  • Flow Trigger: Record created

  • Flow Action: Script step

(function execute(inputs, outputs) { var category = inputs.current.category.toString(); if (!inputs.current.short_description) { inputs.current.short_description = 'Incident logged under ' + category; } })(inputs, outputs);

Key Points About Flow Designer Scripting in ITSM

  1. Tables used most commonly: incident, problem, change_request, task.

  2. Triggers: Record created, Record updated, Record deleted.

  3. Common actions in flows:

    • Send notifications

    • Update fields dynamically

    • Create related tasks (e.g., incident → problem or change request)

    • SLA updates

    • Auto-assignment and routing

  4. Script step: Only use for dynamic or conditional logic not supported by normal Flow Designer actions.

  5. Best practice: Avoid heavy GlideRecord queries in high-volume flows; consider scheduled flows for batch operations.



Comments

Popular posts from this blog

Workflow