# Salesforce Integration

### Overview

[Salesforce](https://www.salesforce.com/eu/products/) is the world's leading CRM platform, used by sales, support, and operations teams to manage customer relationships, cases, and workflows. By adding a lightweight Apex Class and Apex Trigger to your Salesforce org, you can automatically forward Case alerts to ITOC360 whenever a case is created or updated — and resolve them automatically when the case is closed.

This integration uses Salesforce's native Apex callout mechanism to send a structured JSON payload to the ITOC360 webhook endpoint.

### Provider Configuration & Mapping

ITOC360 uses the `event_type` field in the payload to determine the alert type.

| Payload Value | ITOC360 Type |
| ------------- | ------------ |
| `ALERT`       | ALERT        |
| `RESOLVE`     | RESOLVE      |

Priority is mapped from the `priority` field on the Salesforce Case object:

| Salesforce Case Priority | ITOC360 Priority |
| ------------------------ | ---------------- |
| `Low`                    | LOW              |
| `Medium`                 | MEDIUM           |
| `High`                   | HIGH             |
| `Critical`               | CRITICAL         |

***

### Setup Instructions

#### Step 1: Get Your Webhook URL and Token

1. Log in to your ITOC360 platform.
2. Go to **Sources → Add Source**.
3. Select **Salesforce** as your provider.
4. Save the configuration and copy the generated **Token**.

Your webhook URL will be in the following format:

```
https://api.itoc360.app/functions/v1/events?token=YOUR_TOKEN
```

***

#### Step 2: Add ITOC360 as a Remote Site in Salesforce

Salesforce blocks outbound HTTP calls to external domains by default. You must allowlist the ITOC360 domain before the Apex trigger can send requests.

1. In Salesforce, click the **gear icon** (⚙️) in the top right and select **Setup**.
2. In the Quick Find search box, type **Remote Site Settings** and open it.
3. Click **New Remote Site**.
4. Fill in the fields:
   * **Remote Site Name:** `ITOC360`
   * **Remote Site URL:** `https://api.itoc360.app`
5. Click **Save**.

***

#### Step 3: Create the Apex Class

The Apex Class handles the outbound HTTP callout to ITOC360. It runs asynchronously to avoid blocking Salesforce transactions.

1. Click the **gear icon** and select **Developer Console**.
2. In the Developer Console, go to **File → New → Apex Class**.
3. Name the class `ITOC360Class` and click **OK**.
4. Replace the contents with the following code and click **Save**:

```java
global class ITOC360Class {

    @future(callout=true)
    global static void sendAlert(String endpoint, String payload) {
        HttpRequest req = new HttpRequest();
        req.setEndpoint(endpoint);
        req.setMethod('POST');
        req.setBody(payload);
        req.setHeader('Content-Type', 'application/json');
        req.setHeader('Accept', 'application/json');
        Http http = new Http();
        HTTPResponse res = http.send(req);
        System.debug('ITOC360 Response: ' + res.getStatusCode() + ' ' + res.getBody());
    }

    global static String sanitize(String value) {
        if (value == null) return 'null';
        return '"' + value.replaceAll('[\\\\"]', '') + '"';
    }
}
```

***

#### Step 4: Create the Apex Trigger

The Apex Trigger fires on Case insert and update events. It builds the JSON payload and calls the Apex Class to send it to ITOC360.

1. In the Developer Console, go to **File → New → Apex Trigger**.
2. Name the trigger `ITOC360CaseTrigger`, set the **sObject** to `Case`, and click **Submit**.
3. Replace the contents with the following code.
4. Replace `YOUR_TOKEN` with the token you copied in Step 1, then click **Save**:

```java
trigger ITOC360CaseTrigger on Case (after insert, after update) {

    String token = 'YOUR_TOKEN';
    String endpoint = 'https://api.itoc360.app/functions/v1/events?token=' + token;

    Case obj = Trigger.new[0];

    String eventType = obj.IsClosed ? 'RESOLVE' : 'ALERT';
    String caseNumber = obj.CaseNumber == null ? '' : obj.CaseNumber;
    String subject = obj.Subject == null ? '' : obj.Subject;
    String description = obj.Description == null ? '' : obj.Description;
    String priority = obj.Priority == null ? 'Medium' : obj.Priority;
    String status = obj.Status == null ? '' : obj.Status;
    String accountName = obj.Account == null ? '' : obj.Account.Name;

    String payload = '{' +
        '"event_type": '   + ITOC360Class.sanitize(eventType)   + ',' +
        '"case_number": '  + ITOC360Class.sanitize(caseNumber)  + ',' +
        '"subject": '      + ITOC360Class.sanitize(subject)      + ',' +
        '"description": '  + ITOC360Class.sanitize(description)  + ',' +
        '"priority": '     + ITOC360Class.sanitize(priority)     + ',' +
        '"status": '       + ITOC360Class.sanitize(status)       + ',' +
        '"account_name": ' + ITOC360Class.sanitize(accountName)  +
    '}';

    System.debug('ITOC360 Payload: ' + payload);
    ITOC360Class.sendAlert(endpoint, payload);
}
```

***

#### Step 5: Test the Integration

1. In Salesforce, go to the **Cases** object and create a new Case:
   * **Subject:** Any descriptive title
   * **Priority:** High
   * **Status:** New
2. Save the case.
3. Log in to ITOC360 and go to **Alerts** — the new case should appear as an active alert with the correct priority.
4. To test automatic resolution, open the case in Salesforce and change its **Status** to **Closed**, then save.
5. The corresponding alert in ITOC360 should automatically resolve.

***

### Troubleshooting

**Alerts are not appearing in ITOC360**

* Verify that the Remote Site Settings entry for `https://api.itoc360.app` is saved and active.
* Check the Salesforce Debug Logs: go to **Setup → Environment → Logs → Debug Logs**, add your user as a traced entity, and recreate the case. Look for `ITOC360 Response` in the log output.
* Make sure the token in the trigger matches the one generated in your ITOC360 source.

**Debug Logs are empty**

* In Debug Logs, check that the **Expiration Date** for your traced user is set in the future. Edit and extend it if needed, then recreate the case.
