0% found this document useful (0 votes)
8 views1 page

Apex Class

The RelatedRecordsService class provides a method to retrieve related records from a specified child object in Salesforce. It takes parameters for the parent record ID, child object API name, relationship field, and field set name, and constructs a dynamic SOQL query to fetch the relevant fields. The results are returned in a map containing the records and field names.

Uploaded by

champion202547
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

Apex Class

The RelatedRecordsService class provides a method to retrieve related records from a specified child object in Salesforce. It takes parameters for the parent record ID, child object API name, relationship field, and field set name, and constructs a dynamic SOQL query to fetch the relevant fields. The results are returned in a map containing the records and field names.

Uploaded by

champion202547
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

public with sharing class RelatedRecordsService {

@AuraEnabled(cacheable=true, userMode=true)
public static Map<String, Object> getRelatedRecords(
Id parentId,
String childObjectApiName,
String relationshipField,
String fieldSetName
) {
Map<String, Object> result = new Map<String, Object>();

// Retrieve field set fields


Schema.SObjectType objType =
Schema.getGlobalDescribe().get(childObjectApiName);
if (objType == null) {
throw new AuraHandledException('Invalid object API name');
}

Schema.DescribeSObjectResult describeResult = objType.getDescribe();


List<String> fieldNames = new List<String>();
for (Schema.FieldSetMember f :
describeResult.fieldSets.getMap().get(fieldSetName).getFields()) {
fieldNames.add(f.getFieldPath());
}

String query = 'SELECT Id, ' + String.join(fieldNames, ', ') +


' FROM ' + childObjectApiName +
' WHERE ' + relationshipField + ' = :parentId';

List<SObject> records = Database.query(query);

result.put('records', records);
result.put('fields', fieldNames);
return result;
}
}

You might also like