0

Below is my code where I am describing all the fields of selected object. Instead of fetching ID for reference fields I need to fetch values for example I in case of account Id I want to fetch account name. I have all the reference fields stored in lookUpFields and I have a final list finalRecordList which contains fields and values. So for reference fields I need them to be converted to values. Please help.

@AuraEnabled
    Public static ListMapWrapper getMergeRecord (List<SObject> selecteData, String objectName){
        Map<string, List<dataWrapperClass>> wrapmap = new Map<string, List<dataWrapperClass>>();
        list<String> recordID = new list<String>();
        for(SObject record : selecteData){
            recordID.add(record.id);
        }
        
        Map<String, Schema.SObjectField> objectFields = new Map<String, Schema.SObjectField>();
        List<String> fieldNames = new List<String>();
        List<String> lookUpFields = new List<String>();
        Schema.SObjectType convertType = Schema.getGlobalDescribe().get(ObjectName);
        Schema.DescribeSObjectResult r = convertType.getDescribe();    
        objectFields = Schema.getGlobalDescribe().get(ObjectName).getDescribe().fields.getMap();
        system.debug('objectFields : '+objectFields);
        
        if(objectName == 'Account' || objectName == 'Opportunity' || objectName == 'Case'){
            for(String fieldName: objectFields.keySet()){
                string datatypeval = String.valueOf(r.fields.getMap().get(fieldName).getDescribe().getType());
               // system.debug('datatypeval : '+datatypeval);
                if(datatypeval == 'REFERENCE' || datatypeval == 'reference'){
                    lookUpFields.add(objectFields.get(fieldName).getDescribe().getName());
                    system.debug('lookUpFields : '+lookUpFields);
                }
                if(fieldName != 'ShippingAddress' && fieldName != 'BillingAddress' && fieldName != 'Description' && fieldName != 'MasterRecordId' && fieldName != 'Jigsaw' ){
                       fieldNames.add(objectFields.get(fieldName).getDescribe().getName());
                   }
          
            } 
        }
        else if(objectName == 'Lead' || objectName == 'Contact'){
            for(String fieldName: objectFields.keySet()){
                string datatypeval = String.valueOf(r.fields.getMap().get(fieldName).getDescribe().getType());
                if(datatypeval == 'REFERENCE' || datatypeval == 'reference'){
                    lookUpFields.add(objectFields.get(fieldName).getDescribe().getName());
                    system.debug('lookUpFields : '+lookUpFields);
                }
                if(fieldName != 'Name' && fieldName != 'ShippingAddress' && fieldName != 'BillingAddress' && fieldName != 'Description' && fieldName != 'MasterRecordId' && fieldName != 'Jigsaw'){
                       fieldNames.add(objectFields.get(fieldName).getDescribe().getName());
                   }
                
            } 
        }

        List<String> fieldlist = new List<String>();
        Set<String> aPINamesSet = objectFields.keyset();
        fieldlist.addAll(aPINamesSet);
        system.debug('fieldlist : '+fieldlist);
        
        string soqlQueryCurrent = string.join(fieldlist,',');
        
        if(soqlQueryCurrent.endsWithIgnoreCase(',')){
            soqlQueryCurrent = soqlQueryCurrent.removeEndIgnoreCase(',');                  
        }
        String queryStr ='SELECT '+soqlQueryCurrent+' FROM '+ObjectName+' where Id =:recordID';                
        List <Sobject> finalRecordList = Database.query(String.escapeSingleQuotes(queryStr));
        system.debug('finalRecordList : '+finalRecordList);
2

In order to convert a Lookup field to a relationship and then get the Name for the related object you need to do something like:

objectFields.get(fieldName).getDescribe().getRelationshipName() + '.Name'

This will convert "AccountId" to "Account.Name", or "MyObject__c" to "MyObject__r.Name".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.