diff --git a/force-app/main/default/classes/CollectionGetFirstValue_Records.cls b/force-app/main/default/classes/CollectionGetFirstValue_Records.cls index 65c2537..eb05e7d 100644 --- a/force-app/main/default/classes/CollectionGetFirstValue_Records.cls +++ b/force-app/main/default/classes/CollectionGetFirstValue_Records.cls @@ -14,6 +14,13 @@ public with sharing class CollectionGetFirstValue_Records { } SObject firstRecord = req.records[0]; + + if (firstRecord == null || String.isBlank(req.fieldApiName)) { + resp.response = null; + results.add(resp); + continue; + } + Object fieldValue = firstRecord.get(req.fieldApiName); resp.response = (fieldValue == null) ? null : String.valueOf(fieldValue); diff --git a/force-app/main/default/classes/CollectionGetFirstValue_RecordsTest.cls b/force-app/main/default/classes/CollectionGetFirstValue_RecordsTest.cls index 4008110..cb9d3b6 100644 --- a/force-app/main/default/classes/CollectionGetFirstValue_RecordsTest.cls +++ b/force-app/main/default/classes/CollectionGetFirstValue_RecordsTest.cls @@ -49,4 +49,50 @@ public with sharing class CollectionGetFirstValue_RecordsTest { System.assertEquals(null, responses[0].response, 'Null records should result in null response'); } + @IsTest + static void testGetFirstValue_NullFirstRecord() { + CollectionGetFirstValue_Records.Request req = new CollectionGetFirstValue_Records.Request(); + req.records = new List{ null }; + req.fieldApiName = 'Name'; + + List requests = new List{ req }; + + List responses = CollectionGetFirstValue_Records.getFirstValue(requests); + + System.assertEquals(1, responses.size()); + System.assertEquals(null, responses[0].response, 'Null first record should result in null response'); + } + + @IsTest + static void testGetFirstValue_NullFieldApiName() { + Account a = new Account(Name = 'Test Account'); + + CollectionGetFirstValue_Records.Request req = new CollectionGetFirstValue_Records.Request(); + req.records = new List{ a }; + req.fieldApiName = null; + + List requests = new List{ req }; + + List responses = CollectionGetFirstValue_Records.getFirstValue(requests); + + System.assertEquals(1, responses.size()); + System.assertEquals(null, responses[0].response, 'Null fieldApiName should result in null response'); + } + + @IsTest + static void testGetFirstValue_BlankFieldApiName() { + Account a = new Account(Name = 'Test Account'); + + CollectionGetFirstValue_Records.Request req = new CollectionGetFirstValue_Records.Request(); + req.records = new List{ a }; + req.fieldApiName = ' '; + + List requests = new List{ req }; + + List responses = CollectionGetFirstValue_Records.getFirstValue(requests); + + System.assertEquals(1, responses.size()); + System.assertEquals(null, responses[0].response, 'Blank fieldApiName should result in null response'); + } + } \ No newline at end of file