# Airkit Lightning Component (Salesforce)

## Overview

Airkit apps can be embedded into Salesforce pages using the Airkit lightning component that can be found on the [Salesforce App Exchange](https://appexchange.salesforce.com/appxListingDetail?listingId=a0N3u00000Pu4EcEAJ). This allows developers to quickly build out and launch personalized web apps and omnichannel experiences on top of Salesforce.

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-dc583e08eb0267607c32fbbbea848920675cbbea%2Fairkit-lightning-component-salesforce-v182-0.png?alt=media)

## Installing the SF Package

## Configuring CSP Trusted Sites

1. Look at the top of any Salesforce page.
   * If you are using Lightning Experience, click on the gear icon, then select Setup Home
   * If you’re using Salesforce Classic and you see Setup in the user interface header, click it.
   * If you’re using Salesforce Classic and you don’t see Setup in the header, click your name, then select Setup.

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-6ae1a93751d9a0d80dd1cdaa624bd72c741b64f8%2Fairkit-lightning-component-salesforce-v182-1.png?alt=media)

2. Navigate to **Security** > **CSP Trusted Sites**
3. Create a **New Trusted Site**

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-773432e4b7b967a27a7f66d9cffc9205d1f05c43%2Fairkit-lightning-component-salesforce-v182-2.png?alt=media)

4. Fill in the information under General Information
   1. Trusted Site URL: This is the base domain of your Airkit application that you would like to pass into the lightning component. To get the base domain, **Edit** your Airkit app and go to **Settings** > **Base Domain**
   2.  For example:

       URL: [https://app.airkit.com/{trigger}](https://app.airkit.com/%7Btrigger%7D)\
       Base domain: [https://app.airkit.com](https://app.airkit.com)

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-9515a990206d1384ba7fbb4f0346e949d5a04aff%2Fairkit-lightning-component-salesforce-v182-3.png?alt=media)

5. Select all of the CSP Directives and click on **Save**

## Configuring Target Hostnames

You will need to configure **Target Hostnames** in your Airkit App in order to allow your app to be embedded into Salesforce.

1. Go to [studio.airkit.com](https://studio.airkit.com) and **Edit** your app and go to **Settings**

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-096c664f55719b2d29f6131e90c82b945c32dc7d%2Fairkit-lightning-component-salesforce-v182-4.png?alt=media)

2. Scroll down until you see **Target Hostnames**

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-ac203e188c77e286eb1de9d81c216e7b7d900dc3%2Fairkit-lightning-component-salesforce-v182-5.png?alt=media)

3. Add the [domain of your Salesforce](https://help.salesforce.com/s/articleView?id=sf.domain_name_overview.htm\&type=5) instance as the Target Hostname

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-e8d961beffbc2d0d7936bb66ee12d640ac3b929c%2Fairkit-lightning-component-salesforce-v182-6.png?alt=media)

4. Save your App

## Setting up Airkit App

The Airkit lightning component takes in a Configuration Url which needs to be set up as a part of your Airkit application. This requires two API endpoints that you will need to configure, an App API and a SDK API.

### Config the SDK API

1. Navigate to Triggers and create a SDK API

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-afd8f72ec3aa9b8297de06785241934d41705801%2Fairkit-lightning-component-salesforce-v182-7.png?alt=media)

2. Create a new URL Route and click on **Create**. You can also use an existing route as well.

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-4771ba687843830a1bd306b1166b70a94a35f7ff%2Fairkit-lightning-component-salesforce-v182-8.png?alt=media)

3. Click on the `sdk` endpoint where you will be able to build out the API

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-7a586d5604ca93c6b9fb18907d88172bb46b9cf2%2Fairkit-lightning-component-salesforce-v182-9.png?alt=media)

4. This is an SDK API which means it has preconfigured headers and properties that allow you to use with Airkit's Web SDK. To get the record's ID and object name when the Airkit lightning component is embedded into Salesforce, the endpoint will need to be modified slightly. Add two **Query Params** called:
   1. `objectName`
   2. `recordId`

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-233e2b666e02e9539848f0607bd0da7b0bd71191%2Fairkit-lightning-component-salesforce-v182-10.png?alt=media)

5. Then, scroll down to the \*\*Start Journey \*\*data operation and under the **Start Parameters** you will need to add the following code block. This will allow you to access the`objectName` and the `recordId` using `session.start.queryParams`.

```javascript
{ "queryParams": payload.queryParams }
```

![](https://files.readme.io/52daab0-image.png)

6. Save your app

### Configure the App API

1. Create an App API using the same URL route created in the previous step and add `config` to the end of the endpoint

![](https://files.readme.io/2a5e07e-image.png)

2. Add a transform data operation and pass in the following. The `SDK Url` is the url endpoint created in the previous step.

![](https://files.readme.io/a40427e-image.png)

```javascript
{
  "appUrl": "<SDK Url>",
  "sdkConfig": {
    "renderMode": "inline"
  }
}


```

3. Configure the response of the API:
   1. Response Headers: `Content-Type`: `application/json`
   2. Response Status : `200`
   3. Response Body: `TO_JSON(transform)`

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-02ac58445a14da351e023f6b6e1e7c5fbb7ee173%2Fairkit-lightning-component-salesforce-v182-11.png?alt=media)

4. Save your app

### Publishing

1. After your App APIs are configured, your app is ready to be embedded and published. In order to see a page render in Salesforce, your app must have a web page with some controls on the page. For this example, the web page will simply say "Hello World" and also pass in the `session` variable on the page.

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-8427caef1379881557ef669be3be77bfd67c4b78%2Fairkit-lightning-component-salesforce-v182-12.png?alt=media)

2. When ready to publish, click on the "Publish" button in the top right hand corner and publish your application. The API endpoint will also be published when you hit publish. This is important to pass the `config` endpoint into the Airkit lightning component in the following steps

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-362dccda594d4fa6b14e653ce60a2c2b645146f6%2Fairkit-lightning-component-salesforce-v182-13.png?alt=media)

## Configuring the Airkit Lightning Component

1. Add the Airkit lightning component to a page in Salesforce. For this example, the component will be added to the Case Object page. The component has the following properties
   1. **configUrl**: The config url of the Airkit app to be embedded
   2. **events**: Comma delimited list of [change events in Salesforce](https://developer.salesforce.com/docs/atlas.en-us.change_data_capture.meta/change_data_capture/cdc_intro.htm) to emit into Airkit
   3. **debug**: When checked, will send events to the console to assist in debugging
   4. **height**: A number that determines the height of the lightning component

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-e1b8fc0005a7ac3bdd75df4943cb3a41bdde80bf%2Fairkit-lightning-component-salesforce-v182-14.png?alt=media)

2. Take the `config` endpoint that was created in [this step](https://docs.airkit.ai/?q=embedding-an-app-in-salesforce#configure-the-app-api) and pass it into the `configUrl` property on the lightning component
3. Pass in a value for the height
4. Save your page

## Configuring Incoming Events from Salesforce

You can set up [data capture events](https://developer.salesforce.com/docs/atlas.en-us.change_data_capture.meta/change_data_capture/cdc_intro.htm) from Salesforce that can emit events and data into Airkit. For example, you can trigger off an action chain in Airkit that updates the UI and sends a text message, whenever a status changes on a Salesforce case to completed and have it pertain to the existing session that is being run on the lightning component.

### Setting up an event when the Case record changes

**Change Data Capture setup in Salesforce**

1. Go to **Setup** > **Integrations** > **Change Data Capture**
2. Select the Case entity

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-ff49add0f677d590de0ed7a25a6497abdd31b5b3%2Fairkit-lightning-component-salesforce-v182-15.png?alt=media)

3. Save your changes

**Configuring the incoming event in Airkit**

1. Navigate to your app in the studio and go to **Connections** > **Embedded Event Sources** and add an **Incoming Event Source**

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-7cf84490b1865bd63d80661a7e250398d25bf43e%2Fairkit-lightning-component-salesforce-v182-16.png?alt=media)

2. Add an `Any` variable and name it `payload`

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-ed49b7460b58da19766324c0e8460c1118996a67%2Fairkit-lightning-component-salesforce-v182-17.png?alt=media)

3. In **Identifiers** > **Key** add `/data/CaseChangeEvent`. This key is dependent on what change event object you select. For standard objects, the key will follow the pattern of `/data/<Standard_Object_Name>ChangeEvent` For more information, see [here](https://developer.salesforce.com/docs/atlas.en-us.240.0.object_reference.meta/object_reference/sforce_api_associated_objects_change_event.htm).

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-c404b90a21c1d126270c9adbc7df1b66f5172dbd%2Fairkit-lightning-component-salesforce-v182-18.png?alt=media)

4. Add an action to the action chain. This action chain will run whenever there is a change on the case record

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-3a0156c77e847471f430b377901589ec6cfde10a%2Fairkit-lightning-component-salesforce-v182-19.png?alt=media)

5. Save and publish your Airkit app

**Adding the event to the Airkit Lightning Component**

1. The last step to tie it all together is to add the key to the events property on the lightning component. Add `/data/CaseChangeEvent` to the events property

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-5c1c58f98beb5eb3eda1ac940846e8778d3360b0%2Fairkit-lightning-component-salesforce-v182-20.png?alt=media)

2. Save your changes

## Configuring Outgoing Events

You can trigger off [platform events](https://developer.salesforce.com/docs/atlas.en-us.platform_events.meta/platform_events/platform_events_intro.htm) from Airkit into Salesforce using Airkit's Outgoing events. This allows you to pass data from Airkit back into Salesforce in real-time.

This example will walk through setting up an event to pass data into a Chatter feed on a case.

### Setting up the Platform Event

1. Go to Salesforce Setup and navigate to **Integrations** > **Platform Events**

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-31fafd86dce4a2d06d9eae632b7eeceaf7460bc1%2Fairkit-lightning-component-salesforce-v182-21.png?alt=media)

2. Create a new Platform event and fill in the Paltform Event Information

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-b92828147117a06b9ffd378764027a8fac7aa556%2Fairkit-lightning-component-salesforce-v182-22.png?alt=media)

3. Save the platform event and you will see the Platform Event Definition Detail. Here you will have an API Name that will be the Identifier Key for the outgoing event in Airkit

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-ce0c1e3c11f501b246e59260989d8e5d68f0d653%2Fairkit-lightning-component-salesforce-v182-23.png?alt=media)

4. Next, add custom fields on the platform event. These will be the fields to pass data into Salesforce. The field types should also map to the types in Airkit as well when creating the variables on the outgoing event

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-49ff0690b6fe1c53391d84ef98542b1178665056%2Fairkit-lightning-component-salesforce-v182-24.png?alt=media)

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-dde152af94526091b924c3053311c87584debfb9%2Fairkit-lightning-component-salesforce-v182-25.png?alt=media)

5. Fill in the Custom field information

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-b64a28b1ec1de570d8967a550a842d75b6c06f70%2Fairkit-lightning-component-salesforce-v182-26.png?alt=media)

6. Save the custom field. Repeat the process for `recordId`
7. Once the Custom Fields are created, you will see the API Name that gets generated from the custom fields. These will be used as a part of the Apex trigger and also the Airkit event

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-7739ba47266052477067b8a673cf519197b3c4ac%2Fairkit-lightning-component-salesforce-v182-27.png?alt=media)

#### Creating the Apex Trigger to write to Chatter

1. Create a new Apex Trigger and paste in the following snippet:

```java
trigger CreateMessage on messageRecieved__e (after insert) {
 List<FeedItem> itemsToSave = new List<FeedItem>();
    for(messageRecieved__e ev: trigger.new) {
        FeedItem item = new FeedItem();
        item.ParentId = ev.recordId__c;
        item.Body = ev.message__c;
        itemsToSave.add(item);
    }
    insert itemsToSave;
}
```

Some specific callouts:

* `messageRecieved__e` is the Platform event API name created.
* `message__c` is the message custom field API name
* `recordId__c` is the record custom field API name

2. Save the Apex Trigger

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-bf2fbf2d9741722ac0d5805ab3529c2dec4987c5%2Fairkit-lightning-component-salesforce-v182-28.png?alt=media)

### Setting up the Outbound Event in Airkit

#### Configuring the config API

1. Navigate to **Triggers** > **App APIs** > **config** endpoint
2. Edit the transform data operation that is currently setting the `appUrl` and `sdkConfig` to include the platform event that was created

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-3a7ae4dc0b64a9f182885b110ea6ef5a80cb5630%2Fairkit-lightning-component-salesforce-v182-29.png?alt=media)

```javascript
{
  "appUrl": "https://app.airkit.com/sdk-sfdc-ismaen/sdk",
  "sdkConfig": { "renderMode": "inline" },
  "events": [ "messageRecieved__e" ]
}
```

#### Configuring the Embedded Outgoing Event

1. Navigate to the studio and go to **Connections** > **Outgoing Events** > **Create new Outgoing event**

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-26a2f66dcf55111969512c2fc55966fa707377af%2Fairkit-lightning-component-salesforce-v182-30.png?alt=media)

2. Click on the event created and add in the Platform Event API name into the **Identifiers** > **Key** property

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-cb9796e5618b2525613d5f1e2a033d503efa2c5d%2Fairkit-lightning-component-salesforce-v182-31.png?alt=media)

3. Then add the variables that map to the custom fields that were created. The names of the variables should correspond to the custom field API Names

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-571389f352276ec248c5f96d8a5db8f340ab03fb%2Fairkit-lightning-component-salesforce-v182-32.png?alt=media)

4. After publishing, you can run this event on any action chain such as a button and pass in values into the inputs to write data from Airkit to the Chatter feed!

![](https://3088276961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC6qMwMGWBgxnN70QsDuH%2Fuploads%2Fgit-blob-55968a8443fdf5e795379d8267e4614045e67d6c%2Fairkit-lightning-component-salesforce-v182-33.png?alt=media)
