+ Start a Discussion
DEV_CGDEV_CG 

Unable to fetch the related object field data in batch apex

Select id, firstname, account.name, account.type from contact
i passed this query into start method, when I checked scope list of execute method I couldn't see any files related to account ..how to fetch them
NagendraNagendra (Salesforce Developers) 
Hi,

Sorry for this issue you are facing.

May I request you please post the complete code snippet of what you have tried so that we can look into it and can help you accordingly.

Happy to help further.

Regards,
Nagendra
DEV_CGDEV_CG
Hi Nagendra,

see the below code, When I exceute batch is unable to recognize the account fileds

global class SampleBatch implements database.Batchable<sobject>{
    
    global string query='select id,firstname,accountID,account.name,account.type,account.industry from contact';
    global database.QueryLocator start(Database.Batchablecontext bc)
    {
        return database.getQueryLocator(query);
    }
    global void execute(Database.Batchablecontext bc, List<Sobject> scope)
    {
        List<String> queryFields;
        queryFields=new List<String>{'id','firstname','accountId','account.name','account.type','account.industry'};
            String csvFileString = ''; 
           for(String fieldName : queryFields)
            {
            csvFileString = csvFileString+' '+ fieldName;
            } 
      csvFileString=csvFileString.replaceAll(' ',',').replaceFirst(',','')+'\n';
       System.debug(csvFileString);
        for(SObject obj : scope)
         {
            String fieldValue='';
                for(String fieldName : queryFields)
                {   
                fieldValue = fieldValue +','+ obj.get(fieldName);
                System.debug('fieldvalue@@@@@ '+obj.get(fieldName));
                }
            fieldValue=fieldValue.replaceFirst(',','');
             csvFileString = csvFileString + fieldValue + '\n';
        }
         
    }
    global void finish(Database.Batchablecontext bc)
    {
        
    }

}
DEV_CGDEV_CG
actully my requirement is to get all filed values of the queried filed of Sobjects we are working with..(we need mention likst list<Sobject> scope only...not like List<Contact> scope   in execute method)...
and I mentioned the Queryfileds manually becoz, I am uable get the queired fields of Sobject dyanamically...