diff --git a/force-app/main/default/classes/CollectionGetFirstRecord_Records.cls b/force-app/main/default/classes/CollectionGetFirstRecord_Records.cls index 8d31523..550f24f 100644 --- a/force-app/main/default/classes/CollectionGetFirstRecord_Records.cls +++ b/force-app/main/default/classes/CollectionGetFirstRecord_Records.cls @@ -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); } @@ -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; } } \ No newline at end of file diff --git a/force-app/main/default/classes/CollectionGetFirstRecord_RecordsTest.cls b/force-app/main/default/classes/CollectionGetFirstRecord_RecordsTest.cls index a088820..c489498 100644 --- a/force-app/main/default/classes/CollectionGetFirstRecord_RecordsTest.cls +++ b/force-app/main/default/classes/CollectionGetFirstRecord_RecordsTest.cls @@ -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{ acc, con }; + + List requests = new List{ req }; + + Test.startTest(); + List 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