{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":48561,"path":"servicenow-platform/password-reset/pw-reset-ext-script-includes.md","title":"Password Reset extension scripts","bundle":null,"doc_type":null,"release":"australia","body":"\n# Password Reset extension scripts\n\nExtension scripts allow you to extend Password Reset functionality in credential store, verification, or identification types.\n\n## Password Reset extension script includes\n\nEach script include is associated with a specific category, which is available in the appropriate field of a Password Reset form.\n\n**Note:** Create extension scripts only from the Password Reset Extension Script form \\(**Password Reset** &gt; **Extensions** &gt; **New extension script**\\). Extension scripts are special purpose script includes that are not created in the **System Definition** &gt; **Script Includes** interface. To use a script include as an extension script, you must set it to **Active** status.\n\n## 'Enrollment Check' script include category\n\n<table><thead><tr><th>\n\nScript include category\n\n</th><th>\n\nDescription\n\n</th><th>\n\nMethod signature\n\n</th><th>\n\nInput fields\n\n</th><th>\n\nOutput fields\n\n</th></tr></thead><tbody><tr><td>\n\nEnrollment Check\n\n</td><td>\n\nChecks whether a user is enrolled for a given verification.\n\n</td><td>\n\nprocess\\(params\\)\n\n</td><td>\n\nParameters:-   params.userId - The sys\\_id of the user to check \\(table: sys\\_user\\).\n-   params.verificationId - The sys\\_id of the verification to check \\(table: pwd\\_verification\\).\n\n</td><td>\n\nReturns: \\(boolean\\) true, if the user is enrolled in the specified verification; otherwise, false.\n\n</td></tr></tbody>\n</table>This Enrollment Check example signals that the user is enrolled if both expected parameters are supplied. The code would be contained in the **Script** field of an extension script named SampleEnrollmentCheck:\n\n```\nvar SampleEnrollmentCheck = Class.create ();\nSampleEnrollmentCheck. prototype = {\n  category:'password_reset.extension.enrollment_check', // DO NOT REMOVE THIS LINE!\n \n   /**********\n   * Returns boolean telling whether the user is enrolled.\n   * This sample returns true if both parameters are supplied, false otherwise \n   *\n   * @param params.userId         The sys-id of the user being checked (table: sys_user)\n   * @param params.verificationId The sys-id of the verification being checked (table: pwd_verification)\n   * @return Boolean indicating whether the user is enrolled into the specified verification\n   **********/\n  process:function (params) {return (params.userId && params.verificationId)? true:false; },\n \n  type:'SampleEnrollmentCheck'};\n```\n\n## 'Enrollment Form Processor' script include category\n\n<table><thead><tr><th>\n\nScript include category\n\n</th><th>\n\nDescription\n\n</th><th>\n\nMethod signature\n\n</th><th>\n\nInput fields\n\n</th><th>\n\nOutput fields\n\n</th></tr></thead><tbody><tr><td>\n\nEnrollment Form Processor\n\n</td><td>\n\nChecks whether all necessary information has been collected from the user. Stores the information so it can be used for verification when the user resets their password.\n\n</td><td>\n\nprocess\\(params\\)\n\n</td><td>\n\nParameters:-   params.resetRequestId - The sys\\_id of the current Password Reset request \\(table: pwd\\_reset\\_request\\).\n-   params.userId - The sys\\_id of the user to be verified \\(table: sys\\_user\\).\n-   params.verificationId - The sys\\_id of the verification to be processed \\(table: pwd\\_verification\\).\n-   request - The form request object. Fields in the form can be accessed with request.getParameter\\('&lt;element-id&gt;'\\).\n\n The following information should be added to the state of the enrollment process:\n\n-   gs.getSession\\(\\).putProperty\\(\"result.status\",status\\) - Whether the user was successfully enrolled.\n-   gs.getSession\\(\\).putProperty\\(\"result.message\",message\\) - An associated message to be returned to the UI, such as a detailed error message.\n-   gs.getSession\\(\\).putProperty\\(\"result.value\",value\\) - A custom value associated with the enrollment.\n\n</td><td>\n\nReturns: \\(boolean\\) true, if the user is enrolled in the specified verification; otherwise, false.\n\n</td></tr></tbody>\n</table>This example processes an enrollment form submission successfully if the user-submitted response was success. The code would be contained in the **Script** field of an extension script named SampleEnrollmentProcessor:\n\n```\nvar SampleEnrollmentProcessor = Class.create ();\nSampleEnrollmentProcessor.prototype = {\n    category: 'password_reset.extension.enrollment_form_processor', // DO NOT REMOVE THIS LINE!\n \n     /**********\n    * Process the enrollment form request, and return whether the user was successfully enrolled.\n    * \n    * @param params.userId         The sys_id of the user trying to enroll (table: sys_user)\n    * @param params.verificationId The sys_id of the verification to be enrolled into (table: pwd_verification)\n    * @param params.enrollmentId   The sys_id of this enrollment process\n    * @param request               The form request object. Felds in the form can be accessed with\n    *                                           request.getParameter('<element-id>')\n    * @return boolean telling whether the user was successfully enrolled\n    * The following information should be added to the state of the enrollment process\n    *     gs.getSession().putProperty(\"result.status\",status) - whether the user was successfully enrolled\n    *     gs.getSession().putProperty(\"result.message\",message) - an associated message to be returned \n    *             to the UI. Eg. a detailed error message\n    *     gs.getSession().putProperty(\"result.value\",value) - custom value associated with the enrollment\n    **********/\n    processForm:function (params, request) {var verificationId = params.verificationId; var sampleInput = request.getParameter ('sample_input');\n \n\t if (gs.nil (verificationId) || (sampleInput!= 'success')) { return false; }\n \n\t var now_GR = new GlideRecord ('sys_user');\n\tnow_GR.get (params. userId);\n\tnow_GR.print ('User:' + now_GR.getValue ('user_name') + ' successfully enrolled'); return true; },\n    type: 'SampleEnrollmentProcessor' };\n```\n\n## 'Identification Form Processor' script include category\n\n<table><thead><tr><th>\n\nScript include category\n\n</th><th>\n\nDescription\n\n</th><th>\n\nMethod signature\n\n</th><th>\n\nInput fields\n\n</th><th>\n\nOutput fields\n\n</th></tr></thead><tbody><tr><td>\n\nIdentification Form Processor\n\n</td><td>\n\nProcesses an identification form request.\n\n</td><td>\n\nprocessForm\\(params, request\\)\n\n</td><td>\n\nParameters:-   params.processId - The sys\\_id of the calling Password Reset process \\(table: pwd\\_process\\).\n-   request - The form request object. Fields in the form can be accessed with `request.getParameter('<element-id>')`. Use `request.getParameter('sysparm_user_id')` to get the user ID that was entered into the form.\n\n</td><td>\n\nReturns: the sys\\_id of the user that corresponds to the requested input. Returns null, if no user was found.\n\n</td></tr></tbody>\n</table>This example attempts to identify the user within the sys\\_user table given a user name submitted from the identification form. The code would be contained in the **Script** field of an extension script named PwdIdentifyViaUsername:\n\n```\nvar PwdIdentifyViaUsername = Class.create ();\nPwdIdentifyViaUsername. prototype = {\n    category: 'password_reset.extension.identification_form_processor', // DO NOT REMOVE THIS LINE!\n \n    initialize: function () { },\n \n   /**********\n   * Process the identification form request, and returns the user's sys_id. If user was not identified return null.\n   *\n   * @param params.processId   The sys_id of the calling Password Reset process (table: pwd_process)\n   * @param request            The form request object. fields in the form can be accessed with\n   *  request.getParameter('<element-id>')\n   *  Supported request parameters: sysparm_user_id - the user identifier value entered in the form                       \n   * @return The sys_id of the user that corresponds to the requested input; \n   *  if no user was found, null should be returned\n   **********/\n   processForm: function (params, request) {var processId = params. processId; var sysparm_user_id = request.getParameter ('sysparm_user_id');\n      now_GR = new GlideRecord ('sys_user') ;\n      now_GR.addQuery ('user_name', sysparm_user_id) ;\n      now_GR.query (); if (!now_GR.next ()) {return null; } return now_GR.sys_id;},\n \n    type:'PwdIdentifyViaUsername' }\n```\n\n## 'Password Generator' script include category\n\n|Script include category|Description|Method signature|Input fields|Output fields|\n|-----------------------|-----------|----------------|------------|-------------|\n|Password Generator|Returns an auto-generated password.|process\\(params\\)|Parameters:params.processId - The sys\\_id of the calling Password Reset process \\(table: pwd\\_process\\).|Returns: \\(String\\) an auto-generated password.|\n\nThis example randomly generates a password from a base word and numbers. The base word is selected depending on the credential store. The code would be contained in the **Script** field of an extension script named SamplePasswordGenerator:\n\n```\nvar SamplePasswordGenerator = Class.create ();\nSamplePasswordGenerator. prototype = {\n  category: 'password_reset.extension.password_generator', // DO NOT REMOVE THIS LINE!\n \n   /**********\n   * Returns an auto-generated string password.\n   * This sample randomly generates 4 digits to add to the password.\n   * \n   * @param params.credentialStoreId The sys_id of the target Password Reset credential store to generate \n   *  a password for (table: pwd_cred_store)\n   * @return An auto-generated string password\n   **********/\n  process:function (params) { var basePassword ;\n \n\t var now_GR = new GlideRecord ('pwd_cred_store');\n\tnow_GR.addQuery ('name', 'Local ServiceNow Instance');\n\tnow_GR.query (); if (now_GR.next ()) { if (params.credentialStoreId = now_GR.getValue ('sys_id'))\n\t\t\tbasePassword = \"Password\"; else\n\t\t\tbasePassword = \"Dorwssap\"; } return this. generateSimple (basePassword); },\n \n  generateSimple:function (base) {var pwd = base; var numbers = '0123456789'; var length = 4;\n \n     for (var i  = 0 , n  = numbers. length ; i  < length ; i ++) {\n        pwd += numbers.charAt (Math.floor (Math.random () * n) + 1); } return pwd;}, \n \n  type:'SamplePasswordGenerator'} ;\n```\n\n## 'Post Reset' script include category\n\n<table><thead><tr><th>\n\nScript include category\n\n</th><th>\n\nDescription\n\n</th><th>\n\nMethod signature\n\n</th><th>\n\nInput fields\n\n</th><th>\n\nOutput fields\n\n</th></tr></thead><tbody><tr><td>\n\nPost Reset\n\n</td><td>\n\nPerforms additional operations after the completion of the Password Reset process.\n\n</td><td>\n\nprocess\\(params\\)\n\n</td><td>\n\nParameters:-   params.resetRequestId - The sys\\_id of the calling Password Reset process \\(table: pwd\\_process\\).\n-   params.wfSuccess - A flag indicating whether the workflow completed successfully. True if, and only if, successful.\n\n</td><td>\n\nReturns: void\n\n</td></tr></tbody>\n</table>This example adds failed reset requests to the system log. The code would be contained in the **Script** field for an extension script named PwdPostProcessor':\n\n```\nvar PwdPostProcessor = Class.create ();\n \nPwdPostProcessor. prototype = {\n    category: 'password_reset.extension.post_reset_script', // DO NOT REMOVE THIS LINE!\n \n    initialize:function () { },\n \n     /**********\n     * Execute custom actions after the Password Reset process has completed.\n    * \n    * @param params.resetRequestId The sys_id of the current Password Reset request (table: pwd_reset_request)\n    * @param params.wfSuccess      A flag indicating if the workflow completed sucessfully. \n    *  True if (and only if) successful.\n    * @return no return value\n    **********/\n    process: function (params) {if (!params. wfSuccess) {\n           now_GS.log ('[PwdPostProcessor.process] failure post processing for request [' + params. resetRequestId + ']'); }\n \n         // We could place actions here that we always want executed return; },\n \n    type:'PwdPostProcessor' }\n```\n\n## 'User Account Lookup' script include category\n\n|Script include category|Description|Method signature|Input fields|Output fields|\n|-----------------------|-----------|----------------|------------|-------------|\n|User Account Lookup|Gets the credential store account ID for a given user.|process\\(params\\)|Parameters:params.userId - The sys\\_id of the user being checked \\(table: sys\\_user\\).|Returns: \\(String\\) the credential store account ID for the given user.|\n\nThis example gets the credential store account for a user. This code would be contained in the **Script** field of an extension script named SampleUserAccountLookupExtension:\n\n```\nvar SampleUserAccountLookupExtension = Class.create ();\nSampleUserAccountLookupExtension. prototype = {\n  category:'password_reset.extension.user_account_lookup', // DO NOT REMOVE THIS LINE!\n \n   /**********\n  * Returns the credential store account id for a given user.\n  * This sample echoes the user_id supplied as the credential store account id for that user.\n  * \n  * @param params.userId  The sys_id of the user being checked (table: sys_user)\n  * @return               The credential store account id (string) for a given user\n  **********/\n  process:function (params) {return params.userId;},\n \n  type:'SampleUserAccountLookupExtension' }\n```\n\n## 'Verification Form Processor' script include category\n\n<table><thead><tr><th>\n\nScript include category\n\n</th><th>\n\nDescription\n\n</th><th>\n\nMethod signature\n\n</th><th>\n\nInput fields\n\n</th><th>\n\nOutput fields\n\n</th></tr></thead><tbody><tr><td>\n\nVerification Form Processor\n\n</td><td>\n\nProcesses a verification form request and indicates whether the user was verified or not.\n\n</td><td>\n\nprocessForm\\(params, request\\)\n\n</td><td>\n\nParameters:-   params.resetRequestId - The sys\\_id of the current Password Reset request \\(table: pwd\\_reset\\_request\\).\n-   params.userId - The sys\\_id of the user to be verified \\(table: sys\\_user\\).\n-   params.verificationId - The sys\\_id of the verification to be processed \\(table: pwd\\_verification\\).\n-   request - The form request object. Fields in the form can be accessed with request.getParameter\\('&lt;element-id&gt;'\\).\n\n</td><td>\n\nReturns: \\(boolean\\) true, if the user is verified; otherwise, false.\n\n</td></tr></tbody>\n</table>This example shows a verification processor that returns true only if the user sent **ok** in the input field; otherwise, it returns false. The code would be contained in the Script field of an extension script named SampleVerificationFormProcessor:\n\n```\nvar SampleVerificationFormProcessor = Class.create ();\nSampleVerificationFormProcessor.prototype = {\n  category:'password_reset.extension.verification_form_processor', // DO NOT REMOVE THIS LINE!\n \n   /**********\n   * Process the verification form request, and return whether the user was successfully verified.\n   * This is a sample verification processor returns true only if the user sent \"ok\" in the input field; \n   * otherwise, it returns false.\n   * \n   * @param params.resetRequestId The sys_id of the current Password Reset request (table: pwd_reset_request)\n   * @param params.userId         The sys_id of the user trying to be verified (table: sys_user)\n   * @param params.verificationId The sys_id of the verification to be processed (table: pwd_verification)\n   * @param request               The form request object. Fields in the form can be accessed with\n   * request.getParameter('<element-id>')\n   * @return Boolean indicating whether the user is successfully verified\n   **********/\n  processForm:function (params, request) {if (request.getParameter (\"sysparm_simple_input\") == \"ok\") return true; else return false; },\n \n  type:'SampleVerificationFormProcessor'};\n```\n\n**Related topics**  \n\n\n[Extension points](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/web-services/extension-points.md)\n\n"}}