> ## Documentation Index
> Fetch the complete documentation index at: https://docs.superoffice.com/llms.txt
> Use this file to discover all available pages before exploring further.

# CRMScript webhooks event handlers

> How to create CRMScript webhook event handlers that respond to SuperOffice events.

Webhooks can also target CRMScripts, not just URLs on the internet. The webhook will call your CRMScript directly, passing the ID and changed values to the script as parameters.

First you need to create a CRMScript to handle the event:

```crmscript theme={null}
#setLanguageLevel 3;
String param1 = getVariable("PrimaryKey");
String param2 = getVariable("Event");
String param3 = getVariable("Changes.name");
print("Params: " + param1 + " " + param2);
print("Name = " + param3 );
```

Save the script with a unique include name, `ScriptHook1`.

![include-name -screenshot][img1]

We then need to create a hook that will call this script:

<Tabs>
  <Tab title="CRMScript API">
    ## Create a webhook using CRMScript

    This CRMScript will create a webhook that when triggered, calls a CRMScript with Include name `ScriptHook1`.

    ```crmscript theme={null}
    #setLanguageLevel 3;

    /*
     * Create a new webhook to subscribe to person events.
     */

    String targetCrmScript = "ScriptHook1";
    String[] events = String("contact.created,contact.changed").split(",");

    NSWebhookAgent webhookAgent;
    NSWebhook webhook = webhookAgent.CreateDefaultWebhook();
    webhook.SetEvents(events);
    webhook.SetName("CRMScript webhook handler");
    webhook.SetType("crmscript");
    webhook.SetTargetUrl(targetCrmScript);
    webhook.SetErrorsEmail("username@domain.com");
    webhook.SetState(1);                      // Set it as active!

    webhook = webhookAgent.SaveWebhook(webhook);

    printLine("Webhook succeeded with ID: " + webhook.GetWebhookId().toString());
    ```
  </Tab>

  <Tab title="REST API">
    ## Create a webhook using the REST API

    This CRMScript will create a webhook that when triggered, calls a CRMScript with Include name `ScriptHook1`.

    This example uses an online OAuth Bearer token for authorization. See the [API Authentication][1] section for alternative options.

    ```json theme={null}
    POST /api/v1/Webhook
    Content-Type: application/json
    Authorization: Bearer {{token}}

    {
      "Name": "CRMScript webhook handler",
      "Events": [
            "contact.created",
            "contact.changed"
        ],
      "Type": "crmscript",
      "TargetUrl": "ScriptHook1",
      "State": "Active"
    }
    ```
  </Tab>
</Tabs>

The webhook must have a name, a list of events to subscribe to and must be set to `crmscript`.

This example above adds a webhook that will call the `ScriptHook1` script whenever a new contact is created or changed. The script is identified using the unique *include name* as the `TargetUrl` value.

Unlike webhooks of type *webhook*, a *CRMScript* webhook gets the values of the changed fields, not just the names of the changed fields as parameters.

Parameters passed to the CRMScript:

* ChangedByAssociateId
* Changes.(fieldname)
* ContextIdentifier
* Entity
* Event
* EventId
* PrimaryKey
* Timestamp
* any properties defined on the webhook

CRMScript hooks are run in the background, without access to the user interface. They do not block the user's action, so they are low-impact from the end-users perspective. They may run some time after the triggering event since webhooks are dispatched on a background thread.

## Get all webhooks with CRMScript

<Note>
  Applications in SuperOffice CRM Online can only see the webhooks they themselves create. Therefore, when you call `GetAllWebhooks` in CRMScript, you will only see the webhooks created by CRMScript in Service.
</Note>

```crmscript theme={null}
#setLanguageLevel 3;

NSWebhookAgent webhookAgent;
NSWebhook[] webhooks = webhookAgent.GetAllWebhooks("","",0);

for(Integer i = 0; i < webhooks.length(); i++) {
  Integer state = webhooks[i].GetState();
  printLine("Name: " + webhooks[i].GetName());
  printLine("ID: " + webhooks[i].GetWebhookId().toString());
  printLine("State: " + state.toString());
  printLine("Secret: " + webhooks[i].GetSecret());
  printLine("TargetUrl: " + webhooks[i].GetTargetUrl());
  printLine("Registered By: " + webhooks[i].GetRegisteredAssociate().GetName());
  printLine("Registered Date: " + webhooks[i].GetRegistered().toString());
  printLine("Updated By: " + webhooks[i].GetUpdatedAssociate().GetName());
  printLine("Updated Date: " + webhooks[i].GetUpdated().toString());
  printLine("Type: " + webhooks[i].GetType());
  printLine("ErrorsEmail: " + webhooks[i].GetErrorsEmail());
  printLine("Events:");
  String[] events = webhooks[i].GetEvents();
  for(Integer i = 0; i < events.length(); i++) {
    printLine("-  " + events[i]);
  }

  // delete when not needed
  //webhookAgent.DeleteWebhook(webhooks[i].GetWebhookId());
  printLine("****************************************************************");
}
```

[1]: ../../../api/authentication/index

[img1]: ../../../../media/loc/en/automation/webhook-include-name.png


## Related topics

- [Namespace CRMScript.Native](/en/automation/crmscript/reference/CRMScript.Native.md)
- [Webhooks](/en/automation/webhook/index.md)
- [SuperOffice Webhooks](/en/automation/webhook/dev/index.md)
- [Execute Event Handlers](/en/api/reference/restful/agent/customerservice_agent/execute-event-handlers.md)
- [Enum EventHandlerType](/en/automation/crmscript/reference/CRMScript.NetServer.EventHandlerType.md)
- [Webhook](/en/api/archive-providers/reference/webhook.md)
