Overview
This article provides a step-by-step guide for extending your Salesforce CTI integration by leveraging a Twilio Flex Plugin to interact with Salesforce’s OpenCTI framework. By following these instructions, you’ll be able to load OpenCTI within your Flex Plugin, verify that Flex is running inside a Salesforce iFrame, and successfully call OpenCTI methods such as getAppViewInfo (as shown in this example).
Prerequisites
- Salesforce CTI integration is already installed and working in your Salesforce org.
- Twilio Flex is embedded in Salesforce via the OpenCTI utility bar.
- Familiarity with Flex Plugin development. (See Flex Plugin Quickstart for setup.)
Procedure
Step 1: Create Your Flex Plugin
- Use the Flex Plugin CLI to create a new plugin:
twilio flex:plugins:create opencti-sample --install- Navigate to your plugin directory and install dependencies:
cd opencti-sample
npm installStep 2: Detect iFrame Context in Salesforce
To ensure your plugin ONLY loads OpenCTI when Flex is running inside Salesforce, check if the app is running in an iFrame.
In OpenctiSamplePlugin.js, update the content of the async init method as follows:
async init(flex, manager) {
// Condtional statement to check if Flex is running inside an iframe.
if (window.self !== window.top) {
console.log("This web app is embedded in an iframe.");
// window.top results in an error. Can't be accessed from an iFrame
let salesForceURL = 'https://your_SalesForce_URL.lightning.force.com'
let sfdcOriginUrlPram = 'https%3A%2F%2Fyour_SalesForce_URL.lightning.force.com'
loadScript.loadOpenCTI(salesForceURL,sfdcOriginUrlPram)
}
else {
// The web application is running directly in the top-level browsing context.
console.log("This web app is not in an iframe.");
const options = { sortOrder: -1 };
flex.AgentDesktopView.Panel1.Content.add(<CustomTaskList key="OpenctiSamplePlugin-component" />, options);
}Important
-
salesForceURLshould be the base URL of your SFDC -
sfdcOriginUrlPramshould use HTML URL encoding From UTF-8 for special characters. For example https:// should be defined as string, https%3A%2F%2F.
We're calling the loadOpenCTI function and passing above values as parameters. Now we have to create the function and import it 🙂
Step 3: Create loadOpenCTI Function
In src, create a file called LoadScript.js. For this example, we’re creating the function within the same directory.
function loadOpenCTI(sfdcOrigin,sfdcOriginUrlPram){
const execute = (url) => {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.async = true;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
execute(sfdcOrigin+'/support/api/65.0/lightning/opencti_min.js?sfdcIframeOrigin='+sfdcOriginUrlPram+'&mode=Lightning').then(() => {
console.log('External script loaded successfully.');
if ( window.sforce && window.sforce.opencti && typeof window.sforce.opencti.getAppViewInfo === 'function') {
window.sforce.opencti.getAppViewInfo({
callback: function(response) {
console.log('getAppViewInfo response:', response);
}
});
} else {
console.warn('OpenCTI getAppViewInfo is not available.');
}
}).catch((error) => {
console.error('Failed to load external script:', error);
});
});
};
}Step 4: Import the loadOpenCTI Function
In OpenctiSamplePlugin.js make the function we just created accessible by importing LoadScript.js
import {loadScript} from './LoadScript'Step 5: Validate and Test
- Validate your plugin
twilio flex:plugins:start- Deploy your plugin to your Flex instance
twilio flex:plugins:deploy- Open Flex within Salesforce and check the browser console for
getAppViewInforesponses.
If Flex is not running in an iFrame, your custom Flex UI components (e.g., CustomTaskList) will be rendered instead.
Additional Information
Best Practices & Troubleshooting
- Ensure your Salesforce domain is registered as a trusted URL in Flex Console.
- Confirm that third-party cookies are enabled for seamless SSO and OpenCTI communication.
- For more advanced use cases, you can extend this pattern to call other OpenCTI methods or listen for Flex/Salesforce events.