{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":616,"path":"api-reference/scripts/c_UsefulSchedulingScripts.md","title":"Scheduling script use cases","bundle":null,"doc_type":null,"release":"australia","body":"\n# Scheduling script use cases\n\nA business rule script specifies the actions that the business rule takes. Scripts commonly include predefined global variables to reference items in your system, such as the current record. Global variables are available to all business rules.\n\n**Warning:** The customization described here was developed for use in specific instances, and is not supported by Now Support. This method is provided as-is and should be tested thoroughly before implementation. Post all questions and comments regarding this customization to our community [forum](http://community.service-now.com/).\n\n## Calculate duration given a schedule\n\nType: Before update/insert business rule.\n\nDescription: A business duration calculates the open to close duration on an incident based on the particular [Creating and using schedules](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-administration/c_UseSchedules.md). If there is no schedule specified, the script will simply use the first schedule returned by the query.\n\nScript example:\n\nThe example below sets the resolved duration when the incident state moves to resolved.\n\n```\nvar gr_rec = new GlideRecord('incident');\ngr_rec.get('ed92e8d173d023002728660c4cf6a7bc');\nif (gr_rec.incident_state == 6) {\nvar dur = calcDurationSchedule(gr_rec.opened_at, gr_rec.sys_updated_on);\n}\n \nfunction calcDurationSchedule(start, end){ \n  // Get the user   \n  var usr = new GlideRecord('sys_user');\n  usr.get(gs.getUserID());\n  // Create schedule - pass in the sys_id of your standard work day schedule and pass in the users timezone\n  var sched = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae',usr.time_zone);\n  // Get duration based on schedule/timezone \n  return(sched.duration(start.getGlideObject(), end.getGlideObject()));\n}\n```\n\n## Check upcoming termination dates\n\nType: Scheduled script.\n\nDescription: This script checks nightly for termination dates on contracts coming up in 90, 50, or 10 days \\(depending on the contract duration field\\).\n\nScript example:\n\n```\nfunction contractNoticeDue(){\n  var now_GR = new GlideRecord(\"contract\");\n  now_GR.addQuery(\"u_contract_status\",\"Active\");\n  now_GR.query();\n  while(now_GR.next()){\n    if((now_GR.u_termination_date<= gs.daysAgo(-90))&&(now_GR.u_contract_duration==\"Long\")){\n      now_GR.u_contract_status=\"In review\";}\n    elseif((now_GR.u_termination_date<= gs.daysAgo(-50))&&(now_GR.u_contract_duration==\"Medium\")){\n      now_GR.u_contract_status=\"In review\";}\n    elseif((now_GR.u_termination_date <= gs.daysAgo(-10))&&(now_GR.u_contract_duration==\"Short\")){\n      now_GR.u_contract_status=\"In review\";}\n    now_GR.update();\n  }\n}\n```\n\nUse scripts in business rules to accomplish common tasks such as:\n\n-   Comparing two date fields.\n-   Parsing XML payloads.\n-   Aborting a database action in a business rule.\n\nWith scripts, you can also:\n\n-   Specify the operation that triggers the business rule.\n-   Use the scratchpad with display business rules to change form values just before a user loads the form.\n-   Use the OR condition like you would in a condition builder.\n\nYou can also utilize the system's scripting functionality available for server-side scripts.\n\nYou can use options on the Business Rules form to build conditions, set field values, and display alert messages without needing to write a script.\n\n**Parent Topic:**[Useful scripts](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/scripts/usefulScripts.md)\n\n"}}