Skip to content

Commit

Permalink
Add best practice suggestions from sf codescanner
Browse files Browse the repository at this point in the history
  • Loading branch information
pickettd committed Jul 11, 2024
1 parent 42b7ab1 commit 0e28a2d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
21 changes: 19 additions & 2 deletions force-app/main/default/classes/FieldLabelController.cls
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
/**
* @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,
String fieldApiName
) {
String label = null;
try {
List<String> fieldLabels = new List<String>();
Map<String, Schema.SObjectField> fieldMap = Schema.getGlobalDescribe()
.get(objectName)
.getDescribe()
Expand All @@ -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());
}
Expand Down
21 changes: 17 additions & 4 deletions force-app/main/default/classes/FieldLabelController_Test.cls
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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');
}
}

Expand All @@ -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');
}
}
}

0 comments on commit 0e28a2d

Please sign in to comment.