From 0e28a2d9565762355fd12444ff2e24d1781fa7cd Mon Sep 17 00:00:00 2001 From: David Pickett Date: Wed, 10 Jul 2024 14:51:15 -1000 Subject: [PATCH] Add best practice suggestions from sf codescanner --- .../default/classes/FieldLabelController.cls | 21 +++++++++++++++++-- .../classes/FieldLabelController_Test.cls | 21 +++++++++++++++---- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/force-app/main/default/classes/FieldLabelController.cls b/force-app/main/default/classes/FieldLabelController.cls index 6821bd7..57953ec 100644 --- a/force-app/main/default/classes/FieldLabelController.cls +++ b/force-app/main/default/classes/FieldLabelController.cls @@ -1,4 +1,16 @@ +/** + * @description FieldLabelController class assists with retrieving field labels + * for a given object and field API name. And includes a helper function to get + * the sObjectType of a given record. + */ public with sharing class FieldLabelController { + /** + * @description getFieldLabels retrieves field labels + * for a given object and field API name + * @param objectName Input object name + * @param fieldApiName Input field API name + * @return String label name + */ @AuraEnabled(cacheable=true) public static String getFieldLabels( String objectName, @@ -6,7 +18,6 @@ public with sharing class FieldLabelController { ) { String label = null; try { - List fieldLabels = new List(); Map fieldMap = Schema.getGlobalDescribe() .get(objectName) .getDescribe() @@ -18,13 +29,19 @@ public with sharing class FieldLabelController { return label; } + /** + * @description getSObjectType retrieves the sObjectType + * for a given record + * @param record sObject input record + * @return String sObjectType Name + */ @AuraEnabled(cacheable=true) public static String getSObjectType(sObject record) { String sObjectType = null; try { Id recordId = record.Id; sObjectType = recordId.getSObjectType().getDescribe().getName(); - System.debug('sObjectType: ' + sObjectType); + //System.debug(System.info,'sObjectType: ' + sObjectType); } catch (Exception e) { throw new AuraHandledException(e.getMessage()); } diff --git a/force-app/main/default/classes/FieldLabelController_Test.cls b/force-app/main/default/classes/FieldLabelController_Test.cls index c472588..6f49361 100644 --- a/force-app/main/default/classes/FieldLabelController_Test.cls +++ b/force-app/main/default/classes/FieldLabelController_Test.cls @@ -7,7 +7,11 @@ public class FieldLabelController_Test { 'Account', fieldApiName ); - System.assertEquals('Created Date', fieldLabel); + System.assertEquals( + 'Created Date', + fieldLabel, + 'Field label does not match' + ); } @isTest static void labelException() { @@ -17,8 +21,13 @@ public class FieldLabelController_Test { 'Account', fieldApiName ); + System.assertEquals( + 'Created Date', + fieldLabel, + 'This should have thrown an exception' + ); } catch (Exception ex) { - System.assert(true); + System.assert(true, 'Exception was thrown'); } } @@ -28,9 +37,13 @@ public class FieldLabelController_Test { try { //insert acct; String sObjectApiName = FieldLabelController.getSObjectType(acct); - System.assertEquals('Account', sObjectApiName); + System.assertEquals( + 'Account', + sObjectApiName, + 'sObject type name does not match' + ); } catch (Exception ex) { - System.assert(true); + System.assert(true, 'Exception was thrown'); } } }