Custom scripts
If you require Salesforce objects to be created or updated automatically, go to your field mapping settings. There you can enable automation for each object if needed and choose the expected trigger. It's description can be found here.
However, if you need more complex operations to be performed, you can use the custom scripts feature. It allows you to write your own code that will be executed after the chat ends. You can use it to access all the data available in the chat widget and perform any operations you need using the LiveChat and Salesforce APIs.
New Automation
To find your custom scripts settings, go to your Salesforce for LiveChat settings and choose the Scripts tab.
Click Create New Automation. You can name yout automation script and choose to which connected account it should be assigned by choosing from the Account dropdown.
There you can also choose to enable the script right away if needed. Please always remember to click Save after making any changes. If not, your changes will not apply.
Script Editor
Below the Name and Account fields, you can find the script editor. Here you can write your own script, which will be executed automatically within the Chat Ended trigger. The supported packages are dayjs and axios. The supported language is typescript. If you require some additional packages, let us know!
Please do not change the name of the imported function GetChatResponse as it is required for the integration to work properly. You can use it to access all the data available in the chat widget and perform any operations you need using the LiveChat and Salesforce APIs. Just add your custom code in place of the comment // your code here
.
Example function with a simple axios request:
async function handler({
chat,
forms,
salesforce_token
}: {
chat: GetChatResponse,
forms: SalesorceForms,
salesforce_token: string
}) {
try {
//your code here
//i.e. Create Lead
const {data} = await axios({
method: 'post',
url: `https://XXXXX.salesforce.com/services/data/v52.0/sobjects/Lead`,
headers: {
Authorization: `Bearer ${salesforce_token}`,
'Content-Type': 'application/json',
},
data: JSON.stringify(forms['XXXXX'].Case),
})
console.log(data)
} catch (e) {
console.log("Error occured in automation")
console.log((e as Error).message)
}
}
//AUTO GENERATED TYPES FOR EACH CONFIGURED ACCOUNT
type SalesorceForms = {
"XXXXX": {
Case: {
SuppliedEmail: string
Description: string
Subject: string
ContactId: string
CreatedById: string
}
Lead: {
LastName: string
FirstName: string
Email: string
Description: string
Status: string
OwnerId: string
}
}
}
In the above example, the script creates a new Lead in Salesforce. The data is taken from the form with the key XXXXX
(your Salesforce account) and the object Lead
. The authorization takes place using salesforce_token
so you need to make sure you provide it with the required authentication method and token. The data is sent to the Salesforce API using the axios library. You can call also other external API endpoints if needed to push data even further than Salesforce.
Testing
You can test your script by clicking the Test Automation button. The script will be executed with the data from the chat chosen from the dropdown. You can see the result in the console.
Logs
Once your script is enabled and running, you can track its exececution records in the Logs tab. There you can see the status of each event and the result of the script.
We will be happy to build any complex script scenario for you. Just let us know at suppot@darka.io to schedule a discovery call and receive a quote.