-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8604b3c
commit 3c96d8e
Showing
27 changed files
with
3,434 additions
and
16 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
force-app/main/default/classes/ObjectFieldSelectorController.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
public with sharing class ObjectFieldSelectorController { | ||
|
||
public static final String STANDARD = 'standard'; | ||
public static final String SPECIFIC = 'specific'; | ||
public static final String CUSTOM = 'custom'; | ||
public static final String BOTH = 'both'; | ||
public static final String ANCILLARY = 'ancillary'; | ||
public static final String ALL = 'all'; | ||
|
||
@AuraEnabled | ||
public static GetObjectsResult getObjects(String selectionType, List<String> availableObjects) { | ||
if (String.isBlank(selectionType)) | ||
selectionType = BOTH; | ||
selectionType = selectionType.toLowerCase(); | ||
GetObjectsResult result = new GetObjectsResult(); | ||
result.objects = new List<ObjectResult>(); | ||
List<Schema.DescribeSObjectResult> describeResults = new List<Schema.DescribeSObjectResult>(); | ||
if (selectionType == ALL) { | ||
Map<String, Schema.SObjectType> objMap = Schema.getGlobalDescribe(); | ||
for (Schema.SObjectType objType : objMap.values()) { | ||
describeResults.add(objType.getDescribe()); | ||
} | ||
} else if (selectionType == SPECIFIC) { | ||
describeResults = Schema.describeSObjects(availableObjects); | ||
} else { | ||
List<String> objectNames = new List<String>(); | ||
List<EntityDefinition> entityDefs = new List<EntityDefinition>(); | ||
if (selectionType == STANDARD || selectionType == BOTH) { | ||
entityDefs.addAll([SELECT KeyPrefix, QualifiedApiName, DeveloperName FROM EntityDefinition WHERE (NOT QualifiedApiName LIKE '%__c') AND (NOT QualifiedApiName LIKE '%Feed') AND (NOT QualifiedApiName LIKE '%Tag') AND (NOT QualifiedApiName LIKE '%Share') AND (NOT QualifiedApiName LIKE '%ChangeEvent') AND (NOT QualifiedApiName LIKE '%History')]); | ||
} | ||
if (selectionType == CUSTOM || selectionType == BOTH) { | ||
entityDefs.addAll([SELECT QualifiedApiName FROM EntityDefinition WHERE QualifiedApiName LIKE '%__c']); | ||
} | ||
if (selectionType == ANCILLARY) { | ||
entityDefs.addAll([SELECT QualifiedApiName, DeveloperName FROM EntityDefinition WHERE QualifiedApiName LIKE '%Feed' OR QualifiedApiName LIKE '%Tag' OR QualifiedApiName LIKE '%Share' OR QualifiedApiName LIKE '%ChangeEvent' OR QualifiedApiName LIKE '%History']); | ||
} | ||
for (EntityDefinition def : entityDefs) { | ||
// The standard list of EntityDefinitions may still return some odd types like metadata, so we filter out any object with double underscores | ||
if (selectionType != STANDARD || !def.QualifiedApiName.contains('__')) { | ||
objectNames.add(def.QualifiedApiName); | ||
} | ||
} | ||
describeResults = Schema.describeSObjects(objectNames); | ||
} | ||
for (Schema.DescribeSObjectResult res : describeResults) { | ||
result.objects.add(new ObjectResult(res.getLabel(), res.getName())); | ||
} | ||
return result; | ||
} | ||
|
||
@AuraEnabled(cacheable=true) | ||
public static GetObjectFieldsResult getObjectFields(String objectName) { | ||
GetObjectFieldsResult result = new GetObjectFieldsResult(); | ||
result.fields = new List<FieldResult>(); | ||
try { | ||
Map<String, Schema.SObjectField> tokenMap = ((SObject)Type.forName('Schema', objectName).newInstance()).getSObjectType().getDescribe().fields.getMap(); | ||
for (Schema.SObjectField objField : tokenMap.values()) { | ||
FieldResult newField = new FieldResult(objField.getDescribe()); | ||
System.debug(newField); | ||
result.fields.add(newField); | ||
} | ||
} catch (Exception e) { | ||
result.errorMessage = e.getMessage(); | ||
return result; | ||
} | ||
System.debug('about to return result, with '+ result.fields.size() +' fields'); | ||
return result; | ||
} | ||
|
||
public class GetObjectsResult { | ||
@AuraEnabled public List<ObjectResult> objects; | ||
} | ||
|
||
public class ObjectResult { | ||
@AuraEnabled public String label; | ||
@AuraEnabled public String value; | ||
|
||
public ObjectResult(String label, String value) { | ||
this.label = label; | ||
this.value = value; | ||
} | ||
} | ||
|
||
public class GetObjectFieldsResult { | ||
@AuraEnabled public String errorMessage; | ||
@AuraEnabled public List<FieldResult> fields; | ||
} | ||
|
||
public class FieldResult { | ||
@AuraEnabled public String apiName; | ||
@AuraEnabled public String label; | ||
@AuraEnabled public String dataType; | ||
@AuraEnabled public List<ReferenceToInfo> referenceToInfos; | ||
|
||
public FieldResult(Schema.DescribeFieldResult fieldResult) { | ||
this.apiName = fieldResult.getName(); | ||
this.label = fieldResult.getLabel(); | ||
this.dataType = fieldResult.getType().name(); | ||
List<ReferenceToInfo> refToInfos = new List<ReferenceToInfo>(); | ||
for (Schema.sObjectType objType : fieldResult.getReferenceTo()) { | ||
refToInfos.add(new ReferenceToInfo(objType.getDescribe().getName())); | ||
} | ||
this.referenceToInfos = refToInfos; | ||
} | ||
} | ||
|
||
public class ReferenceToInfo { | ||
@AuraEnabled public String apiName; | ||
|
||
public ReferenceToInfo(String apiName) { | ||
this.apiName = apiName; | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
force-app/main/default/classes/ObjectFieldSelectorController.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>54.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
18 changes: 18 additions & 0 deletions
18
force-app/main/default/classes/ObjectFieldSelectorControllerTest.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
@isTest | ||
public class ObjectFieldSelectorControllerTest { | ||
|
||
@isTest | ||
public static void testOFSController() { | ||
ObjectFieldSelectorController.GetObjectsResult result1 = ObjectFieldSelectorController.getObjects('all', new List<String>()); | ||
ObjectFieldSelectorController.GetObjectsResult result2 = ObjectFieldSelectorController.getObjects('both', new List<String>()); | ||
ObjectFieldSelectorController.GetObjectsResult result3 = ObjectFieldSelectorController.getObjects('specific', new List<String>{'Account', 'Opportunity'}); | ||
System.assertEquals(result3.objects.size(), 2); | ||
System.assert(result3.objects.size() < result2.objects.size()); | ||
System.assert(result2.objects.size() < result1.objects.size()); | ||
|
||
ObjectFieldSelectorController.GetObjectFieldsResult result4 = ObjectFieldSelectorController.getObjectFields('Account'); | ||
ObjectFieldSelectorController.GetObjectFieldsResult result5 = ObjectFieldSelectorController.getObjectFields('NotARealObject'); | ||
System.assert(String.isBlank(result4.errorMessage)); | ||
System.assert(!String.isBlank(result5.errorMessage)); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
force-app/main/default/classes/ObjectFieldSelectorControllerTest.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>54.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.disabledCursor, .disabledCursor ~ .slds-button { | ||
cursor: not-allowed; | ||
} |
Oops, something went wrong.