Created
October 15, 2017 22:37
-
-
Save jsmithdev/57367ff69d81bf6853da90dcfd8e5329 to your computer and use it in GitHub Desktop.
Example of creating csv from SalesForce Data all via an external site without SF libs (or any aside from jQuery)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="description" content=""> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<meta name="generator" content="Read Example"> | |
<meta name="application-name" content="Read Example"> | |
<title>SmithsIO.com</title> | |
<meta name="apple-mobile-web-app-title" content="Read Example"> | |
<meta name="application-name" content="Read Example"> | |
<link rel="apple-touch-icon" sizes="180x180" href="//i.imgur.com/Fx2rQav.png"> | |
<link rel="mask-icon" href="//i.imgur.com/Fx2rQav.png" color="#5bbad5"> | |
<meta name="apple-mobile-web-app-status-bar-style" content="grey"> | |
<meta name="apple-mobile-web-app-capable" content="yes"> | |
<link href="https://fonts.googleapis.com/css?family=Press+Start+2P" rel="stylesheet"> | |
<link rel="icon" type="image/png" href="//i.imgur.com/Fx2rQav.png" sizes="32x32"> | |
<link rel="icon" type="image/png" href="//i.imgur.com/Fx2rQav.png" sizes="16x16"> | |
<!-- Chrome for Android theme color --> | |
<meta name="theme-color" content="#35c73"> | |
<link rel="manifest" href="manifest.json"> | |
<meta name="msapplication-TileColor" content="#3372DF"> | |
<meta name="msapplication-TileImage" content="//i.imgur.com/Fx2rQav.png"> | |
<!-- Add to homescreen for Chrome on Android --> | |
<meta name="mobile-web-app-capable" content="yes"> | |
<meta name="theme-color" content="#00b2ff"> | |
<!--<link rel="import" href="elements.html">--> | |
<link rel="stylesheet" href="https://smithsio.com/styles/main.css"> | |
<link rel="stylesheet" type="text/css" href="https://unpkg.com/@salesforce-ux/[email protected]/assets/styles/salesforce-lightning-design-system-scoped.min.css" /> | |
<body class="center"> | |
<br /> | |
<img class="foot-icon left" src="//i.imgur.com/Fx2rQav.png" /> | |
<h2>Smith's I/O</h2> | |
<i><a href="//jamiesmiths.com">Jamie Smith's</a> dev area</i> | |
<hr /> | |
<br /> | |
<div class="slds"> | |
<article class="slds-card"> | |
<div class="slds-card__header slds-grid"> | |
<header class="slds-media slds-media_center slds-has-flexi-truncate"> | |
<div class="slds-media__figure"> | |
<span class="slds-icon_container blue" title="description of icon when needed"> | |
<img style="background:#00b2ff" src="https://unpkg.com/@salesforce-ux/[email protected]/assets/icons/standard/account.svg" /> | |
</span> | |
</div> | |
<div class="slds-media__body"> | |
<h2> | |
<a href="javascript:void(0);" class="slds-card__header-link slds-truncate" title="[object Object]"> | |
<span class="slds-text-heading_small"></span> | |
</a> | |
</h2> | |
</div> | |
</header> | |
<div class="slds-no-flex"> | |
<button class="slds-button slds-button_brand" onclick="javascript:jsforce.browser.login();">Login</button> | |
</div> | |
</div> | |
<div class="slds-card__body slds-card__body_inner"> | |
<div class="slds-grid"> | |
<div class="slds-col"> | |
<a id="dlLink" href="" download="">Processing CSV</a> | |
<div id="accounts"> | |
</div> | |
</div> | |
</div> | |
</div> | |
<footer class="slds-card__footer">Written with 💓 by Jamie Smith</footer> | |
</article> | |
</div> | |
</body> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> | |
<script> | |
/*global $*/ | |
const getById = (x) => document.getElementById(x) | |
const accessToken = GetURLParameter('access_token') ? decodeURI(GetURLParameter('access_token')) : '' | |
console.log(accessToken) | |
if(!accessToken){ | |
//The clientId aka Consumer Key and consumerSecret are the ones from the Connected App | |
const clientId = "xxxxxx";// todo change this | |
const consumerSecret = "xxxxx";// todo change this | |
//Authorization endpoint | |
const authEndPoint = "https://login.salesforce.com/services/oauth2/authorize"; | |
//response_type must be set to token | |
const responseType = "token"; | |
//This is the callback URL from the connected app | |
const redirectURI = "https://smithsio.com/YOUR-Page.html"; // todo change this | |
//Construct the URL with the required parameters | |
const requestURL = authEndPoint + '?client_id=' + clientId + '&response_type=' + responseType + '&redirect_uri=' + redirectURI; | |
//redirect the user to the endpoint | |
window.location = requestURL; | |
} | |
else { | |
const query = 'SELECT Id, Name FROM Account' | |
getRecords(query) | |
} | |
function getRecords(rawQuery) { | |
//curl https://***yourInstance***.salesforce.com/services/data/v20.0/query?q=SELECT+name+from+Account -H "Authorization: Bearer access_token" -H "X-PrettyPrint:1" | |
const soql = rawQuery.replace("equals", "=").replace(/\ /g, "+"); | |
console.log('Preforming soql: ' + soql); | |
//replace the below url with your domain name or https://YOURINSTANCE.salesforce.com/services/data/v29.0/query?q= | |
const requestUrl = 'https://jamiesmith-dev-ed.my.salesforce.com/services/data/v29.0/query?q=' + soql; // todo change this | |
console.log('requestUrl: ' + requestUrl); | |
$.ajax({ | |
type: 'GET', | |
url: requestUrl, | |
headers: { //add the authorization header including the access_token | |
'Authorization': 'Bearer ' + accessToken | |
}, | |
dataType: 'json', | |
success: function(response) { | |
console.log('YESSS.'); | |
console.dir(response); | |
const div = getById('accounts') | |
let csv = '' | |
response.records.map(x => csv += String(x.Name).replace('/,/g', '') + ', ' + x.Id + '\n') | |
const filename = 'export.csv'; | |
if (!csv.match(/^data:text\/csv/i)) { | |
csv = 'data:text/csv;charset=utf-8,' + csv; | |
} | |
const data = encodeURI(csv); | |
const link = getById('dlLink') | |
link.innerHTML = 'CSV is Ready, W00T!'; | |
link.setAttribute('href', data); | |
link.setAttribute('download', filename) | |
link.style.fontSize = '3rem' | |
}, | |
error: function(err) { | |
console.log('ERROR'); | |
console.dir(err); | |
} | |
}); | |
} | |
function GetURLParameter(sParam) { | |
const mainURL = document.location + ''; | |
const pageUrls = mainURL.split('#'); | |
if(pageUrls.length > 1){ | |
const sURLVariables = pageUrls[1].split('&'); | |
for (let i = 0; i < sURLVariables.length; i++) { | |
const sParameterName = sURLVariables[i].split('='); | |
if (sParameterName[0] == sParam) { | |
return sParameterName[1]; | |
} | |
} | |
} | |
} | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment