You need to sign in to do that
Don't have an account?
rahul Shukla 37
getting error while fetching relationship field value from metadata
Below is my code
public class ApplicantController {
public List<ApplicantDataWrapper> applicantData { get; set; }
public ApplicantController() {
String fieldLabel;
applicantData = new List<ApplicantDataWrapper>();
// Query the custom metadata records for field mapping
List<FieldMapping__mdt> fieldMappings = [SELECT Object_API_Name__c, Field_API_Name__c, Relationship_Field__c, Lookup_API_Name__c, label FROM FieldMapping__mdt WHERE Object_API_Name__c = 'Co_Applicant__c'];
// Get the API names of the fields to query dynamically
Set<String> fieldAPINames = new Set<String>();
for (FieldMapping__mdt fieldMapping : fieldMappings) {
if (fieldMapping.Object_API_Name__c == 'Co_Applicant__c') {
fieldAPINames.add(fieldMapping.Field_API_Name__c);
System.debug('fieldAPINames adding' + fieldAPINames);
}
}
// Build the dynamic SOQL query
String dynamicQuery = 'SELECT Id';
for (String fieldAPIName : fieldAPINames) {
dynamicQuery += ', ' + fieldAPIName;
System.debug('dynamicQuery adding' + dynamicQuery);
}
dynamicQuery += ' FROM Co_Applicant__c LIMIT 1';
System.debug('dynamicQuery' + dynamicQuery);
// Query the Applicant object dynamically
List<SObject> applicants = Database.query(dynamicQuery);
if (!applicants.isEmpty()) {
Co_Applicant__c applicant = (Co_Applicant__c)applicants[0];
System.debug('applicantd ' + applicant);
// Iterate over the field mappings and retrieve the label and value dynamically
for (FieldMapping__mdt fieldMapping : fieldMappings) {
String objectAPIName = fieldMapping.Object_API_Name__c;
String fieldAPIName = fieldMapping.Field_API_Name__c;
String fieldValue;
if (objectAPIName == 'Co_Applicant__c') {
// Retrieve the label dynamically from the custom metadata records based on the field API name
if (!fieldMapping.Relationship_Field__c) {
fieldLabel = Schema.getGlobalDescribe().get(objectAPIName).getDescribe().fields.getMap().get(fieldAPIName).getDescribe().getLabel();
fieldValue = (applicant != null) ? String.valueOf(applicant.get(fieldAPIName)) : '';
} else {
// Handle lookup field value
String lookupFieldAPIName = fieldMapping.Lookup_API_Name__c;
fieldValue = (applicant != null) ? getLookupFieldValue(lookupFieldAPIName, applicant) : '';
}
// Map field label with value
ApplicantDataWrapper dataWrapper = new ApplicantDataWrapper();
dataWrapper.fieldLabel = fieldLabel;
dataWrapper.fieldValue = fieldValue;
// Add the data to the list
applicantData.add(dataWrapper);
System.debug('applicantData: ' + applicantData);
}
}
}
}
// Method to fetch lookup field value using the --r notation from metadata
public String getLookupFieldValue(String lookupFieldAPIName, Co_Applicant__c applicant) {
if (applicant != null && applicant.get(lookupFieldAPIName) != null) {
String lookupObjectName = lookupFieldAPIName.substringBefore('.');
String lookupFieldName = lookupFieldAPIName.substringAfter('.');
System.debug('lookupObjectName: ' + lookupObjectName);
System.debug('lookupFieldName: ' + lookupFieldName);
// Query the lookup object dynamically
String query = 'SELECT ' + lookupFieldName + ' FROM ' + lookupObjectName +
' WHERE Id = \'' + applicant.get(lookupFieldAPIName) + '\' LIMIT 1';
List<SObject> lookupObjects = Database.query(query);
if (!lookupObjects.isEmpty()) {
SObject lookupObject = lookupObjects[0];
if (lookupObject != null) {
return String.valueOf(lookupObject.get(lookupFieldName));
}
}
}
return '';
}
public class ApplicantDataWrapper {
public String fieldLabel { get; set; }
public String fieldValue { get; set; }
}
}
I want to get the value of lead__r.Name from Co_Applicant__c Object
public class ApplicantController {
public List<ApplicantDataWrapper> applicantData { get; set; }
public ApplicantController() {
String fieldLabel;
applicantData = new List<ApplicantDataWrapper>();
// Query the custom metadata records for field mapping
List<FieldMapping__mdt> fieldMappings = [SELECT Object_API_Name__c, Field_API_Name__c, Relationship_Field__c, Lookup_API_Name__c, label FROM FieldMapping__mdt WHERE Object_API_Name__c = 'Co_Applicant__c'];
// Get the API names of the fields to query dynamically
Set<String> fieldAPINames = new Set<String>();
for (FieldMapping__mdt fieldMapping : fieldMappings) {
if (fieldMapping.Object_API_Name__c == 'Co_Applicant__c') {
fieldAPINames.add(fieldMapping.Field_API_Name__c);
System.debug('fieldAPINames adding' + fieldAPINames);
}
}
// Build the dynamic SOQL query
String dynamicQuery = 'SELECT Id';
for (String fieldAPIName : fieldAPINames) {
dynamicQuery += ', ' + fieldAPIName;
System.debug('dynamicQuery adding' + dynamicQuery);
}
dynamicQuery += ' FROM Co_Applicant__c LIMIT 1';
System.debug('dynamicQuery' + dynamicQuery);
// Query the Applicant object dynamically
List<SObject> applicants = Database.query(dynamicQuery);
if (!applicants.isEmpty()) {
Co_Applicant__c applicant = (Co_Applicant__c)applicants[0];
System.debug('applicantd ' + applicant);
// Iterate over the field mappings and retrieve the label and value dynamically
for (FieldMapping__mdt fieldMapping : fieldMappings) {
String objectAPIName = fieldMapping.Object_API_Name__c;
String fieldAPIName = fieldMapping.Field_API_Name__c;
String fieldValue;
if (objectAPIName == 'Co_Applicant__c') {
// Retrieve the label dynamically from the custom metadata records based on the field API name
if (!fieldMapping.Relationship_Field__c) {
fieldLabel = Schema.getGlobalDescribe().get(objectAPIName).getDescribe().fields.getMap().get(fieldAPIName).getDescribe().getLabel();
fieldValue = (applicant != null) ? String.valueOf(applicant.get(fieldAPIName)) : '';
} else {
// Handle lookup field value
String lookupFieldAPIName = fieldMapping.Lookup_API_Name__c;
fieldValue = (applicant != null) ? getLookupFieldValue(lookupFieldAPIName, applicant) : '';
}
// Map field label with value
ApplicantDataWrapper dataWrapper = new ApplicantDataWrapper();
dataWrapper.fieldLabel = fieldLabel;
dataWrapper.fieldValue = fieldValue;
// Add the data to the list
applicantData.add(dataWrapper);
System.debug('applicantData: ' + applicantData);
}
}
}
}
// Method to fetch lookup field value using the --r notation from metadata
public String getLookupFieldValue(String lookupFieldAPIName, Co_Applicant__c applicant) {
if (applicant != null && applicant.get(lookupFieldAPIName) != null) {
String lookupObjectName = lookupFieldAPIName.substringBefore('.');
String lookupFieldName = lookupFieldAPIName.substringAfter('.');
System.debug('lookupObjectName: ' + lookupObjectName);
System.debug('lookupFieldName: ' + lookupFieldName);
// Query the lookup object dynamically
String query = 'SELECT ' + lookupFieldName + ' FROM ' + lookupObjectName +
' WHERE Id = \'' + applicant.get(lookupFieldAPIName) + '\' LIMIT 1';
List<SObject> lookupObjects = Database.query(query);
if (!lookupObjects.isEmpty()) {
SObject lookupObject = lookupObjects[0];
if (lookupObject != null) {
return String.valueOf(lookupObject.get(lookupFieldName));
}
}
}
return '';
}
public class ApplicantDataWrapper {
public String fieldLabel { get; set; }
public String fieldValue { get; set; }
}
}
I want to get the value of lead__r.Name from Co_Applicant__c Object
To retrieve the value of the lead__r.Name field from the Co_Applicant__c object, you can modify your code as follows:
Update the query in the getLookupFieldValue method to include the lead__r.Name field: Modify the ApplicantDataWrapper class to include a new field for leadName: Within the ApplicantController class, add a new line to set the leadName field value: By adding the leadName field to the ApplicantDataWrapper class and setting its value within the for loop, you can capture the lead__r.Name field value from the Co_Applicant__c object and display it as needed.
If this helps , please mark this as Best Answer.
Thank you.