Skip to content

Commit

Permalink
Merge pull request #1541 from aagrossman/update-count-records
Browse files Browse the repository at this point in the history
Update CountRecordsAndFields to accommodate boolean fields
  • Loading branch information
ericrsmith35 authored Aug 13, 2024
2 parents bda9c56 + 8f5c48d commit 0a311e5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@ global with sharing class CountRecordsAndFields {
List<SObject> records = curRequest.inputCollection;
String fieldName = curRequest.fieldName;
String fieldValue = curRequest.fieldValue;

//Create a Results object to hold the return values
Results response = new Results();
try {
for (SObject record : records) {
if (!String.isBlank(fieldName) && !String.isBlank(fieldValue)) {
if (record.get(fieldName) == fieldValue) {
response.matchedNumber++;
// Retrieve the field value from the record
Object fieldVal = record.get(fieldName);
if (fieldVal != null) {
// Convert the field value to a string for comparison
// Handle boolean fields by converting them to strings
String fieldValStr = (fieldVal instanceof Boolean) ? String.valueOf(fieldVal) : fieldVal.toString();
// Compare the field value string with the provided field value string
if (fieldValStr == fieldValue) {
response.matchedNumber++;
}
}
}

Expand All @@ -22,12 +30,12 @@ global with sharing class CountRecordsAndFields {
} catch (Exception ex) {
response.errors = ex.getMessage();
}

//Wrap the Results object in a List container (an extra step added to allow this interface to also support bulkification)

responseWrapper.add(response);
}

return responseWrapper;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,44 @@ public with sharing class ListActionsTest {
static void makeData() {
Test.startTest();
List<Account> testAccounts = new List<Account>();
List<Contact> testContacts = new List<Contact>(); // Added to create contacts

Boolean doNotCallFlag = true; // Initialize the flag
for (Integer i = 0; i < NUMBER_OF_TEST_RECORDS; i++) {
testAccounts.add(
new Account(
Name = TEST_RECORD_NAME + i,
Website = '' + i,
AnnualRevenue = 5000
)
Account acc = new Account(
Name = TEST_RECORD_NAME + i,
Website = '' + i,
AnnualRevenue = 5000
);
testAccounts.add(acc);

// Create contacts with DoNotCall field set for boolean testing
Contact con = new Contact(
LastName = TEST_CONTACT_NAME + i,
AccountId = acc.Id,
DoNotCall = doNotCallFlag // Set the value based on the flag
);
testContacts.add(con);

// Toggle the flag
doNotCallFlag = !doNotCallFlag;
}

// Insert testAccounts without triggering duplicate rules
Database.DMLOptions dml = new Database.DMLOptions();
dml.DuplicateRuleHeader.AllowSave = true;
Database.insert(testAccounts, dml);
insert testContacts; // Insert the test contacts

List<Contact> testContacts = new List<Contact>();
List<Task> testTasks = new List<Task>();
for (Integer i = 0; i < testAccounts.size(); i++) {
testContacts.add(
new Contact(
LastName = TEST_CONTACT_NAME + i,
AccountId = testAccounts[i].Id
)
);
testTasks.add(
new Task(
Subject = TEST_RECORD_NAME + i,
WhatId = testAccounts[0].Id
)
);
}
insert testContacts;
insert testTasks;
Test.stopTest();
}
Expand Down Expand Up @@ -582,6 +589,20 @@ public with sharing class ListActionsTest {
Assert.areEqual(responseWrapper.size(), 1);
Assert.areEqual(1, responseWrapper[0].matchedNumber);
Assert.areEqual(NUMBER_OF_TEST_RECORDS, responseWrapper[0].totalNumber);

// Clear the requests list for the next test
requests.clear();

// Test counting by boolean field 'DoNotCall'
request.inputCollection = getContacts(NUMBER_OF_TEST_RECORDS);
request.fieldName = 'DoNotCall';
request.fieldValue = 'true';
requests.add(request);

responseWrapper = CountRecordsAndFields.count(requests);
Assert.areEqual(responseWrapper.size(), 1);
Assert.areEqual(3, responseWrapper[0].matchedNumber); // Three records should match (even indices)
Assert.areEqual(NUMBER_OF_TEST_RECORDS, responseWrapper[0].totalNumber);
}

//this is incompatible with recent work done on this class. however, there's a new set of test classes in a separate file, so just going to comment this one out
Expand Down Expand Up @@ -743,7 +764,7 @@ public with sharing class ListActionsTest {

private static List<Contact> getContacts(Integer numberOfRecords) {
return [
SELECT Id, LastName, AccountId
SELECT Id, LastName, AccountId, DoNotCall
FROM Contact
ORDER BY LastName
LIMIT :numberOfRecords
Expand Down

0 comments on commit 0a311e5

Please sign in to comment.