Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setDatabaseResult(EmptyRecycleBinResult) and setDatabaseResult(List<EmptyRecycleBinResult>) #806

Merged
merged 16 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ global with sharing class LogEntryEventBuilder {
private static void setUserInfoDetails(LogEntryEvent__e templateLogEntryEvent) {
templateLogEntryEvent.Locale__c = System.UserInfo.getLocale();
templateLogEntryEvent.LoggedById__c = System.UserInfo.getUserId();
templateLogEntryEvent.LoggedByUsername__c = System.UserInfo.getUserName();
templateLogEntryEvent.LoggedByUsername__c = System.UserInfo.getUsername();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jongpie, why? It was correct, according to the Salesforce API.
The IDE complains about the current one:
image

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TrangOul I've never seen that warning in VS Code, but maybe I've disabled whatever option that is - do you know what plugin/feature controls that warning message?

Personally, I've always considered the user of getUserName() (capital N) a bug in Apex/Salesforce docs. I think getUsername() should be considered the correct casing - it's consistent with the rest of the platform, and it provides clarification that it is referring to the "username", and the "user's name".

  • Everywhere else in Salesforce (and from what I've seen in other systems too/software engineering in general), it's always spelled Username with a lowercase n. The only place I'm aware of in Salesforce that uses the capital N is that method name. And since it returns the value of the field User.Username, I think the method name should use the same casing.
    • User.Username field has lowercase n in the field API name, the field label, and the corresponding docs

      image

    • Salesforce login pages (including Community Cloud/Experience Cloud, etc) all

      image

I've also worked on multiple teams (at different companies) where using getUserName() is too ambiguous & confusing for people new to Apex (or anyone unfamiliar with the UserInfo class), occasionally resulting in bugs.

  • Some people (incorrectly) thought UserName referred to the user's name (i.e., the User.Name field that combines User.FirstName and User.LastName
  • Some people (correctly) thought UserName referred to the user's username (i.e., the User.Username field)

I think both are valid thoughts/guesses with the UserName casing - Salesforce uses User as a prefix on large number of standard fields (listed below), so I think there will always, inevitably, be some confusion if getUserName() refers to User.Name or User.Username

  • User.UserRoleId
  • User.UserType
  • 12 fields that with UserPermissions..., like User.UserPermissionsMobileUser
  • 81 fields that start with UserPreferences...., like User.UserPreferencesUserDebugModePref

But I've personally never had a situation where using getUsername() is unclear/confusing.

Copy link
Contributor Author

@TrangOul TrangOul Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jongpie, I'm using WebStorm with Illuminated Cloud 2. Here are my settings:
image

Unfortunately, Salesforce is inconsistent when it comes to letter casing. Another example: SObject:
image
(in my own code I use sobject at the beginning of the name and SObject later, for example getSObjectApiName(sobj); IMO sObj looks goofy)

It's even more annoying, since Apex is case-insensitive and such inconsistencies could be fixed in one of the release updated without causing any regression.

I agree with your reasoning that Username = username (technical) and UserName = user's name (user-friendly), and I also stick to "username" as a single word in my own class/function/variable names (and also correct them in code reviews).
Nevertheless, I'm sticking to what SF API uses because it simplifies my workflow - I just use automatic formatting (which, obviously, does much more that changing the case).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TrangOul thanks for the info & sharing your perspective! I might need to finally try Illuminated Cloud 2 again, it seems like it has a lot of cool features built in that I'd like to try out.

I wasn't aware of those inconsistencies of sobj.getSObjectType() and recordId.getSobjectType() 😭 That's kind of crazy, haha I wish Salesforce would be consistent (especially since SObject and SObjectType are a Salesforce-created terms). I personally find that having consistency (like always use SObject, never Sobject) really helps me with readability when jumping between different parts of the codebase (especially as Nebula Logger's codebase continues to grow in size/lines of code). But I totally understand your approach of sticking to what SF API uses if it simplifies your workflow.

templateLogEntryEvent.ProfileId__c = System.UserInfo.getProfileId();
templateLogEntryEvent.ThemeDisplayed__c = System.UserInfo.getUiThemeDisplayed();
templateLogEntryEvent.TimeZoneId__c = System.UserInfo.getTimeZone().getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@IsTest(IsParallel=true)
private class LoggerParameter_Tests {
private static Schema.User getUserRecord() {
return new Schema.User(Id = System.UserInfo.getUserId(), Username = System.UserInfo.getUserName());
return new Schema.User(Id = System.UserInfo.getUserId(), Username = System.UserInfo.getUsername());
}

@IsTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,7 @@ private class LogEntryHandler_Tests {
if (IS_OMNISTUDIO_ENABLED == false) {
return;
}
Schema.User currentUser = new Schema.User(Id = System.UserInfo.getUserId(), Username = System.UserInfo.getUserName());
Schema.User currentUser = new Schema.User(Id = System.UserInfo.getUserId(), Username = System.UserInfo.getUsername());
SObject mockOmniProcessRecord = (SObject) (System.Type.forName('Schema.OmniProcess').newInstance());
LoggerSObjectProxy.OmniProcess mockOmniProcessProxy = new LoggerSObjectProxy.OmniProcess(mockOmniProcessRecord);
mockOmniProcessProxy.CreatedById = System.UserInfo.getUserId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private class FlowCollectionLogEntry_Tests {
Id = System.UserInfo.getUserId(),
FirstName = System.UserInfo.getFirstName(),
LastName = System.UserInfo.getLastName(),
Username = System.UserInfo.getUserName()
Username = System.UserInfo.getUsername()
);
System.LoggingLevel userLoggingLevel = System.LoggingLevel.FINEST;
System.LoggingLevel flowCollectionEntryLoggingLevel = System.LoggingLevel.DEBUG;
Expand Down Expand Up @@ -63,7 +63,7 @@ private class FlowCollectionLogEntry_Tests {
Id = System.UserInfo.getUserId(),
FirstName = System.UserInfo.getFirstName(),
LastName = System.UserInfo.getLastName(),
Username = System.UserInfo.getUserName()
Username = System.UserInfo.getUsername()
);
System.LoggingLevel userLoggingLevel = System.LoggingLevel.FINEST;
System.LoggingLevel flowCollectionEntryLoggingLevel = System.LoggingLevel.DEBUG;
Expand Down Expand Up @@ -102,7 +102,7 @@ private class FlowCollectionLogEntry_Tests {
Id = System.UserInfo.getUserId(),
FirstName = System.UserInfo.getFirstName(),
LastName = System.UserInfo.getLastName(),
Username = System.UserInfo.getUserName()
Username = System.UserInfo.getUsername()
);
System.LoggingLevel userLoggingLevel = System.LoggingLevel.FINEST;
System.LoggingLevel flowCollectionEntryLoggingLevel = System.LoggingLevel.DEBUG;
Expand Down Expand Up @@ -148,7 +148,7 @@ private class FlowCollectionLogEntry_Tests {
Id = System.UserInfo.getUserId(),
FirstName = System.UserInfo.getFirstName(),
LastName = System.UserInfo.getLastName(),
Username = System.UserInfo.getUserName()
Username = System.UserInfo.getUsername()
);
System.LoggingLevel userLoggingLevel = System.LoggingLevel.ERROR;
System.LoggingLevel flowCollectionEntryLoggingLevel = System.LoggingLevel.DEBUG;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private class FlowRecordLogEntry_Tests {
Id = System.UserInfo.getUserId(),
FirstName = System.UserInfo.getFirstName(),
LastName = System.UserInfo.getLastName(),
Username = System.UserInfo.getUserName()
Username = System.UserInfo.getUsername()
);
System.LoggingLevel userLoggingLevel = System.LoggingLevel.FINEST;
System.LoggingLevel flowRecordEntryLoggingLevel = System.LoggingLevel.DEBUG;
Expand Down Expand Up @@ -64,7 +64,7 @@ private class FlowRecordLogEntry_Tests {
Id = System.UserInfo.getUserId(),
FirstName = System.UserInfo.getFirstName(),
LastName = System.UserInfo.getLastName(),
Username = System.UserInfo.getUserName()
Username = System.UserInfo.getUsername()
);
System.LoggingLevel userLoggingLevel = System.LoggingLevel.FINEST;
System.LoggingLevel flowRecordEntryLoggingLevel = System.LoggingLevel.DEBUG;
Expand Down Expand Up @@ -108,7 +108,7 @@ private class FlowRecordLogEntry_Tests {
Id = System.UserInfo.getUserId(),
FirstName = System.UserInfo.getFirstName(),
LastName = System.UserInfo.getLastName(),
Username = System.UserInfo.getUserName()
Username = System.UserInfo.getUsername()
);
System.LoggingLevel userLoggingLevel = System.LoggingLevel.FINEST;
System.LoggingLevel flowRecordEntryLoggingLevel = System.LoggingLevel.DEBUG;
Expand Down Expand Up @@ -145,7 +145,7 @@ private class FlowRecordLogEntry_Tests {
Id = System.UserInfo.getUserId(),
FirstName = System.UserInfo.getFirstName(),
LastName = System.UserInfo.getLastName(),
Username = System.UserInfo.getUserName()
Username = System.UserInfo.getUsername()
);
System.LoggingLevel userLoggingLevel = System.LoggingLevel.ERROR;
System.LoggingLevel flowRecordEntryLoggingLevel = System.LoggingLevel.DEBUG;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,7 @@ private class LogEntryEventBuilder_Tests {
Schema.User nullUser;
Map<Id, SObject> userIdToUser = new Map<Id, SObject>{
mockUserId => nullUser,
System.UserInfo.getUserId() => new Schema.User(Id = System.UserInfo.getUserId(), Username = System.UserInfo.getUserName())
System.UserInfo.getUserId() => new Schema.User(Id = System.UserInfo.getUserId(), Username = System.UserInfo.getUsername())
};
builder.setRecord(userIdToUser);

Expand Down Expand Up @@ -1877,7 +1877,7 @@ private class LogEntryEventBuilder_Tests {
System.Assert.areEqual(System.UserInfo.getUserId(), builder.getLogEntryEvent().LoggedById__c);
System.Assert.areEqual(System.UserInfo.getLocale(), builder.getLogEntryEvent().Locale__c);
System.Assert.areEqual(System.UserInfo.getUserId(), builder.getLogEntryEvent().LoggedById__c);
System.Assert.areEqual(System.UserInfo.getUserName(), builder.getLogEntryEvent().LoggedByUsername__c);
System.Assert.areEqual(System.UserInfo.getUsername(), builder.getLogEntryEvent().LoggedByUsername__c);
System.Assert.areEqual(System.UserInfo.getProfileId(), builder.getLogEntryEvent().ProfileId__c);
System.Assert.areEqual(System.UserInfo.getUiThemeDisplayed(), builder.getLogEntryEvent().ThemeDisplayed__c);
System.Assert.areEqual(System.UserInfo.getTimeZone().getId(), builder.getLogEntryEvent().TimeZoneId__c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private class LoggerSObjectProxy_Tests {
if (IS_OMNISTUDIO_ENABLED == false) {
return;
}
Schema.User currentUser = new Schema.User(Id = System.UserInfo.getUserId(), Username = System.UserInfo.getUserName());
Schema.User currentUser = new Schema.User(Id = System.UserInfo.getUserId(), Username = System.UserInfo.getUsername());
// String createdByFieldName = 'CreatedBy';
String createdByIdFieldName = 'CreatedById';
String createdDateFieldName = 'CreatedDate';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11451,7 +11451,7 @@ private class Logger_Tests {
return new Schema.User(
Id = System.UserInfo.getUserId(),
ProfileId = System.UserInfo.getProfileId(),
Username = System.UserInfo.getUserName(),
Username = System.UserInfo.getUsername(),
UserRoleId = System.UserInfo.getUserRoleId()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private class LogFlowExecutionErrorEventHandler_Tests {
flowExecutionErrorEvent.ElementApiName = 'test_exception_element';
flowExecutionErrorEvent.FlowApiName = 'Testing_FlowExecutionErrorEvent';
flowExecutionErrorEvent.FlowVersionNumber = 1;
flowExecutionErrorEvent.Username = System.UserInfo.getUserName();
flowExecutionErrorEvent.Username = System.UserInfo.getUsername();

System.runAs(new Schema.User(Id = [SELECT Id FROM User WHERE Alias = 'autoproc'].Id)) {
System.Test.startTest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public without sharing class LogEntryArchiveBuilder {
ApiVersion__c = this.logEntryEvent.ApiVersion__c,
ArchivedById__c = System.UserInfo.getUserId(),
ArchivedDate__c = System.Datetime.now(),
ArchivedByUsername__c = System.UserInfo.getUserName(),
ArchivedByUsername__c = System.UserInfo.getUsername(),
ComponentType__c = this.logEntryEvent.ComponentType__c,
DatabaseResultCollectionSize__c = this.logEntryEvent.DatabaseResultCollectionSize__c,
DatabaseResultCollectionType__c = this.logEntryEvent.DatabaseResultCollectionType__c,
Expand Down Expand Up @@ -180,7 +180,7 @@ public without sharing class LogEntryArchiveBuilder {
ApiVersion__c = this.logEntry.Log__r.ApiVersion__c,
ArchivedById__c = System.UserInfo.getUserId(),
ArchivedDate__c = System.Datetime.now(),
ArchivedByUsername__c = System.UserInfo.getUserName(),
ArchivedByUsername__c = System.UserInfo.getUsername(),
ClosedById__c = this.logEntry.Log__r.ClosedBy__c,
ClosedByUsername__c = this.logEntry.Log__r.ClosedBy__r.Username,
ClosedDate__c = this.logEntry.Log__r.ClosedDate__c,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private class LogEntryArchiveBuilder_Tests {
System.Assert.areEqual(System.UserInfo.getUserId(), logEntryArchive.ArchivedById__c, 'logEntryArchive.ArchivedById__c was not properly set');
System.Assert.isNotNull(logEntryArchive.ArchivedDate__c, 'logEntryArchive.ArchivedDate__c was not properly set');
System.Assert.areEqual(System.today(), logEntryArchive.ArchivedDate__c.date(), 'logEntryArchive.ArchivedDate__c was not properly set');
System.Assert.areEqual(System.UserInfo.getUserName(), logEntryArchive.ArchivedByUsername__c, 'logEntryArchive.ArchivedByUsername__c was not properly set');
System.Assert.areEqual(System.UserInfo.getUsername(), logEntryArchive.ArchivedByUsername__c, 'logEntryArchive.ArchivedByUsername__c was not properly set');
System.Assert.areEqual(mockEvent.ComponentType__c, logEntryArchive.ComponentType__c, 'logEntryArchive.ComponentType__c was not properly set');
System.Assert.areEqual(
mockEvent.DatabaseResultCollectionSize__c,
Expand Down Expand Up @@ -457,7 +457,7 @@ private class LogEntryArchiveBuilder_Tests {
System.Assert.areEqual(System.UserInfo.getUserId(), logEntryArchive.ArchivedById__c, 'logEntryArchive.ArchivedById__c was not properly set');
System.Assert.isNotNull(logEntryArchive.ArchivedDate__c, 'logEntryArchive.ArchivedDate__c was not properly set');
System.Assert.areEqual(System.today(), logEntryArchive.ArchivedDate__c.date(), 'logEntryArchive.ArchivedDate__c was not properly set');
System.Assert.areEqual(System.UserInfo.getUserName(), logEntryArchive.ArchivedByUsername__c, 'logEntryArchive.ArchivedByUsername__c was not properly set');
System.Assert.areEqual(System.UserInfo.getUsername(), logEntryArchive.ArchivedByUsername__c, 'logEntryArchive.ArchivedByUsername__c was not properly set');
System.Assert.areEqual(logEntry.Log__r.ClosedBy__c, logEntryArchive.ClosedById__c, 'logEntryArchive.ClosedById__c was not properly set');
System.Assert.areEqual(
logEntry.Log__r.ClosedBy__r?.Username,
Expand Down