Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Related List custom component #2119

Merged
merged 2 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
public with sharing class TAG_RelatedListController {
/**
* @description: Returns a list of records
* @return List<sObject>
**/
@AuraEnabled(cacheable=true)
public static List<sObject> getCRMRelatedList(
String parentId,
String objectApiName,
String relationField,
String parentRelationField,
String parentObjectApiName,
String dateField,
String filterConditions,
String orderConditions
) {
Set<String> parentRelationIds = getParentRelation(parentId, parentRelationField, parentObjectApiName);
if (parentRelationIds.isEmpty()) {
return new List<SObject>();
}

String query = 'SELECT Id' + (String.isBlank(dateField) ? '' : ', ' + dateField);
query += ' FROM ' + objectApiName + ' WHERE ';
query += String.isNotBlank(filterConditions) ? filterConditions.trim() + ' AND ' : '';
// query += relationField + ' IN (SELECT ' + parentRelationField + ' FROM ' + parentObjectApiName + ' WHERE Id = \'' + parentId + '\')';
query += relationField + ' IN :parentRelationIds ';
query += 'ORDER BY ' + (orderConditions != null ? orderConditions : 'ID DESC');

List<sObject> returnList = Database.query(query);
return returnList;
}

@AuraEnabled
public static List<sObject> getRelatedList(
List<String> fieldNames,
String parentId,
String objectApiName,
String relationField,
String parentRelationField,
String parentObjectApiName,
String filterConditions
) {
Set<String> parentRelationIds = getParentRelation(parentId, parentRelationField, parentObjectApiName);
String query = 'SELECT ';

//Appending fields to query string
for (String field : fieldNames) {
query += field + ', ';
}

query = query.removeEndIgnoreCase(', ') + ' FROM ' + objectApiName + ' WHERE ';
query += String.isNotBlank(filterConditions) ? filterConditions.trim() + ' AND ' : '';
// query += relationField + ' IN (SELECT ' + parentRelationField + ' FROM ' + parentObjectApiName + ' WHERE Id = \'' + parentId + '\')';
query += relationField + ' IN :parentRelationIds ORDER BY ID DESC';

List<sObject> returnList = Database.query(query);
return returnList;
}

/**
* @description: Returns a set of Strings to be used in getRelatedList
* @return Set<String>
*/
private static Set<String> getParentRelation(
String parentId,
String parentRelationField,
String parentObjectApiName
) {
Set<String> parentRelationIds = new Set<String>();
String query = 'SELECT ' + parentRelationField + ' FROM ' + parentObjectApiName + ' WHERE Id = :parentId';

String relationId;
for (SObject sObj : Database.query(query)) {
relationId = getParentRelationId(sObj, parentRelationField);
if (relationId != null)
parentrelationIds.add(relationId);
}

return parentRelationIds;
}

/**
* @description: Get the parent relation Id from sObject. Will recursively walk through the field hierarchy
* @return String
*/
private static String getParentRelationId(Sobject obj, String parentRelationField) {
List<String> relationHierarchy = parentRelationField.split('\\.');
String fieldApiName = relationHierarchy.remove(0);

if (relationHierarchy.isEmpty()) {
return (String) obj.get(fieldApiName);
} else {
return getParentRelationId(obj.getSObject(fieldApiName), String.join(relationHierarchy, '.'));
}

}
}
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>62.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.headerContainer {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0.25rem 0.5rem;
}

.headerLeft {
display: flex;
align-items: center;
cursor: pointer;
}

.headerIcon {
width: 1.5rem;
height: 1.5rem;
}

.headerTitle {
margin: 0 0.5rem;
line-height: 1.5rem;
}

.headerToggle {
margin-left: 0.5rem;
line-height: 1.5rem;
}

.newRecordButton {
align-self: center;
height: 2rem;
padding: 0 0.5rem;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<template>
<article class="slds-card">
<!-- HEADER WITH TOGGLE AND "NEW RECORD" BUTTON -->
<div class="headerContainer" style={headerBackground}>
<!-- Left Group: Icon, Title, Toggle Icon -->
<div class="headerLeft" onclick={toggleAccordion}>
<template if:true={icon}>
<lightning-icon icon-name={icon} size="small" class="headerIcon slds-m-right_x-small"></lightning-icon>
</template>
<h1 class="headerTitle slds-text-heading_small">{cardTitle}</h1>
<span class="headerToggle">{toggleIcon}</span>
</div>
<!-- Right Group: New Record Button -->
<template if:true={showNewRecordButton}>
<lightning-button
label={newRecordButtonLabel}
variant="brand"
onclick={handleNewRecord}
class="newRecordButton slds-m-left_small">
</lightning-button>
</template>
</div>

<!-- TABLE RENDERED BASED ON EXPANSION STATE -->
<template if:true={showRecords}>
<div style="overflow:auto; max-height: {maxHeight}em;">
<table class="slds-table slds-table_cell-buffer slds-table_bordered slds-max-medium-table_stacked-horizontal" style="table-layout: auto;">
<thead>
<tr class="slds-line-height_reset">
<template for:each={fieldLabels} for:item="label">
<th key={label.value} scope="col" style={label.headerStyle}>
<div style="display: flex; align-items: center; height: 100%; font-size: 0.75rem; white-space: nowrap;" title={label.value}>
{label.value}
</div>
</th>
</template>
</tr>
</thead>
<tbody>
<template for:each={displayedRecords} for:item="record" for:index="rowIndex">
<!-- For clickable rows, added mouse event handlers for popover -->
<template if:true={clickableRows}>
<tr key={record.Id} data-record-id={record.Id} data-value={rowIndex} class="slds-hint-parent" onclick={handleRowClick} style="cursor: pointer"
onmouseenter={handleMouseEnter} onmouseleave={handleMouseLeave}>
<template for:each={record.recordFields} for:item="field">
<td key={field.label}>
<div class="slds-truncate slds-cell-wrap">{field.value}</div>
</td>
</template>
</tr>
</template>
<!-- For non-clickable rows, also added mouse event handlers for popover -->
<template if:false={clickableRows}>
<tr key={record.Id} data-record-id={record.Id} data-value={rowIndex} class="slds-hint-parent"
onmouseenter={handleMouseEnter} onmouseleave={handleMouseLeave}>
<template for:each={record.recordFields} for:item="field">
<td key={field.label}>
<div class="slds-truncate slds-cell-wrap">{field.value}</div>
</td>
</template>
</tr>
</template>
</template>
</tbody>
</table>
</div>
</template>

<!-- Popover Template for additional record details -->
<template if:true={showPopover}>
<div class="slds-popover slds-nubbin_top" style={popoverStyle} onmouseleave={handleMouseLeave}>
<div class="slds-popover__body">
<template for:each={popoverFieldValues} for:item="fieldObj">
<p key={fieldObj.apiName}>
<strong>{fieldObj.apiName}:</strong> {fieldObj.value}
</p>
</template>
</div>
</div>
</template>
</article>
</template>
Loading
Loading