{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":592,"path":"api-reference/scripts/c_ExScptDftBfrQryBsnRu.md","title":"Restricting record access","bundle":null,"doc_type":null,"release":"australia","body":"\n# Restricting record access\n\nYou can use a query business rule that executes before the database query to prevent users from accessing certain records.\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\nConsider the following example from a default business rule that limits access to incident records.\n\n|Name|Table|When|\n|----|-----|----|\n|incident query|Incident|before, query|\n\n## Restricting record access\n\nIn the following example, users are restricted from accessing incident records unless they have the itil role and are listed in the Caller or Opened by field. When self-service users open a list of incidents, they can only see the incidents they submitted.\n\n```\nif (!gs.hasRole(\"itil\")&& gs.isInteractive()) {\n  var u = gs.getUserID();\n  var qc = current.addQuery(\"caller_id\", u).addOrCondition(\"opened_by\", u).addOrCondition(\"watch_list\",\"CONTAINS\", u);\n  gs.print(\"query restricted to user: \" + u);}\n```\n\n**Note:** You can also use access controls to restrict the records that users can see. For information, see [Access Control Lists \\(ACLs\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-security/access-control-rules.md).\n\n## Schedule script for weekdays\n\nType: Business Rules/Client Scripts.\n\nThis script schedules the script for weekdays. Insert any script where it says \"Your Script Here.\"\n\n```\nvar go ='false';\nvar now =new Date(); \n \n// Correct time zone, which is by default GMT -7 \nnow.setHours(now.getHours()+8);\nvar day = now.getDay(); \n \n// No go on Saturday or Sunday \nif(day !=0&& day !=6){\n \n// (your script here)\n \n}\n```\n\n## Set date field according to current date\n\nThis script sets a date field depending on the current day of the week. In this example, if the day is Monday through Wednesday, it sets the date to this coming Monday; otherwise it sets the date field to next Monday.\n\n```\nfunction setCabDate(){\nvar today = new Date();\nvar thisDay = today.getDay();\n\n//returns 0 for Sunday, 1 for Monday, through 6 for Saturday.\nvar thisMon = new GlideDateTime();\nthisMon.setDisplayValue(gs.beginningOfThisWeek());\nvar nextMon = thisMon.getNumericValue();\nnextMon +=(1000*60*60*24*7);\n \nif((thisDay <4)&&(thisDay >0))\n  //if today is Mon thru Wed (thisDay = 1, 2, or 3), set cab to this coming Monday.\n  current.u_req_cab_rev_date.setDateNumericValue(thisMon.getNumericValue());\nelse if((thisDay >=4)||(thisDay ==0))\n  //if today is Thurs thru Sun (thisDay = 4, 5, 6, or 0), set cab to next Monday.\n  current.u_req_cab_rev_date.setDateNumericValue(nextMon);\n}\n```\n\nTo validate the input of all date/time fields, you can use the following in a validation script \\(**System Definition** &gt; **Validation Scripts**\\). Because the date/time format is hard coded in this script, it must match your instance's date/time format. If your instance's date/time format changes, you must update your validation script.\n\nSet the validation script's type to Date/Time. Then, with this validation script, if a user enters an incorrect format in a date/time field, they receive an error message.\n\n```\nfunction validate(value){\n// empty fields are still valid dates \nif(!value) \n    return true; \n \n// We \"should\" have the global date format defined always defined. But there's always that edge case. \nif(typeof g_user_date_time_format !=='undefined')\n    return isDate(value, g_user_date_time_format); \n \n// if we don't have that defined, we can always try guessing \nreturn parseDate(value)!==null;}\n```\n\nFor more information, see [Validation script use case - Date and time](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/scripts/c_ValidateDateAndTime.md).\n\n**Parent Topic:**[Business rule use cases](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/scripts/useful-business-rules.md)\n\n"}}