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
sites1.3909952495827087E12sites1.3909952495827087E12 

Iam trying to update on user how many accounts user owned. Have written batch class Select id,Total_Accounts__c, (Select id,Parent.id From Accounts__r) From User u but accounts are returned 0 , can any one suggest wats wrong in this query

global class BatchAccountUserCount implements Database.Batchable<SObject>, Database.Stateful{
   
    global Database.queryLocator start(Database.BatchableContext ctx){
     return Database.getQueryLocator([Select id,Total_Accounts__c, (Select id,Parent.id From Accounts__r) From User u where id=:userinfo.getuserid()]);
    }
    
    global   void execute(Database.BatchableContext ctx, List<Sobject> scope){
        List<User> userList = (List<User>)scope;
        System.debug('@@@@@userList@@@@@@@@'+userList.size());
        for(Integer i = 0; i < userList.size(); i++){
            List<Account> accList = userList[i].Accounts__r;
            System.debug('@@@@@accList@@@@@@@@'+accList.size());
            userList[i].Total_Accounts__c  = accList.size();
        }
       // ProcessorControl.inFutureContext = true;
        //List<Database.SaveResult> dsrs = Database.update(userList, false);
        update userList;
    }
   
    global   void finish(Database.BatchableContext ctx){   
   
    }
}
Sonam_SFDCSonam_SFDC
I understand that you have created a relationship between Acocunt and User:

If you wish to use relationships in SOQL you should be using the following format to access Fields in the child object:
E.g.
SELECT Widget__c.Name, (SELECT Model__c.Name FROM Widget__c.Models__r) FROM Widget__c
Where Model is a child of Widget.