Creating a custom search Engine in Salesforce

Creating a custom search Engine in Salesforce

Hi All,

We can create a custom search engine in salesforce with the help of google. Let’s go in detail to create a custom search engine in salesforce these are the steps needs to follow.

  1. Need to have a google developer Account
  2. Create a custom search engine in Google
  3. Creating a visual Force Page in Salesforce
  4. Integrating Salesforce with Google to fetch the results.

In this blog, I am creating a custom search engine to return the person details based on the email address. Note: If the email address is linked with linkedIn, Facebook or Google Plus.

To create a google developer account visit this link: https://console.developers.google.com

On creation of account then create a project in google and on the creation of project create credentials and then API key will be created. Copy the API Key.

credential-screen

Create a custom search Engine by visiting this link: https://cse.google.com/

custom-serach-engine-creation-page

The sites needs to be added so the new custom search engine will search only from the respective sites. Sites add screenshot is shown below.

site-add-page

Then create a custom search engine and add the sites to search in your created new custom search engine. On the URL you can see the cx=’017xxxxxx’ so copy the cx parameter.

getting-cs

Then Open your salesforce account create a visual Force Page and Controller with the following code.

The Visual Force Page:

start-scree

By entering an email address in the page and then you can see the either facebook, googleplus or facebook link.

search-screen

Visual Force Page:

[sourcecode language=”java”]

<apex:page controller=”searchcontroller”>
<h> Please Search Here </h>
<apex:form>
<apex:inputText value=”{!inputstring}”/>
<apex:commandButton action=”{!submit}” value=”Search” />
<apex:pageBlock rendered=”{!check}”>
<apex:commandLink value=”{!laststrurl}” action=”{!open}” target=”_blank” />

<apex:image url=”{!img}” />
</apex:pageBlock>
</apex:form>
</apex:page>

[/sourcecode]

Controller:

[sourcecode language=”java”]

public with sharing class searchcontroller {

public String inputstring {
get;
set;
}
public string img {
get;
set;
}
public string laststrurl {
get;
set;
}
public boolean check {
get;
set;
}
//Api key is used as keyVal
public string keyVal = ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’;
//cx is used as searchVal
public string searchVal =’xxxxxxxxxxxxxxxxxxxxxxxx’;

public intelligentcontroller() {
check = false;
}

public PageReference submit() {

String strBody;
Http http = new Http();
HttpRequest req = new HttpRequest();

string endurl = ‘https://www.googleapis.com/customsearch/v1?key=’+keyVal +’&amp;cx=’+searchVal +’&amp;q=’ + inputstring;
req.setEndpoint(endurl);
req.setmethod(‘GET’);
try {
HttpResponse res = http.send(req);
strBody= res.getbody();

} catch (exception e) {
system.debug(e);
}

// Below logic is to parse the output

System.debug(strBody);
str.replace(‘n’, ”);
system.debug(‘final’ + strBody);

string substr = strBody.substringBetween(‘”items”: [‘, ‘”cse_image”: [‘);
system.debug(‘substringgggg’ + substr);

if (substr != NULL) {
laststrurl = substr.substringBetween(‘”link”:’, ‘,’);
laststrurl = laststrurl.replace(‘ ‘, ”);
string substrlast = strBody.substringBetween(‘”cse_image”: [‘, ‘]’);
img = substr.substringBetween(‘”src”:’, ‘}’);
img = img.replace(‘ ‘, ”);
img = img.replace(‘”‘, ”);
if (laststrurl != NULL || laststrurl != ”) {
check = true;
} else {
check = false;
}
} else {
substr = strBody.substringBetween(‘”items”: [‘, ‘”pagemap”:’);
if (substr != NULL) {
laststrurl = substr.substringBetween(‘”link”:’, ‘,’);
laststrurl = laststrurl.replace(‘ ‘, ”);
string substrlast = str.substringBetween(‘”cse_image”: [‘, ‘]’);
img = ‘test’;
if (laststrurl != NULL || laststrurl != ”) {
check = true;
} else {
check = false;
}
}
}

return null;
}

public PageReference open() {
PageReference pg = new PageReference(laststrurl);
pg.setRedirect(true);
return pg;
}
}

[/sourcecode]

Leave a Comment

Your email address will not be published. Required fields are marked *

Recent Posts

future-of-ai-powered-business-solutions
Dreamforce 2024: Unveiling the Future of AI-Powered Business Solutions
gitex 2024 worlds largest tech show
GITEX 2024 |  World's Largest Tech Show - ABSYZ
inclusive practices in women leadership equitable future
Inclusive Practices in Women’s Leadership: Equitable Future
salesforce dreamfest 2024 the highlights of dreamforce 2024
Dreamfest 2024: The highlights of Dreamforce 2024
DocuSign CLM the ultimate tool for managing contracts
DocuSign CLM: The Ultimate Tool for Managing Contracts
Document

How can i help you? close button

powered     by   ABSYZ
Scroll to Top