Skip to content

Instantly share code, notes, and snippets.

View danieljpeter's full-sized avatar

Daniel Peter danieljpeter

View GitHub Profile
public class ChrisNamespace {
public static final String ns = Namespace;
public static final String nsUU = NamespaceUU;
public static String Namespace {
get {
if (Namespace != null) return Namespace;
ApexClass ac = [SELECT NameSpacePrefix FROM ApexClass WHERE Name = 'ChrisNamespace' limit 1];
Namespace = ac.NameSpacePrefix != null ? ac.NameSpacePrefix : '';
@danieljpeter
danieljpeter / powerBI_import.js
Created July 23, 2017 17:41
Example code of how to upload an excel / pbix file to Power BI embedded
/*
Example code of how to upload an excel / pbix file to Power BI embedded
uses this API here: https://msdn.microsoft.com/en-us/library/mt243840.aspx
*/
var group_id = '<app workspace id>';
var access_token = '<power bi access token>';
@danieljpeter
danieljpeter / FindHardCodedURLs.apex
Last active May 9, 2017 13:00
Execute anonymous script to check Email Templates for Hard Coded URLs before turning on My Domain
/*
* Hard coding your Salesforce URL in places like Email Tempaltes and Apex Classes is a worst practice, but some people do it.
* These URLs will break when you turn on my domain, since your URL changes.
* More on this here: https://help.salesforce.com/apex/HTViewSolution?urlname=Updating-Hard-Coded-References-FAQ
* Here are some quick and dirty checks for your Salesforce URL being hard coded in:
* EmailTemplates, WebLinks, ApexClasses, Visualforce Pages, and Triggers.
* Ideally you would retrieve all metadata and search it, but this will find some of the low hanging problems easily.
*
* Usage:
* - Copy / Paste this into Salesforce Execute Anonymous window.
@danieljpeter
danieljpeter / index.php
Created August 6, 2013 22:07
Use this script to fake a salesforce user login and scrape the api get and data/file storage available, used. Useful for monitoring an org from an external server, but could be adapter for other purposes. made to be served via a web server, rather than CLI.
<?php
//this requires the php toolkit: https://github.com/developerforce/Force.com-Toolkit-for-PHP
require_once ('settings.php');
require_once ('phpToolkit/SforcePartnerClient.php');
require_once ('phpToolkit/SforceHeaderOptions.php');
$storage = checkStorageUsage($USERNAME, $PASSWORD, $ORG_ID);
$api = checkAPI($USERNAME, $PASSWORD, $ORG_ID);
@danieljpeter
danieljpeter / prechat controller - support
Last active July 10, 2017 10:32
This is a visualforce form for Live Agent for salesforce. It is a prechat form that does server-side input validation based on the Contact object. It checks to see if a contact exists for the given email. If it exists, it does nothing. If it doesn't exist, it creates the contact. Then when the customer comes in for chat, we can pop to their cont…
public class SupportPreChat {
public Contact theContact {get; set;}
public String endpoint {get; set;}
public String jsSubmitForm {get; set;}
public SupportPreChat() {
jsSubmitForm = '';
theContact = new Contact();
trigger LeadConvertReparentChatTranscript on Lead (before update) {
Map<Id,Id> convertedLeadsWithContactIds = new Map<Id,Id>();
for (Lead theLead:Trigger.new) {
Lead beforeUpdate = System.Trigger.oldMap.get(theLead.Id);
if ((theLead.ConvertedContactId != null) && (beforeUpdate.ConvertedContactId == null)) {
//if the converted contactId just went from not populated, to populated
//meaning we are converting the lead into a contact
convertedLeadsWithContactIds.put(theLead.Id,theLead.ConvertedContactId);
@danieljpeter
danieljpeter / controller
Created December 12, 2012 03:33
This is a visualforce form for Live Agent for salesforce. It is a prechat form that does server-side input validation based on the Lead object. It creates the lead and forwards the customer on to the chat. From the operator's perspective, they get the CRM chatlet to pop to the Lead. This relies on an Auto Number indexed field called transcriptLe…
public class SalesPreChat {
public Lead theLead {get; set;}
public Lead leadInsert {get; set;}
public String endpoint {get; set;}
public String jsSubmitForm {get; set;}
public SalesPreChat() {
jsSubmitForm = '';
theLead = new Lead();
@danieljpeter
danieljpeter / LeadUpdateOwnerChat
Created December 12, 2012 03:23
This is a trigger and Apex Class to set Lead Owners which are related Live Agent transcripts to the same owner as the transcript. This is because the leads are created as owned by the anon user for the force.com site the prechat form is hosted on. Also a test class for the trigger. See this https://gist.github.com/4264650 for the prechat form an…
public class LeadUpdateOwnerChat {
//the ID of the anon user for the force.com site your prechat form is hosted on
static final Id SALES_SITE_GUEST_USER = '005000000000000';
@future
public static void updateLeadOwner(Map<Id, Id> leadOwnerMap) {
//map coming has the key as the lead id and the value as the new owner id
//build a list of the Leads we need to change the owner on
@danieljpeter
danieljpeter / gist:2590737
Created May 4, 2012 00:45
remove git files
//remove all the files from the local github repo which aren't in the metadata repo (in case these were objects which were deleted in salesforce)
//get the an array of each file listings, recursively. Strip off the beginning path from each entry in the array so we can do text comparison
$arrMetadata = stripBlanks(str_replace('/home/dpeter/sf/metadata', '', getFilesFromDir('/home/dpeter/sf/metadata')));
$arrGit = stripBlanks(str_replace('/home/dpeter/sf/git/salesforce-metadata', '', getFilesFromDir('/home/dpeter/sf/git/salesforce-metadata')));
//remove array entries in the /.git directory
$arrGit = stripGit($arrGit);
$arrDeleteFromGit = array();
@danieljpeter
danieljpeter / gist:2590723
Created May 4, 2012 00:38
php search and replace
function searchReplaceBuildXmlFile($search, $replace) {
// Open file for read and string modification
$file = "/home/dpeter/sf/build.xml";
$fh = fopen($file, 'r');
$contents = fread($fh, filesize($file));
$new_contents = str_replace($search, $replace, $contents);
fclose($fh);
// Open file to write
$fh = fopen($file, 'w');