Friday, August 19, 2022

Find Hard coding Salesforce URL in Sandbox-Enhanced domains implemention

 /*

* 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 ---