Skip to content

Instantly share code, notes, and snippets.

View SalesforceBobLightning's full-sized avatar
💭
Easy-peasy

Salesforce Bob Lightning SalesforceBobLightning

💭
Easy-peasy
View GitHub Profile
@SalesforceBobLightning
SalesforceBobLightning / GetAddressAPI.apex
Last active May 20, 2018 12:44
Using getAddress() / getaddress.io postcode address lookup with Salesforce Apex
public class GetAddressAPI {
// Get Address API: https://getaddress.io/
// Documentation: https://getaddress.io/Documentation
public String getAddresses(String postcode) {
if (postcode == null) {
return null;
}
@SalesforceBobLightning
SalesforceBobLightning / StreetLayerAddressAutoCompleteAPI.apex
Last active May 20, 2018 12:46
Using StreetLayerAPI Address AutoComplete in Salesforce Apex
public class StreetLayerAddressAutoCompleteAPI {
// Street Layer: https://streetlayer.com/
// Documentation: https://streetlayer.com/documentation
public List<String> getAddressAutoComplete(String input, String countryCode) {
string responseData = makeAPICall(input, countryCode);
AutoCompleteResponse response = (AutoCompleteResponse)JSON.deserialize(responseData, AutoCompleteResponse.class);
@SalesforceBobLightning
SalesforceBobLightning / ExampleMethodToCallApexControllerMethod.js
Created May 31, 2018 17:55
Salesforce Lightning JavaScript Method calling Apex Controller Method
exampleJavaScriptMethod : function(cmp, value1, value2) {
var action = cmp.get("c.exampleApexControllerMethod");
action.setParams({'param1': value1, 'param2': value2 });
action.setCallback(this, function(response) {
var state = response.getState();
console.log('exampleJavaScriptMethod state: ' + state);
if(cmp.isValid() && state === "SUCCESS"){
var result = response.getReturnValue();
console.log(result);
// do something
@SalesforceBobLightning
SalesforceBobLightning / SalesforceLightningShowToastHelper.js
Last active May 31, 2018 18:11
Salesforce Lightning Show Toast JavaScript Helper Method
// Documentation: https://developer.salesforce.com/docs/component-library/bundle/force:showToast/specification
// type: 'error', 'warning', 'success', or 'info'
showToast : function(title, message, type, icon) {
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
title : title,
message: message,
duration: '2000',
key: icon,
@SalesforceBobLightning
SalesforceBobLightning / getQueryStringParameterHelper.js
Created May 31, 2018 18:27
Salesforce Lightning Get Query String Parameter JavaScript Helper Method
getQueryStringParameter : function(name) {
var url = decodeURIComponent(window.location.search.substring(1));
var params = url.split('&');
for (var i = 0; i < params.length; i++) {
var p = params[i].split('=');
if (p[0] === name) {
return p[1] === undefined ? true : p[1];
}
@SalesforceBobLightning
SalesforceBobLightning / GoogleMapsGeocodeAPI.apex
Last active May 31, 2018 22:52
Using Google Maps Geocode API with Salesforce Apex
public class GoogleMapsGeocodeApi {
public string geocode(String address){
String url = 'https://maps.googleapis.com/maps/api/geocode/json?address='
+ EncodingUtil.urlEncode(address, 'UTF-8')
+ '&components=country:GB'
+ '&key=' + getGoogleMapsAPIKey();
return getHttp(url);
}
@SalesforceBobLightning
SalesforceBobLightning / makeAjaxRequest.js
Created May 31, 2018 22:57
JavaScript Ajax Request in Salesforce Lightning
callAjax : function(method, url, async, callback) {
var request = new XMLHttpRequest();
request.onreadystatechange = function(component) {
if (request.readyState == 4 ) {
callback.call(this, request);
}
};
request.open(method, url, async);
request.send();
},
@SalesforceBobLightning
SalesforceBobLightning / promiseAction.js
Created July 2, 2018 23:20
Salesforce Lightning JavaScript Helper method that returns a Promise when calling an Apex Controller method
promiseAction : function(cmp, methodName, params){
console.log(methodName);
console.log(params);
return new Promise(function(resolve, reject) {
var action = cmp.get(methodName);
action.setParams(params);
action.setCallback(this, function(response) {
var state = response.getState();
console.log(methodName + ' ' + state);
if(cmp.isValid() && state === "SUCCESS"){
@SalesforceBobLightning
SalesforceBobLightning / castBooleanPropertiesToString.js
Created July 2, 2018 23:21
JavaScript method which converts Boolean properties of an Object to String
castBooleanPropertiesToString : function(object) {
var props = Object.getOwnPropertyNames(object);
for (var i = 0; i < props.length; i++) {
var propName = props[i];
if (typeof object[propName] == 'boolean'){
object[propName] = object[propName].toString();
}
}
},
@SalesforceBobLightning
SalesforceBobLightning / openModel.js
Created July 2, 2018 23:25
Salesforce Lightning JavaScript Helper method for opening a custom modal window
openModel : function(cmp, componentName, params, modelId, header, cssClass, callback) {
var modalBody;
$A.createComponent(componentName, params,
function(content, status) {
if (status === "SUCCESS") {
modalBody = content;
cmp.find(modelId).showCustomModal({
header: header,
body: modalBody,
cssClass: cssClass,