Create custom actions for a Coveo Insight Panel Classic Component or a custom Box
Create custom actions for a Coveo Insight Panel Classic Component or a custom Box
Salesforce Classic
When creating a Coveo Insight Panel Classic Component, you might want to create custom actions, outside of the default Quick View and Attach to Case functions. You can, for example, add a button to attach a result to an email, insert a result in case feed, or create a Knowledge Base article from a result.
This page outlines the leading practices on how to create a custom action. For explicit tutorials on the previously mentioned use cases, see Examples.
Requirements
-
You’ve installed the Coveo for Salesforce package in your Salesforce integration.
-
You’ve created a Coveo Insight Panel Classic Component or a custom Coveo Box.
Note
While this page only refers to the Coveo Insight Panel Classic Component, the same procedure applies to a Custom Box. |
Step 1: Add code to your custom Coveo Insight Panel Classic Component
The first step you need to do is to add the code to be executed when clicking your button.
-
Access the Visualforce Page of your Coveo Insight Panel Classic Component.
-
In Setup, search for and select Visualforce Pages.
-
Next to your custom Coveo Insight Panel Classic Component, select Edit.
NoteYou’re encouraged to create a custom Coveo Insight Panel Classic Component in which to insert your code (see Create a custom Coveo Insight Panel Classic Component).
-
-
In the Visualforce Markup tab, inside the
apex:page
, add ascript
element. -
Inside the
script
element, add the following code. Modify the sections that you want to fit your needs.// Make sure you use a unique name for each action you implement. Here, it's 'MyCustomAction' var MyCustomAction = function(element, options, bindings, result) { this.type = 'MyCustomAction'; // <------------- Change this if you change the name Coveo.Component.bindComponentToElement(element, this); this.element = element; this.options = options; this.bindings = bindings; this.result = result; }; __extends(MyCustomAction, Coveo.Component); // <------------- Change this if you change the name, as well as on the following line MyCustomAction.prototype.getTitle = function () { // Here you implement your template on how you want the component to appear. var menuDiv = $('<div class="coveo-insert-comment"><div class="coveo-sprites-common-tagging_tag"></div><div class="coveo-caption">Some Text</div></div>'); // <------------- 'Some Text' is the text that you want to be shown var _this = this; menuDiv.click(function() { // // ---------------------------------------------------------------------------- // Your Custom code goes here, you can reference the result using, _this.result // example: console.log(JSON.stringify(_this.result)); // ----------------------------------------------------------------------------- // }); return menuDiv.get(0); }; MyCustomAction.ID = 'MyCustomAction'; // <------------- Change this if you change the name Coveo.CoveoJQuery.registerAutoCreateComponent(MyCustomAction); // <------------- Change this if you change the name
-
After modifying the template and adding your desired code, save the page by clicking Save.
Step 2: Display the action button in the template
Now that you have added the code for your button, you need to change the result template of your panel to display the option.
-
Access the code of your Coveo Insight Panel Classic Component.
-
Access the code through the Visualforce component:
-
In Setup, search for and access the Visualforce Components page.
-
Find the search page you want to change, and under Actions, select Edit.
OR
-
-
Access the code through the Interface Editor:
-
In your Console, access a page that includes the Coveo Insight Panel Classic Component.
-
Inside the panel, select Customize Panel.
-
In the Interface Editor, select the Code View tab at the top right.
-
-
-
Locate the
<div class="CoveoBoxResultAction">
section of the result template you want to add the custom action to. -
Inside this
<div>
, add a new<div>
with a class equal toCoveo
followed by the name of your custom action.ExampleIn the previous step, your action was called
MyCustomAction
. Inside the<div class="CoveoBoxResultAction">
element, you add the following div:<div class="CoveoMyCustomAction"></div>
-
Repeat these steps for every result template that should have present your custom action.
-
Save your modifications by clicking Save. Your custom action is now ready to be used.
Examples
Here are some specific use cases for custom actions. Feel free to adapt and integrate them in your installations.
Action: Send Email
This action creates a Salesforce email and attaches a link to the selected result.
Important
For this action to work, you need to have enabled Email-to-Case (see Enable and Configure Email-to-Case). |
-
When adding script to your Visualforce Page, add the following line before the
script
element.<apex:includeScript value="/canvas/sdk/js/publisher.js" />
-
In your
script
element, add the following code:var MyCustomEmail = function(element, options, bindings, result) { this.type = 'MyCustomEmail'; Coveo.Component.bindComponentToElement(element, this); this.element = element; this.options = options; this.bindings = bindings; this.result = result; }; __extends(MyCustomEmail, Coveo.Component); MyCustomEmail.prototype.getTitle = function () { var menuDiv = $('<div class="coveo-insert-comment"><div class="coveo-sprites-facet-email_sendto"></div><div class="coveo-caption">Email this article</div></div>'); // Change the caption to display here var _this = this; menuDiv.click(function() { // Change the body of the email to display your custom message var body = 'Hi,<br/>'+ 'This article from our Knowledge Base might help you:<br/>'+'<br/><a href="' + _this.result.clickUri.split(' ').join('%20') + '">'+_this.result.title+'</a><br><br>Regards<br>Ramiro Reeves, Product Specialist at BestTech'; Sfdc.canvas.publisher.publish({name: "publisher.selectAction", payload: {actionName:"Case.Email"}}); Sfdc.canvas.publisher.publish({name: "publisher.setActionInputValues", payload:{actionName:"Case.Email", emailFields: {subject: {value: 'This article might help you'}, body: {value:body}}}}); }); return menuDiv.get(0); }; MyCustomEmail.ID = 'MyCustomEmail'; Coveo.CoveoJQuery.registerAutoCreateComponent(MyCustomEmail);
-
When adding your
div
element in yourCoveoBoxResultAction
div of your result template, ensure that you give it theCoveoMyCustomEmail
class, as such:<div class="CoveoBoxResultAction"> <div class="CoveoMyCustomEmail"></div> </div>
Action: Insert into Case Feed
This action inserts a result link into the case feed comments.
-
When adding script to your Visualforce Page, add the following line before the
script
element.<apex:includeScript value="/canvas/sdk/js/publisher.js" />
-
In your
script
element, add the following code:var MyCustomInsertIntoFeed = function(element, options, bindings, result) { this.type = 'MyCustomInsertIntoFeed'; Coveo.Component.bindComponentToElement(element, this); this.element = element; this.options = options; this.bindings = bindings; this.result = result; }; __extends(MyCustomInsertIntoFeed, Coveo.Component); MyCustomInsertIntoFeed.prototype.getTitle = function () { var menuDiv = $('<div class="coveo-insert-comment"><div class="coveo-sprites-common-tagging_tag"></div><div class="coveo-caption">Insert into feed</div></div>'); // Change the caption to display here var _this = this; menuDiv.click(function() { // You can add things to display alongside the result link here var body = _this.result.title + '\n' + _this.result.clickUri.split(' ').join('%20'); Sfdc.canvas.publisher.publish({name: "publisher.selectAction", payload: {actionName:"Case.CaseComment"}}); Sfdc.canvas.publisher.publish({name: "publisher.setActionInputValues", payload:{actionName:"Case.CaseComment", portalPostFields: {body: {value: body}, sendEmail: {value:false}}}}); Sfdc.canvas.publisher.publish({name: 'publisher.refresh', payload : {feed: {value: true}}}); }); return menuDiv.get(0); }; MyCustomInsertIntoFeed.ID = 'MyCustomInsertIntoFeed'; Coveo.CoveoJQuery.registerAutoCreateComponent(MyCustomInsertIntoFeed);
-
When adding your
div
element in yourCoveoBoxResultAction
div of your result template, ensure that you give it theCoveoMyCustomInsertIntoFeed
class, as such:<div class="CoveoBoxResultAction"> <div class="CoveoMyCustomInsertIntoFeed"></div> </div>
Action: Create a Salesforce KB article
A new Create Article button is now available out of the box. To learn how to implement it in your Salesforce integration, see Adding a "Create Article" Button to the Coveo Insight Panel Classic Component.
This action creates a Salesforce KB article from a result.
-
When adding script to your Visualforce Page, add the following line before the
script
element.<apex:includeScript value="/soap/ajax/39.0/connection.js"/>
-
In your
script
element, add the following code:sforce.connection.sessionId='{!GETSESSIONID()}'; // This needs to be added to create objects since Spring '16 var MyCustomCreateKB = function(element, options, bindings, result) { this.type = 'MyCustomCreateKB'; Coveo.Component.bindComponentToElement(element, this); this.element = element; this.options = options; this.bindings = bindings; this.result = result; }; __extends(MyCustomCreateKB, Coveo.Component); MyCustomCreateKB.prototype.getTitle = function () { var menuDiv = $('<div class="coveo-insert-comment"><div class="coveo-sprites-common-copy"></div><div class="coveo-caption">Create KB from result</div></div>'); // Change the caption to display here var _this = this; menuDiv.click(function() { var kb = new sforce.SObject('Troubleshooting__kav'); // Change 'Troubleshooting' to your KB type kb.Summary = '{!JSENCODE(case.Description)}'; kb.Title = '{!JSENCODE(case.Subject)}'; kb.URLName = '{!JSENCODE(case.Subject)}'.replace(/\s+/g, '-').replace(/[^a-zA-Z-0-9\-]/g, '') +'-'+ (Math.random() * 100000 + '').replace('.', '-'); var createResult = sforce.connection.create([kb]); if (createResult[0].getBoolean("success")) { openSubTab('/' + createResult[0].id, kb.Title); menuDiv.addClass('hidden') } else { console.log('Failed creating custom KB',createResult); } }); return menuDiv.get(0); }; MyCustomCreateKB.ID = 'MyCustomCreateKB'; Coveo.CoveoJQuery.registerAutoCreateComponent(MyCustomCreateKB); function openSubTab(url, title) { sforce.console.getEnclosingPrimaryTabId(function (result) { sforce.console.openSubtab(result.id, url, true, title); }); }
-
When adding your
div
element in yourCoveoBoxResultAction
div of your result template, ensure that you give it theCoveoMyCustomCreateKB
class, as such:<div class="CoveoBoxResultAction"> <div class="CoveoMyCustomCreateKB"></div> </div>