Skip to content

Commit 192e198

Browse files
author
mickaelgudin
committed
fixes #5 : getIconName called only if no icon is specified + add class IconUtils (logic separation)
1 parent c64dc0e commit 192e198

File tree

5 files changed

+44
-30
lines changed

5 files changed

+44
-30
lines changed

main/default/classes/IconUtils.cls

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public class IconUtils {
2+
3+
public static String getIconName(String sObjectName){
4+
String u;
5+
List<Schema.DescribeTabSetResult> tabSetDesc = Schema.describeTabs();
6+
List<Schema.DescribeTabResult> tabDesc = new List<Schema.DescribeTabResult>();
7+
List<Schema.DescribeIconResult> iconDesc = new List<Schema.DescribeIconResult>();
8+
9+
for(Schema.DescribeTabSetResult tsr : tabSetDesc) { tabDesc.addAll(tsr.getTabs()); }
10+
11+
for(Schema.DescribeTabResult tr : tabDesc) {
12+
if( sObjectName == tr.getSobjectName() ) {
13+
if( tr.isCustom() == true ) {
14+
iconDesc.addAll(tr.getIcons());
15+
} else {
16+
u = 'standard:' + sObjectName.toLowerCase();
17+
}
18+
}
19+
}
20+
for (Schema.DescribeIconResult ir : iconDesc) {
21+
if (ir.getContentType() == 'image/svg+xml'){
22+
u = 'custom:' + ir.getUrl().substringBetween('custom/','.svg').substringBefore('_');
23+
break;
24+
}
25+
}
26+
27+
return u;
28+
}
29+
30+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>48.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>

main/default/classes/RelatedList.cls

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ public with sharing class RelatedList {
1414

1515

1616
@AuraEnabled
17-
public static ListResults getRecords(String soql, String SObjectName) {
18-
return new ListResults(Database.query(soql), getIconName(SObjectName));
17+
public static ListResults getRecords(String soql, String SObjectName, String iconName) {
18+
//autoretrieve of the icon name based on sobject name if no icon is specified
19+
if(String.isBlank(iconName) ) {
20+
iconName = IconUtils.getIconName(SObjectName);
21+
}
22+
23+
return new ListResults(Database.query(soql), iconName);
1924
}
2025

2126
@AuraEnabled(cacheable=true)
@@ -30,30 +35,4 @@ public with sharing class RelatedList {
3035
return 0;
3136
}
3237

33-
public static String getIconName(String sObjectName){
34-
String u;
35-
List<Schema.DescribeTabSetResult> tabSetDesc = Schema.describeTabs();
36-
List<Schema.DescribeTabResult> tabDesc = new List<Schema.DescribeTabResult>();
37-
List<Schema.DescribeIconResult> iconDesc = new List<Schema.DescribeIconResult>();
38-
39-
for(Schema.DescribeTabSetResult tsr : tabSetDesc) { tabDesc.addAll(tsr.getTabs()); }
40-
41-
for(Schema.DescribeTabResult tr : tabDesc) {
42-
if( sObjectName == tr.getSobjectName() ) {
43-
if( tr.isCustom() == true ) {
44-
iconDesc.addAll(tr.getIcons());
45-
} else {
46-
u = 'standard:' + sObjectName.toLowerCase();
47-
}
48-
}
49-
}
50-
for (Schema.DescribeIconResult ir : iconDesc) {
51-
if (ir.getContentType() == 'image/svg+xml'){
52-
u = 'custom:' + ir.getUrl().substringBetween('custom/','.svg').substringBefore('_');
53-
break;
54-
}
55-
}
56-
57-
return u;
58-
}
5938
}

main/default/classes/RelatedList_Test.cls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public with sharing class RelatedList_Test {
66
User testUser = [Select id from user where id = :UserInfo.getUserId()];
77
RelatedList.ListResults results = null;
88
System.runAs(testUser){
9-
results = RelatedList.getRecords('Select id from contact limit 10', 'Contact');
9+
results = RelatedList.getRecords('Select id from contact limit 10', 'Contact', '');
1010
}
1111

1212
//checking if the SObject icon name is well retrieved

main/default/lwc/lwcRelatedList/lwcRelatedList.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default class LightningDatatable extends NavigationMixin(
5353
}
5454

5555
fetchRecords() {
56-
getRecords({ soql: this.soql, SObjectName: this.objectName })
56+
getRecords({ soql: this.soql, SObjectName: this.objectName, iconName: this.iconName })
5757
.then((data) => {
5858
if (data) {
5959
if( !this.iconName) {

0 commit comments

Comments
 (0)