Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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 @@ -12,6 +12,22 @@ public with sharing class CollectionGetFirstRecord_Records {
continue;
}

Schema.SObjectType expectedType = req.records[0] != null ? req.records[0].getSObjectType() : null;
Boolean mixedTypes = false;
for (SObject record : req.records) {
if (record == null || record.getSObjectType() != expectedType) {
mixedTypes = true;
break;
}
}

if (mixedTypes) {
resp.record = null;
resp.errorMessage = 'Mixed SObject types detected in the input list. All records must be of the same SObject type.';
results.add(resp);
continue;
}

resp.record = req.records[0];
results.add(resp);
}
Expand All @@ -27,5 +43,8 @@ public with sharing class CollectionGetFirstRecord_Records {
public class Response {
@InvocableVariable(label='Record' description='The first record from the provided collection')
public SObject record;

@InvocableVariable(label='Error Message' description='Error message when records contain mixed SObject types')
public String errorMessage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@ private class CollectionGetFirstRecord_RecordsTest {
System.assertEquals(a1.Name, returned.Name, 'Name should match the first account');
}

@isTest
static void testGetFirstRecord_MixedSObjectTypesReturnsNullWithError() {
// Create test records of different SObject types
Account acc = new Account(Name='Test Account');
insert acc;
Contact con = new Contact(LastName='Test Contact', AccountId=acc.Id);
insert con;

CollectionGetFirstRecord_Records.Request req = new CollectionGetFirstRecord_Records.Request();
req.records = new List<SObject>{ acc, con };

List<CollectionGetFirstRecord_Records.Request> requests = new List<CollectionGetFirstRecord_Records.Request>{ req };

Test.startTest();
List<CollectionGetFirstRecord_Records.Response> responses = CollectionGetFirstRecord_Records.getFirstRecord(requests);
Test.stopTest();

System.assertEquals(1, responses.size(), 'Should return one response');
System.assertEquals(null, responses[0].record, 'Mixed types should yield null record');
System.assertNotEquals(null, responses[0].errorMessage, 'Mixed types should set an error message');
}

@isTest
static void testGetFirstRecord_EmptyAndNullListsReturnNull() {
// Empty list
Expand Down