When customers start a messaging conversation, you sometimes already know basic information such as their name, email address, and what product they're looking at. You can set up a pre-chat form in the UI with visible fields such as name, email address, and product name. Then use the SDK to populate the pre-chat fields with this information so it appears when the user first opens the pre-chat form.
important: This feature requires version 1.5.0 or later of the Enhanced In-App Chat SDK.
This article applies to the following implementations:
| UI SDK | Core SDK |
|---|---|
| ✅ |
Before you use the SDK, add the visible fields to the pre-chat form. See Customize Pre-Chat for Enhanced Chat in Salesforce Help.
After you add the visible fields to the pre-chat form, you can populate these values by using the SDK.
- To populate known values in the pre-chat fields, implement
preChatFieldValueProvider. - Add your provider to the SDK by using
initin theInterface.swiftfile. - To modify the pre-chat fields, create a
setPreChatValuesfunction. - To make the pre-chat fields editable, set
isEditabletotrue. To make the pre-chat fields non-editable, setisEditabletofalse.
note: If you configure the pre-populated fields to be non-editable, make sure that the pre-chat form fields follow the validation rules. If a required field exceeds the character limit or is set to an invalid data type, the createConversation flow stops and the form isn't submitted. In these cases, the SDK logs error messages in the developer console.
Interface.swift
/**
* A provider to populate known values in the pre-chat fields.
* It requires a function to modify the list of [PreChatField]s, before they're rendered in the pre-chat form.
*/
var preChatFieldValueProvider: (([PreChatField]) async throws -> [PreChatField])
/**
* Add the provider using init in Interface.swift
*/
public init(_ config: UIConfiguration,
preChatFieldValueProvider: ((_ preChatFields: [PreChatField]) async throws -> [PreChatField])? = nil)
// Create an key value pair object that will be pre-populated
let prechatKeyValuePairs = ["_firstName": "Joe", "_subject": "My app is not working"]
// Create a function to modify the pre-chat fields
private func setPreChatValues() -> (([PreChatField]) async throws -> [PreChatField]) { { prechatFields in
prechatFields.enumerated().forEach { (index, value) in
let currenPrechatName = value.name
for prechatKeyValuePair in prechatKeyValuePairs {
if currenPrechatName == prechatKeyValuePair.key {
prechatFields[index].value = // Set the value for the pre-chat fields
prechatFields[index].isEditable = // Set it to true to make the pre-chat fields editable, or to false to make them non-editable
}
}
}
return prechatFields
}
}