Skip to content

Instantly share code, notes, and snippets.

@jbro-io
Forked from danieljpeter/FindHardCodedURLs.apex
Created October 16, 2016 04:59
Show Gist options
  • Save jbro-io/41f1dac5a77e5bed0485317aa0f3d8b3 to your computer and use it in GitHub Desktop.
Save jbro-io/41f1dac5a77e5bed0485317aa0f3d8b3 to your computer and use it in GitHub Desktop.
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.
* - Highlight the section of the code and click "Execute Hightlighted".
* - View the debug log for the output.
*/
// --- Begin EmailTemplate ---
String sURL = System.URL.getSalesforceBaseURL().toExternalForm().toLowerCase().replace('https://', '');
system.debug('sURL: ' + sURL);
Set<Id> hardCodedSet = new Set<Id>();
for (EmailTemplate et: [SELECT Id, Body, HtmlValue, Subject FROM EmailTemplate]) {
if (et.Body != null && et.Body.containsIgnoreCase(sURL)) {
hardCodedSet.add(et.Id);
continue;
}
if (et.HtmlValue != null && et.HtmlValue.containsIgnoreCase(sURL)) {
hardCodedSet.add(et.Id);
continue;
}
if (et.Subject != null && et.Subject.containsIgnoreCase(sURL)) {
hardCodedSet.add(et.Id);
continue;
}
}
if (hardCodedSet.isEmpty()) {
system.debug('No hard coded URLs found in EmailTemplates');
} else {
system.debug('Hard coded URLs found in these EmailTemplates ' + hardCodedSet);
}
// --- End EmailTemplate ---
// --- Begin WebLink ---
String sURL = System.URL.getSalesforceBaseURL().toExternalForm().toLowerCase().replace('https://', '');
system.debug('sURL: ' + sURL);
Set<Id> hardCodedSet = new Set<Id>();
for (WebLink wl: [SELECT Id, URL FROM WebLink]) {
if (wl.URL != null && wl.URL.containsIgnoreCase(sURL)) {
hardCodedSet.add(wl.Id);
}
}
if (hardCodedSet.isEmpty()) {
system.debug('No hard coded URLs found in WebLinks');
} else {
system.debug('Hard coded URLs found in these WebLinks ' + hardCodedSet);
}
// --- End WebLink ---
// --- Begin ApexClass ---
String sURL = System.URL.getSalesforceBaseURL().toExternalForm().toLowerCase().replace('https://', '');
system.debug('sURL: ' + sURL);
Set<Id> hardCodedSet = new Set<Id>();
for (ApexClass ac: [SELECT Id, Body FROM ApexClass]) {
if (ac.Body != null && ac.Body.containsIgnoreCase(sURL)) {
hardCodedSet.add(ac.Id);
}
}
if (hardCodedSet.isEmpty()) {
system.debug('No hard coded URLs found in ApexClasses');
} else {
system.debug('Hard coded URLs found in these ApexClasses ' + hardCodedSet);
}
// --- End ApexClass ---
// --- Begin ApexPage ---
String sURL = System.URL.getSalesforceBaseURL().toExternalForm().toLowerCase().replace('https://', '');
system.debug('sURL: ' + sURL);
Set<Id> hardCodedSet = new Set<Id>();
for (ApexPage ap: [SELECT Id, Markup FROM ApexPage]) {
if (ap.Markup != null && ap.Markup.containsIgnoreCase(sURL)) {
hardCodedSet.add(ap.Id);
}
}
if (hardCodedSet.isEmpty()) {
system.debug('No hard coded URLs found in ApexPages');
} else {
system.debug('Hard coded URLs found in these ApexPages ' + hardCodedSet);
}
// --- End ApexPage ---
// --- Begin ApexTrigger ---
String sURL = System.URL.getSalesforceBaseURL().toExternalForm().toLowerCase().replace('https://', '');
system.debug('sURL: ' + sURL);
Set<Id> hardCodedSet = new Set<Id>();
for (ApexTrigger at: [SELECT Id, Body FROM ApexTrigger]) {
if (at.Body != null && at.Body.containsIgnoreCase(sURL)) {
hardCodedSet.add(at.Id);
}
}
if (hardCodedSet.isEmpty()) {
system.debug('No hard coded URLs found in ApexTriggers');
} else {
system.debug('Hard coded URLs found in these ApexTriggers ' + hardCodedSet);
}
// --- End ApexTrigger ---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment