Find Fields in Use by DE

Simon Maskell Updated by Simon Maskell

Below is an example Visualforce page and Apex controller that can be used to search for fields that are in use by Distribution Engine configuration. This will not be an exhaustive list of places where a field could be referenced, and does not show what specific configuration option is using it, but contains the name of objects where Salesforce fields are selected within DE configuration.

Controller

public with sharing class FieldsInUseByDEController {

public String fieldName {get; set;}
public String objectName {get; set;}

public List<ObjectWrapper> DEObjectsUsingField {get; private set;}

public PageReference search() {
DEObjectsUsingField = new List<ObjectWrapper>();
if (String.isBlank(fieldName) || String.isBlank(objectName)) {
return null;
}

String escapedFieldName = String.escapeSingleQuotes(fieldName);
String escapedObjectName = String.escapeSingleQuotes(objectName);

String soslString = 'FIND {' + escapedFieldName + '} IN ALL FIELDS RETURNING';
soslString += ' n2de__Distributor_Tag__c(n2de__Distributor__c, n2de__Distributor__r.Name WHERE n2de__Distributor__r.n2de__Team__r.n2de__Object_name__c = \'' + escapedObjectName + '\')';
soslString += ', n2de__Distributor__c(Id, Name WHERE n2de__Team__r.n2de__Object_name__c = \'' + escapedObjectName + '\')';
soslString += ', n2de__Team__c(Id, Name WHERE n2de__Object_name__c = \'' + escapedObjectName + '\')';
soslString += ', n2de__Distribution_team_alert__c(n2de__Team__c, n2de__Team__r.Name WHERE n2de__Team__r.n2de__Object_name__c = \'' + escapedObjectName + '\')';
soslString += ', n2de__cls_Field__c(n2de__Classifier_object__c, n2de__Classifier_object__r.Name WHERE n2de__Classifier_object__r.Name = \'' + escapedObjectName + '\')';
soslString += ', n2de__cls_Object__c(Id, Name WHERE Name = \'' + escapedObjectName + '\')';

List<List<SObject>> deObjectsReferencingField;
try {
deObjectsReferencingField = Search.query(soslString);
} catch (QueryException qe) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Failed to query field usage: ' + qe.getMessage()));
return null;
}

Map<Id, ObjectWrapper> deObjsUsingFieldMap = new Map<Id, ObjectWrapper>();

//Dynamic tag filters
List<n2de__Distributor_Tag__c> dynamicTags = (List<n2de__Distributor_Tag__c>) deObjectsReferencingField.get(0);
for (n2de__Distributor_Tag__c tag : dynamicTags) {
if (!deObjsUsingFieldMap.containsKey(tag.n2de__Distributor__c)) {
ObjectWrapper dist = new ObjectWrapper(tag.n2de__Distributor__c, tag.n2de__Distributor__r.Name, 'Distributor');
deObjsUsingFieldMap.put(tag.n2de__Distributor__c, dist);
}
}
List<n2de__Distributor__c> distributors = (List<n2de__Distributor__c>) deObjectsReferencingField.get(1);
for (n2de__Distributor__c dist : distributors) {
if (!deObjsUsingFieldMap.containsKey(dist.Id)) {
ObjectWrapper distwrapper = new ObjectWrapper(dist.Id, dist.Name, 'Distributor');
deObjsUsingFieldMap.put(dist.Id, distwrapper);
}
}

List<n2de__Team__c> teams = (List<n2de__Team__c>) deObjectsReferencingField.get(2);
for (n2de__Team__c team : teams) {
if (!deObjsUsingFieldMap.containsKey(team.Id)) {
ObjectWrapper teamWrapper = new ObjectWrapper(team.Id, team.Name, 'Team');
deObjsUsingFieldMap.put(team.Id, teamWrapper);
}
}
//SLAs
List<n2de__Distribution_team_alert__c> alerts = (List<n2de__Distribution_team_alert__c>) deObjectsReferencingField.get(3);
for (n2de__Distribution_team_alert__c alert : alerts) {
if (!deObjsUsingFieldMap.containsKey(alert.n2de__Team__c)) {
ObjectWrapper team = new ObjectWrapper(alert.n2de__Team__c, alert.n2de__Team__r.Name, 'Team');
deObjsUsingFieldMap.put(alert.n2de__Team__c, team);
}
}

List<n2de__cls_Field__c> clsFields = (List<n2de__cls_Field__c>) deObjectsReferencingField.get(4);
for (n2de__cls_Field__c field : clsFields) {
if (!deObjsUsingFieldMap.containsKey(field.n2de__Classifier_object__c)) {
ObjectWrapper obj = new ObjectWrapper(field.n2de__Classifier_object__c, field.n2de__Classifier_object__r.Name, 'Classifier object');
deObjsUsingFieldMap.put(field.n2de__Classifier_object__c, obj);
}
}
List<n2de__cls_Object__c> clsObjects = (List<n2de__cls_Object__c>) deObjectsReferencingField.get(5);
for (n2de__cls_Object__c clsObj : clsObjects) {
if (!deObjsUsingFieldMap.containsKey(clsObj.Id)) {
ObjectWrapper obj = new ObjectWrapper(clsObj.Id, clsObj.Name, 'Classifier object');
deObjsUsingFieldMap.put(clsObj.Id, obj);
}
}

if (deObjsUsingFieldMap.isEmpty()) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'No field usage found'));
}

DEObjectsUsingField = deObjsUsingFieldMap.values();

return null;
}

public class ObjectWrapper {
public Id objectId { get; set; }
public String name { get; set; }
public String sobjectType { get; set; }

public ObjectWrapper(String id, String name, String objType) {
this.objectId = id;
this.name = name;
this.sobjectType = objType;
}
}
}

Visualforce Page

<apex:page controller="FieldsInUseByDEController" title="Fields in Use by DE">
<apex:form>
<apex:pageBlock>
<apex:pageBlockSection columns="1" collapsible="false">
<apex:inputText label="Object Name" value="{!objectName}" id="theObjectName" />
<apex:inputText label="Field Name" value="{!fieldName}" id="theFieldName" />
</apex:pageBlockSection>
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!search}" value="Search" reRender="output" />
</apex:pageBlockButtons>
</apex:pageBlock>
<apex:outputPanel id="output">
<apex:pageMessages/>
<apex:pageBlock>
<apex:pageBlockTable value="{!DEObjectsUsingField}" var="item">
<apex:column headerValue="DE Entity" value="{!item.sobjectType}" />
<apex:column headerValue="Name" value="{!item.name}" />
</apex:pageBlockTable>
</apex:pageBlock>
</apex:outputPanel>
</apex:form>
</apex:page>

How did we do?

Contact