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 setRecord method to LogEntryEventBuilder for handling Set of Ids and List of Ids #792

Merged
merged 8 commits into from
Nov 7, 2024
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ The most robust observability solution for Salesforce experts. Built 100% native

## Unlocked Package - v4.14.17

[![Install Unlocked Package in a Sandbox](./images/btn-install-unlocked-package-sandbox.png)](https://test.salesforce.com/packaging/installPackage.apexp?p0=04t5Y0000015ocRQAQ)
[![Install Unlocked Package in Production](./images/btn-install-unlocked-package-production.png)](https://login.salesforce.com/packaging/installPackage.apexp?p0=04t5Y0000015ocRQAQ)
[![Install Unlocked Package in a Sandbox](./images/btn-install-unlocked-package-sandbox.png)](https://test.salesforce.com/packaging/installPackage.apexp?p0=04t5Y0000015ocWQAQ)
[![Install Unlocked Package in Production](./images/btn-install-unlocked-package-production.png)](https://login.salesforce.com/packaging/installPackage.apexp?p0=04t5Y0000015ocWQAQ)
[![View Documentation](./images/btn-view-documentation.png)](https://github.com/jongpie/NebulaLogger/wiki)

`sf package install --wait 20 --security-type AdminsOnly --package 04t5Y0000015ocRQAQ`
`sf package install --wait 20 --security-type AdminsOnly --package 04t5Y0000015ocWQAQ`

`sfdx force:package:install --wait 20 --securitytype AdminsOnly --package 04t5Y0000015ocRQAQ`
`sfdx force:package:install --wait 20 --securitytype AdminsOnly --package 04t5Y0000015ocWQAQ`

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,26 @@ global with sharing class LogEntryEventBuilder {
return this;
}

/**
* @description Sets the log entry event's record fields
* @param recordsIds The Set of `SObject` records ids related to the entry. Will be converted to list and the JSON of the list is automatically added to the entry
* @return The same instance of `LogEntryEventBuilder`, useful for chaining methods
*/
global LogEntryEventBuilder setRecord(System.Iterable<Id> recordsIds) {
if (this.shouldSave() == false) {
return this;
}

List<SObject> records = new List<SObject>();
for (Id recordId : recordsIds) {
SObject record = recordId.getSObjectType().newSObject();
record.Id = recordId;
records.add(record);
}

return this.setRecord(records);
}

/**
* @description Sets the log entry event's HTTP Request fields
* @param request The instance of `HttpRequest` to log
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ private class LogEntryEventBuilder_Tests {
System.Assert.isNull(builder.setRecord(System.UserInfo.getUserId()).getLogEntryEvent());
System.Assert.isNull(builder.setRecord(new Schema.User(Id = System.UserInfo.getUserId())).getLogEntryEvent());
System.Assert.isNull(builder.setRecord(new List<Schema.User>{ new Schema.User(Id = System.UserInfo.getUserId()) }).getLogEntryEvent());
System.Assert.isNull(builder.setRecord(new Set<Id>{ System.UserInfo.getUserId() }).getLogEntryEvent());
System.Assert.isNull(builder.addTags(new List<String>{ 'tag-1', 'tag-2' }).getLogEntryEvent());
System.Assert.isNull(builder.parseStackTrace(new System.DmlException().getStackTraceString()).getLogEntryEvent());
}
Expand Down Expand Up @@ -1249,6 +1250,46 @@ private class LogEntryEventBuilder_Tests {
System.Assert.areEqual(Schema.User.SObjectType.getDescribe().getName(), builder.getLogEntryEvent().RecordSObjectType__c);
}

@IsTest
static void it_should_set_record_fields_for_list_of_id_records_when_populated() {
LogEntryEventBuilder builder = new LogEntryEventBuilder(getUserSettings(), System.LoggingLevel.INFO, true);
System.Assert.isNull(builder.getLogEntryEvent().RecordId__c);
System.Assert.isNull(builder.getLogEntryEvent().RecordJson__c);
System.Assert.isNull(builder.getLogEntryEvent().RecordSObjectClassification__c);
System.Assert.isNull(builder.getLogEntryEvent().RecordSObjectType__c);
System.Assert.isNull(builder.getLogEntryEvent().RecordSObjectTypeNamespace__c);

List<Id> userIdList = new List<Id>(new Map<Id, Schema.User>([SELECT Id, Name, Username, IsActive FROM User LIMIT 5]).keySet());
builder.setRecord(userIdList);

System.Assert.areEqual(userIdList.size(), builder.getLogEntryEvent().RecordCollectionSize__c);
System.Assert.areEqual('List', builder.getLogEntryEvent().RecordCollectionType__c);
System.Assert.isNull(builder.getLogEntryEvent().RecordId__c);
System.Assert.isNotNull(builder.getLogEntryEvent().RecordJson__c);
System.Assert.areEqual('Unknown', builder.getLogEntryEvent().RecordSObjectClassification__c);
System.Assert.areEqual('Unknown', builder.getLogEntryEvent().RecordSObjectType__c);
}

@IsTest
static void it_should_set_record_fields_for_set_of_id_records_when_populated() {
LogEntryEventBuilder builder = new LogEntryEventBuilder(getUserSettings(), System.LoggingLevel.INFO, true);
System.Assert.isNull(builder.getLogEntryEvent().RecordId__c);
System.Assert.isNull(builder.getLogEntryEvent().RecordJson__c);
System.Assert.isNull(builder.getLogEntryEvent().RecordSObjectClassification__c);
System.Assert.isNull(builder.getLogEntryEvent().RecordSObjectType__c);
System.Assert.isNull(builder.getLogEntryEvent().RecordSObjectTypeNamespace__c);

Set<Id> userIdSet = new Map<Id, Schema.User>([SELECT Id, Name, Username, IsActive FROM User LIMIT 5]).keySet();
builder.setRecord(userIdSet);

System.Assert.areEqual(userIdSet.size(), builder.getLogEntryEvent().RecordCollectionSize__c);
System.Assert.areEqual('List', builder.getLogEntryEvent().RecordCollectionType__c);
System.Assert.isNull(builder.getLogEntryEvent().RecordId__c);
System.Assert.isNotNull(builder.getLogEntryEvent().RecordJson__c);
System.Assert.areEqual('Unknown', builder.getLogEntryEvent().RecordSObjectClassification__c);
System.Assert.areEqual('Unknown', builder.getLogEntryEvent().RecordSObjectType__c);
}

@IsTest
static void it_should_skip_setting_http_request_fields_when_request_is_null() {
LogEntryEventBuilder builder = new LogEntryEventBuilder(getUserSettings(), System.LoggingLevel.INFO, true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//------------------------------------------------------------------------------------------------//
// This file is part of the Nebula Logger project, released under the MIT License. //
// See LICENSE file or go to https://github.com/jongpie/NebulaLogger for full license details. //
//------------------------------------------------------------------------------------------------//

// This class intentionally does nothing - it's here to ensure that any references
// in Nebula Logger's codebase use `System.Iterable` instead of just `Iterable`
@SuppressWarnings('PMD.ApexDoc, PMD.EmptyStatementBlock')
public without sharing class Iterable {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>61.0</apiVersion>
<status>Active</status>
</ApexClass>
1 change: 1 addition & 0 deletions sfdx-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@
"Nebula Logger - [email protected]": "04t5Y0000015obxQAA",
"Nebula Logger - [email protected]": "04t5Y0000015ocHQAQ",
"Nebula Logger - [email protected]": "04t5Y0000015ocRQAQ",
"Nebula Logger - [email protected]()-overload-for-list<id>-and-set<id>-parameters": "04t5Y0000015ocWQAQ",
"Nebula Logger - Core Plugin - Async Failure Additions": "0Ho5Y000000blO4SAI",
"Nebula Logger - Core Plugin - Async Failure [email protected]": "04t5Y0000015lhiQAA",
"Nebula Logger - Core Plugin - Async Failure [email protected]": "04t5Y0000015lhsQAA",
Expand Down