{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":3788,"path":"build-workflows/legacy-workflow/r_WorkflowEventSpecificFunctions.md","title":"Workflow event-specific functions","bundle":null,"doc_type":null,"release":"australia","body":"\n# Workflow event-specific functions\n\nThere are several functions that relate specifically to workflow events.\n\n|Function|Description|Purpose|Use|Thread|Source|\n|--------|-----------|-------|---|------|------|\n|registerForEvent \\(eventName\\)|Function in the workflow environment that writes events represented as strings to the `wf_executing.registered_events` field.|The workflow events are just strings. When an activity that has registered for an event executes, a comma delimited set of events is stored with the Workflow Executing Activity \\[wf\\_executing\\] record. When the event is triggered in the workflow context, the wf\\_executing table looks for all executing records that contain the string that represents the event in the `wf_executing.registered_events` field|The global variable workflow that is available to all Workflow Activity \\[wf\\_activity\\] records is the source of the call. For example, from inside a **Run Script** activity, a designer can write: `workflow.registerForEvent('myEventName');`|Current thread, current mutex|Global variable workflow|\n|unRegisterForEvent \\(eventName\\)|Function in the workflow environment that removes a string value representing an event that has been written to the `wf_executing.registered_events` field.|The workflow events are just strings that are written to the `wf_executing.registered_events` field. When an activity unRegisters for an event, the comma delimited set of events stored with the Workflow Executing Activity \\[wf\\_executing\\] record is searched, and if that string is found, it is removed.|The global variable workflow that is available to all Workflow Activity \\[wf\\_activity\\] records is the source of the call. For example, from inside a **Run Script** activity, a designer can write: `workflow.unRegisterForEvent('myEventName');`|Current thread, current mutex|Global variable workflow|\n|fireEvent \\(eventName\\)|Function in the workflow environment that examines the contents of the `wf_executing.registered_events` field, comparing its contents to the `eventName` passed in.|The workflow events are just strings that are written to the `wf_executing.registered_events` field. When fireEvent\\(eventName\\) is called by a workflow activity, the workflow engine queues up any executing records that contain the string in the registered field.|The global variable workflow that is available to all Workflow Activity \\[wf\\_activity\\] records is the source of the call. For example, from inside a **Run Script** activity, a designer can write: `workflow.fireEvent('myEventName');`|Current thread, current mutex|Global variable workflow|\n|fireEvent \\(eventRecord, eventName\\)|Function in the workflow environment that sends an event to a specific Workflow Executing Activity \\[wf\\_executing\\] record. The `eventRecord` is a GlideRecord of the type wf\\_executing.|This event call expects an onMyEvent event handler in the activity represented in the event record \\(Workflow Executing Activity \\[wf\\_executing\\] table\\). When fireEvent\\(eventRecord, eventName\\) is called by a workflow activity, the workflow engine queues up the specific executing record with that event and passes the event into the activity definition for the on&lt;eventName&gt; handler to manage. This event is queued up in its own mutex, so the current queue completes before this event is processed.|The workflow script include contains the call for this. For example, from inside a **Run Script** activity, a designer can write: `var w = new Workflow(); w.fireEvent(executing, eventName);`|Current thread, current mutex|Workflow script include|\n|fireEvent \\(eventRecordSysId, eventName\\)|Function in the workflow environment that sends an event to a specific Workflow Executing Activity \\[wf\\_executing\\] record. The `eventRecordSysId` is the sys\\_id of a GlideRecord of the type wf\\_executing.|This is the same as the fireEvent above, except that it accepts an ID and returns the Workflow Executing Activity \\[wf\\_executing\\] record.|The Workflow script include contains the call for this. For example, from inside a **Run Script** activity, a designer can write: `var w = new Workflow(); w.fireEvent(executing, eventName);`|Current thread, current mutex|Workflow script include|\n|fireEvent \\(eventRecordSysId, eventName, optionalJSONObject\\)|Function in the workflow environment that sends an event to a specific Workflow Executing Activity \\[wf\\_executing\\] record. The `eventRecordSysId` is the sys\\_id of a GlideRecord of the type wf\\_executing.|This is the same as the fireEvent above, except that it accepts a JSON object as a third parameter. This object can specify any data expressible as JSON. You can also specify additional functionality when creating a workflow activity.|The Workflow script include contains the call for this. For example, from inside a **Run Script** activity, a designer can write: `var w = new Workflow(); w.fireEvent(executing, eventName, JSONObject);`|Current thread, current mutex|Workflow script include|\n|broadcastEvent \\(contextId, eventName\\)|Function in the workflow environment that sends an event to all currently running Workflow Executing Activity \\[wf\\_executing\\] records in a specified context, regardless of their state.|This is the same as the fireEvent above, except that it accepts an ID and returns the Workflow Executing Activity \\[wf\\_executing\\] record.|The Workflow script include contains the call for this. For example, from inside a **Run Script** activity, a designer can write: `var w = new Workflow(); w.broadcastEvent(contextId, eventName);`|Current thread, current mutex|Workflow script include|\n|broadcastEvent \\(eventName\\)|Function in the workflow environment that sends an event to all currently running Workflow Executing Activity \\[wf\\_executing\\] records in the current context, regardless of their state.|This should not be confused with broadcastEvent above. This event is only available to current Workflow Executing Activity \\[wf\\_executing\\] records.|This is available only through the global workflow variable of the current context. The following is an example of its use from within an activity definition's script: `workflow.broadcastEvent(eventName)`|Current thread, current mutex|Global variable workflow|\n\n**Parent Topic:**[Workflow events in the base system](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/build-workflows/legacy-workflow/r_WorkflowEventsInTheBaseSystem.md)\n\n"}}