{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":49545,"path":"servicenow-platform/workflow-activities/r_LockActivity.md","title":"Lock workflow activity","bundle":null,"doc_type":null,"release":"australia","body":"\n# Lock workflow activity\n\nThe **Lock** activity prevents other instances of this workflow from continuing past this activity until the lock is released.\n\nSeveral instances of the same workflow may run concurrently. For example, if a workflow triggers when a record is added to a particular table. That workflow triggers multiple times if multiple records are added one after the other, once by each record insertion. You can use the lock activity to ensure that this instance of the workflow has finished one or more activities before any other instance of the workflow can proceed.\n\nA workflow can explicitly release a lock with the **Unlock** activity. The lock may also be released when the **Max duration** is reached.\n\nWhen an instance of the workflow reaches the **Lock** activity, it attempts to obtain a lock using the key specified in the lock activity. If another instance has already obtained the lock and has not yet released it, this lock attempt fails. The instance continues trying to obtain the lock until **Max attempts** has been reached.\n\n**Note:**\n\nWe recommend placing a one-second timer activity before the lock activity. This helps prevent a rare condition in which the lock activity may not be able to distinguish one workflow instance from another. This condition can occur because the entity owning the lock is not the specific workflow instance, but rather the code-execution thread in which that instance is running. In most cases, each workflow instance runs on a different thread. Adding a timer activity ensures that this is the case.\n\n\\[Omitted image \"WF\\_lock\\_with\\_timer\\_before.png\"\\] Alt text: Workflow with timer activity before lock activity\n\nSince a **Lock** activity can only temporarily prevent processing of other workflow instances, do not add activities that cause the workflow to wait between a **Lock** and **Unlock** activity block. This may cause the **Unlock** activity to be unable to acquire the lock to release it and instead take 60 seconds to complete. Restricted wait activities include:\n\n-   Approval activities\n-   Task activities\n-   Timer activities\n-   Wait for condition activity\n-   Wait for WF Event activity\n-   MID server activities such as a PowerShell script\n\n## Results\n\n|Result|Description|\n|------|-----------|\n|Success|The activity successfully obtained a lock. This instance of the workflow can proceed past this point, but other instances cannot proceed until the lock is released.|\n|Failure|After attempting to obtain the lock **Max attempts** times, the activity could not obtain the lock.|\n\n## Input variables\n\nInput variables determine the initial behavior of the activity.\n\n<table id=\"table_dfz_zwv_yr\"><thead><tr><th>\n\nField\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nKey\n\n</td><td>\n\nA unique mutex key. The [Unlock activity]() activity uses this key to release the lock.\n\n</td></tr><tr><td class=\"subhead\" colspan=\"2\">\n\nDuration\n\n</td></tr><tr><td>\n\nMax. duration\n\n</td><td>\n\nThe maximum time the lock persists. The lock is released after it reaches this duration. Releasing a lock this way is equivalent to running the **Unlock** activity.\n\n</td></tr><tr><td class=\"subhead\" colspan=\"2\">\n\nLock attemptsSpecify how the activity behaves if the lock attempt is denied. If the final lock attempt fails, the activity state is set to 'timeout' and the activity result is set to 'failed'.\n\n</td></tr><tr><td>\n\nMax. attempts\n\n</td><td>\n\nSpecify the maximum number of times the activity may attempt to obtain the lock.\n\n</td></tr><tr><td>\n\nDelay between attempts\n\n</td><td>\n\nThe amount of time required after a failed lock attempt before another lock attempt is allowed.\n\n</td></tr></tbody>\n</table>## States\n\nThe activity state tells the workflow engine what to do with the activity.\n\n|State|Description|\n|-----|-----------|\n|Waiting|The workflow engine is waiting to obtain a lock.|\n|Finished|The activity successfully obtained the lock.|\n|Timeout|The activity could not obtain a lock within the number of attempts specified by the **Max. attempts** input variable.|\n\n"}}