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
William Roach 9William Roach 9 

Extracting Information From Nested SOQL Statements in Batch

I am trying to create a batchable program that acts like a roll up helper every night on the account object. In this particular example I am trying to count the number of related activities attached to an account or account child object. I am able to pull my information in a query but when I try to then extract that informaiton I keep getting errors saying the variables I am pulling dont exist. Can someone take a look and see if I have just made a clerical error in my dot notation or someting like that? I am sure the values are returned because ive tested everything in the Query Editor before transitioning it to my code. Here is the program, query and current errors:

APEX CODE:
global class NightlyAccountFieldUpdater implements Database.Batchable<SObject>{

    global Database.QueryLocator start(Database.BatchableContext BC){
        String query = '[SELECT id, Last_Sales_Contact_Date__c,Next_Sales_Activity_Date__c, OwnerId,';
        query += ' (SELECT OwnerId, ActivityDate, ActivitySubType, EndDateTime, IsClosed, IsDeleted, PrimaryWhoId FROM ActivityHistories), '; 
        query+= ' (SELECT OwnerId, ActivityDate, ActivitySubType, WhoId, WhatId FROM OpenActivities) FROM ACCOUNT]';
        return Database.getQueryLocator(query);
    }
    
    global static void execute(Database.BatchableContext BC, List<Account> scope){
        List<Lead> LeadActivityList = new  List<Lead>([SELECT id, Account__c, 
                                                       (SELECT OwnerId, ActivityDate, ActivitySubType, EndDateTime, IsClosed, IsDeleted, 
                                                        PrimaryWhoId FROM ActivityHistories), 
                                                       (SELECT OwnerId, ActivityDate, ActivitySubType, WhoId, WhatId FROM OpenActivities) 
                                                       FROM LEAD WHERE lead.Account__C != NULL]);
        List<Account> masterAccountList = new List<Account>();
        Map<Account, List<Activity>> accountActivityMap = new Map<Account, List<Activity>>();
        for(SObject tempobj: scope){
           
            Account tempact = new Account(Id = tempobj.Id, OwnerId = tempobj.OwnerId, Last_Sales_Contact_Date__c = tempobj.Last_Sales_Contact_Date__c, Next_Sales_Activity_Date__c = tempobj.Next_Sales_Activity_Date__c);
        }
        
        

    }
    
    global void finish(Database.BatchableContext BC){
    }
}

Current Errors:
User-added image​​​​​​​
Gabriel C FerreiraGabriel C Ferreira
Hi Willian,

The first error is because the object Activity doesn't exists in Salesforce, maybe you should use the Task object instead.
The others errors occours because you have to cast the "Scope" variable to the type you know that object is, in this case, Task.

Just replace the line 17 and 18 with this code:
Map<Account, List<Task>> accountActivityMap = new Map<Account, List<Task>>();
for(Task tempobj: (Task) scope) {
Ps: the variable accountActivityMap is useless, you may remove that from your code.

If this helped solve your issue, please mark as best response.