Skip to content

Commit

Permalink
Updated all SObjectType and SObjectField usages of getDescribe().getN…
Browse files Browse the repository at this point in the history
…ame() to toString() - it's easier to read, and based on some benchmarking @jamessimone did, it's faster too
  • Loading branch information
jongpie committed Nov 1, 2024
1 parent be1ad9d commit 3d87c3f
Show file tree
Hide file tree
Showing 26 changed files with 108 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public without sharing class LoggerBatchableContext {
public LoggerBatchableContext(Database.BatchableContext batchableContext, Schema.SObjectType sobjectType) {
this.batchableContext = batchableContext;
this.sobjectType = sobjectType;
this.sobjectTypeName = sobjectType?.getDescribe().getName();
this.sobjectTypeName = sobjectType?.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public without sharing class LoggerTriggerableContext {

private void setupAdditionalProperties() {
// Additional invocable variables for Flow Builders (and Apex Developers too, if they want to use them)
this.sobjectTypeName = this.sobjectType.getDescribe().getName();
this.sobjectTypeName = this.sobjectType.toString();
this.triggerOperationTypeName = this.triggerOperationType?.name();
this.triggerRecords = new List<LoggerTriggerableContext.RecordInput>();
if (this.triggerNew != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public without sharing class LogEntryHandler extends LoggerSObjectHandler {
}

private void setApexClassFields() {
String apexClassSObjectTypeName = Schema.ApexClass.SObjectType.getDescribe().getName();
String apexClassSObjectTypeName = Schema.ApexClass.SObjectType.toString();
Set<String> apexClassNames = new Set<String>();
Set<LogEntry__c> apexClassLogEntries = new Set<LogEntry__c>();
for (LogEntry__c logEntry : this.logEntries) {
Expand Down Expand Up @@ -84,7 +84,7 @@ public without sharing class LogEntryHandler extends LoggerSObjectHandler {
}

private void setApexTriggerFields() {
String apexTriggerSObjectTypeName = Schema.ApexTrigger.SObjectType.getDescribe().getName();
String apexTriggerSObjectTypeName = Schema.ApexTrigger.SObjectType.toString();
Set<String> apexTriggerNames = new Set<String>();
Set<LogEntry__c> apexTriggerLogEntries = new Set<LogEntry__c>();
for (LogEntry__c logEntry : this.logEntries) {
Expand Down Expand Up @@ -371,7 +371,7 @@ public without sharing class LogEntryHandler extends LoggerSObjectHandler {

// Use username instead of name for user
if (sobjectType == Schema.User.SObjectType) {
String userFieldName = Schema.User.Username.getDescribe().getName();
String userFieldName = Schema.User.Username.toString();
SOBJECT_TYPE_TO_DISPLAY_FIELD_NAME.put(Schema.User.SObjectType, userFieldName);
return userFieldName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public without sharing virtual class LogManagementDataSelector {

List<String> logFieldNames = new List<String>(Schema.Log__c.SObjectType.getDescribe().fields.getMap().keySet());
logFieldNames.addAll(new List<String>{ 'Owner.Name', 'Owner.Type' });
List<String> textReplacements = new List<String>{ String.join(logFieldNames, ','), Schema.Log__c.SObjectType.getDescribe().getName() };
List<String> textReplacements = new List<String>{ String.join(logFieldNames, ','), Schema.Log__c.SObjectType.toString() };

String query = String.format(queryTemplate, textReplacements);
return (Log__c) System.Database.query(String.escapeSingleQuotes(query));
Expand All @@ -229,7 +229,7 @@ public without sharing virtual class LogManagementDataSelector {
String queryTemplate = 'SELECT {0} FROM {1} WHERE Id = :logEntryId';

List<String> logEntryFieldNames = new List<String>(Schema.LogEntry__c.SObjectType.getDescribe().fields.getMap().keySet());
List<String> textReplacements = new List<String>{ String.join(logEntryFieldNames, ','), Schema.LogEntry__c.SObjectType.getDescribe().getName() };
List<String> textReplacements = new List<String>{ String.join(logEntryFieldNames, ','), Schema.LogEntry__c.SObjectType.toString() };

String query = String.format(queryTemplate, textReplacements);
return (LogEntry__c) System.Database.query(String.escapeSingleQuotes(query));
Expand All @@ -246,9 +246,9 @@ public without sharing virtual class LogManagementDataSelector {
List<String> logEntryFieldNames = new List<String>(Schema.LogEntry__c.SObjectType.getDescribe().fields.getMap().keySet());
List<String> textReplacements = new List<String>{
String.join(logEntryFieldNames, ', '),
Schema.LogEntry__c.SObjectType.getDescribe().getName(),
Schema.LogEntry__c.Log__c.getDescribe().getName(),
Schema.LogEntry__c.TransactionEntryNumber__c.getDescribe().getName()
Schema.LogEntry__c.SObjectType.toString(),
Schema.LogEntry__c.Log__c.toString(),
Schema.LogEntry__c.TransactionEntryNumber__c.toString()
};

String query = String.format(queryTemplate, textReplacements);
Expand Down Expand Up @@ -357,8 +357,8 @@ public without sharing virtual class LogManagementDataSelector {
public virtual List<LogEntry__c> getRecordLogEntries(Id recordId, String fieldsClause, String orderByClause, Integer rowLimit) {
List<Object> queryTextReplacements = new List<Object>{
fieldsClause,
Schema.LogEntry__c.SObjectType.getDescribe().getName(),
Schema.LogEntry__c.RecordId__c.getDescribe().getName(),
Schema.LogEntry__c.SObjectType.toString(),
Schema.LogEntry__c.RecordId__c.toString(),
orderByClause,
rowLimit
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public with sharing class RelatedLogEntriesController {

// Use username instead of name for user
if (lookupSObjectType == Schema.User.SObjectType) {
return relationshipName + '.' + Schema.User.Username.getDescribe().getName();
return relationshipName + '.' + Schema.User.Username.toString();
}

// There are several commonly used names for the display field name - typically, Name, but check for others
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ global inherited sharing class FlowCollectionLogEntry {
);
LogEntryEventBuilder builder = shadowLogEntry.addToLoggerBuffer()?.setRecord(flowCollectionLogEntry.records);
if (builder.shouldSave() && flowCollectionLogEntry.records?.size() > 0) {
builder.getLogEntryEvent().RecordSObjectType__c = flowCollectionLogEntry.records.get(0).getSObjectType().getDescribe().getName();
builder.getLogEntryEvent().RecordSObjectType__c = flowCollectionLogEntry.records.get(0).getSObjectType().toString();
}

shadowLogEntries.add(shadowLogEntry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ global with sharing class LogEntryEventBuilder {
try {
Schema.SObjectType sobjectType = recordId?.getSObjectType();
this.logEntryEvent.RecordSObjectClassification__c = getSObjectClassification(sobjectType);
this.logEntryEvent.RecordSObjectType__c = sobjectType.getDescribe().getName();
this.logEntryEvent.RecordSObjectType__c = sobjectType.toString();
} catch (System.SObjectException sobjectException) {
// Some SObject Types are considered template objects, such as CaseComment, AccountHistory,
// ContactHistory, etc, so calling Id.getSObjectType() does not work for some types of record IDs
Expand Down Expand Up @@ -382,7 +382,7 @@ global with sharing class LogEntryEventBuilder {
this.logEntryEvent.RecordJson__c = cleanedRecordJson;
this.logEntryEvent.RecordJsonMasked__c = recordJsonMasked;
this.logEntryEvent.RecordSObjectClassification__c = getSObjectClassification(record.getSObjectType());
this.logEntryEvent.RecordSObjectType__c = record.getSObjectType().getDescribe().getName();
this.logEntryEvent.RecordSObjectType__c = record.getSObjectType().toString();
this.logEntryEvent.RecordSObjectTypeNamespace__c = getSObjectTypeNamespace(record.getSObjectType());
}

Expand Down Expand Up @@ -418,7 +418,7 @@ global with sharing class LogEntryEventBuilder {
this.logEntryEvent.RecordJson__c = cleanedRecordJson;
this.logEntryEvent.RecordJsonMasked__c = recordJsonMasked;
this.logEntryEvent.RecordSObjectClassification__c = getSObjectClassification(sobjectType);
this.logEntryEvent.RecordSObjectType__c = sobjectType.getDescribe().getName();
this.logEntryEvent.RecordSObjectType__c = sobjectType.toString();
this.logEntryEvent.RecordSObjectTypeNamespace__c = getSObjectTypeNamespace(sobjectType);
}

Expand Down Expand Up @@ -455,7 +455,7 @@ global with sharing class LogEntryEventBuilder {
this.logEntryEvent.RecordJson__c = cleanedRecordJson;
this.logEntryEvent.RecordJsonMasked__c = recordJsonMasked;
this.logEntryEvent.RecordSObjectClassification__c = getSObjectClassification(sobjectType);
this.logEntryEvent.RecordSObjectType__c = sobjectType.getDescribe().getName();
this.logEntryEvent.RecordSObjectType__c = sobjectType.toString();
this.logEntryEvent.RecordSObjectTypeNamespace__c = getSObjectTypeNamespace(sobjectType);
}

Expand Down Expand Up @@ -880,7 +880,7 @@ global with sharing class LogEntryEventBuilder {
OriginType__c = 'Apex',
TriggerIsExecuting__c = Trigger.isExecuting,
TriggerOperationType__c = Trigger.operationType?.name(),
TriggerSObjectType__c = Trigger.new?.getSObjectType().getDescribe().getName()
TriggerSObjectType__c = Trigger.new?.getSObjectType().toString()
);

if (LoggerParameter.STORE_TRANSACTION_LIMITS) {
Expand Down Expand Up @@ -1100,7 +1100,7 @@ global with sharing class LogEntryEventBuilder {
}

private static String getSObjectClassification(Schema.SObjectType sobjectType) {
String sobjectName = sobjectType.getDescribe().getName();
String sobjectName = sobjectType.toString();

// Check the map to see if we've already determined the classification for this SObject type
if (CACHED_SOBJECT_NAME_TO_CLASSIFICATION.containsKey(sobjectName)) {
Expand Down Expand Up @@ -1158,7 +1158,7 @@ global with sharing class LogEntryEventBuilder {
}

private static String getSObjectTypeNamespace(Schema.SObjectType sobjectType) {
String sobjectFullName = sobjectType.getDescribe().getName();
String sobjectFullName = sobjectType.toString();
String sobjectLocalName = sobjectType.getDescribe().getLocalName();

// Remove the trailing '__' if present
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,18 @@ private class LoggerFieldMapper_Tests {
new LoggerFieldMapping__mdt(
DeveloperName = 'SomeDevName',
IsEnabled__c = true,
SourceSObjectType__c = Schema.LogEntryEvent__e.SObjectType.getDescribe().getName(),
SourceField__c = Schema.LogEntryEvent__e.Message__c.getDescribe().getName(),
TargetSObjectType__c = Schema.LogEntry__c.SObjectType.getDescribe().getName(),
TargetField__c = Schema.LogEntry__c.Message__c.getDescribe().getName()
SourceSObjectType__c = Schema.LogEntryEvent__e.SObjectType.toString(),
SourceField__c = Schema.LogEntryEvent__e.Message__c.toString(),
TargetSObjectType__c = Schema.LogEntry__c.SObjectType.toString(),
TargetField__c = Schema.LogEntry__c.Message__c.toString()
)
);
LogEntryEvent__e logEntryEvent = new LogEntryEvent__e(Message__c = 'Some value');
LogEntry__c logEntry = new LogEntry__c();

LoggerFieldMapper.mapFieldValues(logEntryEvent, logEntry);

System.Assert.isTrue(
logEntry.getPopulatedFieldsAsMap().containsKey(Schema.LogEntry__c.Message__c.getDescribe().getName()),
System.JSON.serialize(logEntry)
);
System.Assert.isTrue(logEntry.getPopulatedFieldsAsMap().containsKey(Schema.LogEntry__c.Message__c.toString()), System.JSON.serialize(logEntry));
System.Assert.areEqual(logEntryEvent.Message__c, logEntry.Message__c);
}

Expand All @@ -51,10 +48,10 @@ private class LoggerFieldMapper_Tests {
new LoggerFieldMapping__mdt(
DeveloperName = 'SomeDevName',
IsEnabled__c = true,
SourceSObjectType__c = Schema.LogEntryEvent__e.SObjectType.getDescribe().getName(),
SourceField__c = Schema.LogEntryEvent__e.Message__c.getDescribe().getName(),
TargetSObjectType__c = Schema.LogEntry__c.SObjectType.getDescribe().getName(),
TargetField__c = Schema.LogEntry__c.Message__c.getDescribe().getName()
SourceSObjectType__c = Schema.LogEntryEvent__e.SObjectType.toString(),
SourceField__c = Schema.LogEntryEvent__e.Message__c.toString(),
TargetSObjectType__c = Schema.LogEntry__c.SObjectType.toString(),
TargetField__c = Schema.LogEntry__c.Message__c.toString()
)
);
Integer messageMaxLength = Schema.LogEntryEvent__e.Message__c.getDescribe().getLength();
Expand All @@ -63,10 +60,7 @@ private class LoggerFieldMapper_Tests {

LoggerFieldMapper.mapFieldValues(logEntryEvent, logEntry);

System.Assert.isTrue(
logEntry.getPopulatedFieldsAsMap().containsKey(Schema.LogEntry__c.Message__c.getDescribe().getName()),
System.JSON.serialize(logEntry)
);
System.Assert.isTrue(logEntry.getPopulatedFieldsAsMap().containsKey(Schema.LogEntry__c.Message__c.toString()), System.JSON.serialize(logEntry));
System.Assert.areEqual(
logEntryEvent.Message__c.left(messageMaxLength),
logEntry.Message__c,
Expand All @@ -80,10 +74,10 @@ private class LoggerFieldMapper_Tests {
new LoggerFieldMapping__mdt(
DeveloperName = 'SomeDevName',
IsEnabled__c = true,
SourceSObjectType__c = Schema.LogEntryEvent__e.SObjectType.getDescribe().getName(),
SourceField__c = Schema.LogEntryEvent__e.TransactionId__c.getDescribe().getName(),
TargetSObjectType__c = Schema.Log__c.SObjectType.getDescribe().getName(),
TargetField__c = Schema.Log__c.TransactionId__c.getDescribe().getName()
SourceSObjectType__c = Schema.LogEntryEvent__e.SObjectType.toString(),
SourceField__c = Schema.LogEntryEvent__e.TransactionId__c.toString(),
TargetSObjectType__c = Schema.Log__c.SObjectType.toString(),
TargetField__c = Schema.Log__c.TransactionId__c.toString()
)
);
LogEntryEvent__e logEntryEvent = new LogEntryEvent__e(Message__c = 'Some value');
Expand All @@ -100,30 +94,30 @@ private class LoggerFieldMapper_Tests {
new LoggerFieldMapping__mdt(
DeveloperName = '🥳 Some Valid Mapping',
IsEnabled__c = true,
SourceSObjectType__c = Schema.LogEntryEvent__e.SObjectType.getDescribe().getName(),
SourceField__c = Schema.LogEntryEvent__e.EntryScenario__c.getDescribe().getName(),
TargetSObjectType__c = Schema.LogEntry__c.SObjectType.getDescribe().getName(),
TargetField__c = Schema.LogEntry__c.EntryScenario__c.getDescribe().getName()
SourceSObjectType__c = Schema.LogEntryEvent__e.SObjectType.toString(),
SourceField__c = Schema.LogEntryEvent__e.EntryScenario__c.toString(),
TargetSObjectType__c = Schema.LogEntry__c.SObjectType.toString(),
TargetField__c = Schema.LogEntry__c.EntryScenario__c.toString()
)
);
LoggerFieldMapper.setMock(
new LoggerFieldMapping__mdt(
DeveloperName = '😭 An Incompatible Mapping (mapping a String to a Datetime)',
IsEnabled__c = true,
SourceSObjectType__c = Schema.LogEntryEvent__e.SObjectType.getDescribe().getName(),
SourceField__c = Schema.LogEntryEvent__e.Message__c.getDescribe().getName(),
TargetSObjectType__c = Schema.LogEntry__c.SObjectType.getDescribe().getName(),
TargetField__c = Schema.LogEntry__c.Timestamp__c.getDescribe().getName()
SourceSObjectType__c = Schema.LogEntryEvent__e.SObjectType.toString(),
SourceField__c = Schema.LogEntryEvent__e.Message__c.toString(),
TargetSObjectType__c = Schema.LogEntry__c.SObjectType.toString(),
TargetField__c = Schema.LogEntry__c.Timestamp__c.toString()
)
);
LoggerFieldMapper.setMock(
new LoggerFieldMapping__mdt(
DeveloperName = '😀 Another Valid Mapping',
IsEnabled__c = true,
SourceSObjectType__c = Schema.LogEntryEvent__e.SObjectType.getDescribe().getName(),
SourceField__c = Schema.LogEntryEvent__e.TransactionEntryNumber__c.getDescribe().getName(),
TargetSObjectType__c = Schema.LogEntry__c.SObjectType.getDescribe().getName(),
TargetField__c = Schema.LogEntry__c.TransactionEntryNumber__c.getDescribe().getName()
SourceSObjectType__c = Schema.LogEntryEvent__e.SObjectType.toString(),
SourceField__c = Schema.LogEntryEvent__e.TransactionEntryNumber__c.toString(),
TargetSObjectType__c = Schema.LogEntry__c.SObjectType.toString(),
TargetField__c = Schema.LogEntry__c.TransactionEntryNumber__c.toString()
)
);
LogEntryEvent__e logEntryEvent = new LogEntryEvent__e(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public class LoggerMockDataCreator {

// To avoid a MIXED_DML_OPERATION exception, use System.runs() for inserting the QueueSObject record
System.runAs(new Schema.User(Id = System.UserInfo.getUserId())) {
QueueSObject queueSObject = new QueueSObject(QueueId = queue.Id, SObjectType = sobjectType.getDescribe().getName());
QueueSObject queueSObject = new QueueSObject(QueueId = queue.Id, SObjectType = sobjectType.toString());
insert queueSObject;
}

Expand Down Expand Up @@ -419,7 +419,7 @@ public class LoggerMockDataCreator {
// Loop through the deserialized record map and put the field & value
// Since it's a map, if the field already exists on the SObject, it's updated (or added if it wasn't there already)
for (Schema.SObjectField sobjectField : changesToFields.keySet()) {
String fieldName = sobjectField.getDescribe().getName();
String fieldName = sobjectField.toString();
deserializedRecordMap.put(fieldName, changesToFields.get(sobjectField));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ private class LogEntryEventHandler_Tests {
String configuredTagName = 'CMDT Tag';
LoggerTestConfigurator.setMock(
new LogEntryTagRule__mdt(
SObjectField__c = Schema.LogEntry__c.Message__c.getDescribe().getName(),
SObjectField__c = Schema.LogEntry__c.Message__c.toString(),
ComparisonType__c = 'CONTAINS',
ComparisonValue__c = 'some value',
Tags__c = configuredTagName
Expand Down Expand Up @@ -918,7 +918,7 @@ private class LogEntryEventHandler_Tests {
String configuredTagName = 'CMDT Tag';
LoggerTestConfigurator.setMock(
new LogEntryTagRule__mdt(
SObjectField__c = Schema.LogEntry__c.Message__c.getDescribe().getName(),
SObjectField__c = Schema.LogEntry__c.Message__c.toString(),
ComparisonType__c = 'CONTAINS',
ComparisonValue__c = logEntryEvent.Message__c.substring(1, 4),
Tags__c = configuredTagName
Expand Down Expand Up @@ -958,7 +958,7 @@ private class LogEntryEventHandler_Tests {
String configuredTagName = 'CMDT Tag';
LoggerTestConfigurator.setMock(
new LogEntryTagRule__mdt(
SObjectField__c = Schema.LogEntry__c.Message__c.getDescribe().getName(),
SObjectField__c = Schema.LogEntry__c.Message__c.toString(),
ComparisonType__c = 'EQUALS',
ComparisonValue__c = logEntryEvent.Message__c,
Tags__c = configuredTagName
Expand Down Expand Up @@ -1000,7 +1000,7 @@ private class LogEntryEventHandler_Tests {
String configuredTagName = 'CMDT Tag';
LoggerTestConfigurator.setMock(
new LogEntryTagRule__mdt(
SObjectField__c = Schema.LogEntry__c.Message__c.getDescribe().getName(),
SObjectField__c = Schema.LogEntry__c.Message__c.toString(),
ComparisonType__c = 'MATCHES_REGEX',
ComparisonValue__c = zipCodeRegEx,
Tags__c = configuredTagName
Expand Down Expand Up @@ -1041,7 +1041,7 @@ private class LogEntryEventHandler_Tests {
String configuredTagName = 'CMDT Tag';
LoggerTestConfigurator.setMock(
new LogEntryTagRule__mdt(
SObjectField__c = Schema.LogEntry__c.Message__c.getDescribe().getName(),
SObjectField__c = Schema.LogEntry__c.Message__c.toString(),
ComparisonType__c = 'STARTS_WITH',
ComparisonValue__c = logEntryEvent.Message__c.left(3),
Tags__c = configuredTagName
Expand Down
Loading

0 comments on commit 3d87c3f

Please sign in to comment.