function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
rahul Shukla 37rahul 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
SubratSubrat (Salesforce Developers) 
Hello Rahul ,

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:
String query = 'SELECT lead__r.Name FROM ' + lookupObjectName +
               ' WHERE Id = \'' + applicant.get(lookupFieldAPIName) + '\' LIMIT 1';
Modify the ApplicantDataWrapper class to include a new field for leadName:
public class ApplicantDataWrapper {
    public String fieldLabel { get; set; }
    public String fieldValue { get; set; }
    public String leadName { get; set; } // Add this line
}
Within the ApplicantController class, add a new line to set the leadName field value:
// Map field label with value
ApplicantDataWrapper dataWrapper = new ApplicantDataWrapper();
dataWrapper.fieldLabel = fieldLabel;
dataWrapper.fieldValue = fieldValue;

// Retrieve the lead__r.Name value
if (fieldAPIName == 'lead__r.Name') {
    dataWrapper.leadName = fieldValue;
}

// Add the data to the list
applicantData.add(dataWrapper);
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.