Last active
September 17, 2017 18:39
-
-
Save tet3/a3dcf76eaac9398ee5af483978808d56 to your computer and use it in GitHub Desktop.
Improved version of script to find any HYPERLINK fields with Javascript - debugs a result when no offending fields found. From https://help.salesforce.com/articleView?id=Using-Apex-Code-in-Workbench-to-Find-JavaScript&language=en_US&type=1
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
// Get all the standard and custom objects in the org | |
List<EntityDefinition> entityDefs = [select QualifiedApiName from EntityDefinition]; | |
// Forming a list of Entity API names | |
List<String> entityNames = new List<String>(); | |
for(EntityDefinition entityDef: entityDefs){ | |
if(!entityDef.QualifiedApiName.endsWith('kav')){ | |
entityNames.add(entityDef.QualifiedApiName); | |
} | |
} | |
// Make the describe call | |
Schema.DescribeSobjectResult[] results = Schema.describeSObjects(entityNames); | |
System.debug('Got describe information for ' + results.size() + ' sObjects.'); | |
// Check if there is a formula field with JavaSscript used in the HYPERLINK function | |
Boolean foundField = FALSE; | |
for(Schema.DescribeSobjectResult res : results) { | |
Map<String, SObjectField> fields = res.fields.getMap(); | |
for(String fieldKey: fields.keySet()){ | |
SObjectField sField = fields.get(fieldKey); | |
Schema.DescribeFieldResult fieldResult = sField.getDescribe(); | |
// This will have the formula string for fields of type Formula | |
String calculatedFormulaString = fieldResult.getCalculatedFormula(); | |
// If the formula uses a HYPERLINK function with javascript vbscript or data protocol | |
// it cannot be used for security reasons | |
if(null != calculatedFormulaString | |
&& calculatedFormulaString.startsWithIgnoreCase('HYPERLINK') | |
&& (calculatedFormulaString.containsIgnoreCase('javascript:') | |
|| calculatedFormulaString.containsIgnoreCase('vbscript:') | |
|| calculatedFormulaString.containsIgnoreCase('data:'))){ | |
System.debug('Object name: ' + res.getLabel()); | |
System.debug('Field name: ' + fieldKey); | |
System.debug(calculatedFormulaString); | |
foundField = TRUE; | |
} | |
} | |
} | |
// Debug negative result | |
if (!foundField) { | |
System.debug('No HYPERLINK formula fields using javascript:, vbscript:, or data: found.'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment