WikiForce
- abhyash
- Oct 30, 2022
- 3 min read
Updated: Nov 1, 2022
v1 Prepared By: Abhyash Timsina - 30 October 2022
Motivation: Some companies need a way to gather information about their accounts. This solution will do a REST callout and retrieve data from Wikipedia for a provided search string and store the data into a field on Account. Then a VisualForce component formats the raw data and displays it. Note - This is a personal project and not intended for financial gain.
My Technical Components

Flow (with annotation

1. Get Account record based upon recordId passed from flow

2. Main Screen that asks user for the search term. Default value is the Account name

2a. What User Sees

3 – Apex Action – Pass search text from screen to invocable method. Also store Boolean if error occurred and the response raw data from Wikipedia as output.

4. Decision – To check if there was an error returned

5. If no, Updated Wikipedia Information field on Account with the response raw data

6. If yes, throw an error that no results were found

6a – What user sees

7. Confirmation Screen

7a. What User Sees

8. Parsed Data in Visualforce Page

Source Code for WikiForce Apex Class (with annotation starting with //)
/**
* Created by Abhyash Timsina on 30/10/22.
*/
public with sharing class WikiForce {
// Used for input for apex action in flow to search text
public class flowInput {
@InvocableVariable(Required=true)
public String searchText;
}
// Used for output from apex action – returns the raw Wikipedia data and returns true if there an error
public class FlowOutput {
@InvocableVariable public String wikiData;
@InvocableVariable public Boolean error;
public FlowOutput(String wikiData, Boolean error) {
this.wikiData = wikiData;
this.error = error;
}
}
// Invocable method that is visible in the flow
@InvocableMethod(Label='WikiForce' Description='Retrieve Wikipedia Page For Search Term')
public static FlowOutput[] retrieveWiki(flowInput[] requests) {
// Variable to store flow results
FlowOutput[] results = new List<FlowOutput>();
for (flowInput request : requests) {
// Boolean variable to store error true or false. Default is false
Boolean containsError = false;
// Changes any input string’s % to And (required for callout)
String replaceAnd = request.searchText.replaceAll('&', 'And');
// Replaces any spaces with %20 (required for callout)
String finalSearchString = replaceAnd.replaceAll(' ', '%20');
// Create an instance of HttpRequest
HttpRequest req = new HttpRequest();
// Set the REST endpoint to specific url below. Note the search string is added in between the url
req.setEndpoint( 'https://en.wikipedia.org/w/api.php?format=xml&action=query&prop=extracts&titles=' + finalSearchString + '&redirects=true' );
// GET the data from endpoint
req.setMethod('GET');
Http http = new Http();
// Send the request
HttpResponse res = http.send(req);
// Store the body of response into string
String returnedString = res.getBody();
// Check if the returned string contains the missing keyword – Wikipedia specific. This will set the Boolean variable value to true.
if (returnedString.contains('missing=""')) {
containsError = true;
}
// Use another string variable to omit bad data from the returned Wikipedia data
String removeBadData = returnedString.replace('<?xml version="1.0"?><api batchcomplete=""><warnings><extracts xml:space="preserve">HTML may be malformed and/or unbalanced and may omit inline images. Use at your own risk. Known problems are listed at https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:TextExtracts#Caveats.</extracts></warnings><query><pages>', '');
// Strip off all HTML tags so data can be presented nicely
String finalReturnedString = removeBadData.stripHtmlTags();
// Add the final string to flow output variable
results.add(new FlowOutput(finalReturnedString, containsError));
}
// Return the data back into the flow
return results;
}
}
Source Code for displayWikipedia Visualforce Page (with annotation starting with //)
<!--
- Created by abhyashtimsina on 30/10/2022.
-->
// Create apex page that references the standard account controller
<apex:page id="displayWikipedia" standardController="Account" >
// Display Wikipedia Information field from account as formatted html
<apex:outputText value="{!account.Wikipedia_Information__c}" escape="false" />
</apex:page>
Other Notes:
Create a tab on the Account Record page via Lightning App Builder and add the Flow and Visualforce page there. Make sure to pass the recordId to the flow.
If you need help in installing and getting this working – please contact me on LinkedIn.
Comments