If you haven't, please check out the "part 1, hello world" if necessary.
Get the current console page dynamically:
The open cti apis has a very important function: getPageInfo(). It returns information about the current page as a JSON string. Let's try to use it:
Let's edit the openctiDemo.html in part 1. Make the content look like:
<html>
<head>
</head>
<body>
<input type="button" onclick="javascript:alertPageInfo()" value="PageInfo"/>
</body>
<script type="text/javascript" src="https://MyInstance.salesforce.com/support/api/27.0/interaction.js"></script>
<script type="text/javascript" src="./js/demo.js"></script>
</html>
var alertPageInfo = function () {
sforce.interaction.getPageInfo(getPageInfoCallback);
}
var getPageInfoCallback = function (response) {
if (response.result) {
pageInfo = response.result;
alert(pageInfo);
} else {
alert(response.error);
}
}
Now if we open our console, you have a dummy button. Clicking on it you'll see this:
Get relavant data from salesforce:
Another important/useful function in the open cti apis is runApex(). The function Executes an Apex method from an Apex class that’s exposed in Salesforce. If you are not sure what Apex is, see “Apex Code Overview” in the Salesforce online help.
The syntax of runApex is:
sforce.interaction.runApex(apexClass:string, methodName:string, methodParams:string, (optional) callback:function)
In this example, I have a pre-defined apex class MyUtils, which contains a method called getPhoneNumberByEntityId. This method will return a phone number associated with the record we pass in.
Now let's modify our demo.js file's getPageInfo() callback like:
var getPageInfoCallback = function (response) {
if (response.result) {
pageInfo = response.result;
var entityId = JSON.parse(pageInfo).objectId;
sforce.interaction.runApex('MyUtil', 'getPhoneNumberByEntityId', "entityId=" + entityId, apexCallback);
} else {
alert(response.error);
}
}
Next step is more exciting: we'll add an apexCallback to handle the data from the salesforce system. We'll cover it in the next article.
No comments:
Post a Comment