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
Ishan Singh 4Ishan Singh 4 

I want to update lead owner to XYZ when lead owner is inactive before for loop starts

global class BirthdayCard implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext bc)
     {
        return Database.getQueryLocator([SELECT OwnerId,DOB_Formula__c,Id,Possible_Followup_Date__c FROM Lead WHERE DOB_Formula__c =  NEXT_N_DAYS:7 AND Possible_Followup_Date__c != null]);
    }
    global void execute(Database.BatchableContext bc, List<Lead> scope)
    { 
        List<Task> listOfTask = new list<Task>();
        
        for(Lead l : scope)
        {
            Task B = new Task();
            B.Subject= 'Send Birthday Card';
            B.ActivityDate = date.today();
            B.OwnerId = l.OwnerId;
            B.WhoId=l.Id;
            listOfTask.add(B);
 
Naval Sharma4Naval Sharma4
You can try something like this.
 
global class BirthdayCard implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([SELECT OwnerId, Owner.IsActive, DOB_Formula__c,Id,Possible_Followup_Date__c FROM Lead WHERE DOB_Formula__c =  NEXT_N_DAYS:7 AND Possible_Followup_Date__c != null]);
    }
    global void execute(Database.BatchableContext bc, List<Lead> scope) { 
        User aUserForInactiveLead = [SELECT Id FROM User WHERE Name = 'XYZ' LIMIT 1];
        List<Task> listOfTask = new list<Task>();
        for(Lead l : scope) {
            Task B = new Task();
            B.Subject= 'Send Birthday Card';
            B.ActivityDate = date.today();
            if( !l.Owner.IsActive ) {
                l.OwnerId = aUserForInactiveLead;
            } 
            B.OwnerId = l.OwnerId;
            B.WhoId=l.Id;
            listOfTask.add(B);
       }
       // If you want to update Leads owner then perform the DML
       update scope;
   }
}

 
Ishan Singh 4Ishan Singh 4
@Naval Sharma4
Some leads have inactive owners so for them I want to assign 'XYZ' owner and leads which have active owners they should remain as it is. After that I want to go inside my 'for' loop.
  
AnudeepAnudeep (Salesforce Developers) 
global class BirthdayCard implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext bc)
     {
        return Database.getQueryLocator([SELECT OwnerId,DOB_Formula__c,Id,Possible_Followup_Date__c FROM Lead WHERE DOB_Formula__c =  NEXT_N_DAYS:7 AND Possible_Followup_Date__c != null]);
    }
    global void execute(Database.BatchableContext bc, List<Lead> scope)
    { 
        List<Task> listOfTask = new list<Task>();
        List<Lead> LeadLstInactive = new List<Lead();
        List<Lead> LeadLstXYZOwner = new List<Lead();
        Set<Id> LeadIds = new List<Id>();

        for(Lead l:scope) {
        LeadIds.add(l.Id);
        }
        
        //Prepare a list of leads where lead owner is inactive

        LeadLstInactive = [Select Id, OwnerId from Lead where Id IN:leadIds AND Owner.isActive = false];

        User XYZ = [SELECT Id FROM User WHERE Name = 'XYZ' LIMIT 1];

]
        // Assign XYZ owner to inactive list
        
        for(Lead l : LeadLstInactive)
        {   
            l.OwnerId = XYZ.Id
            LeadLstXYZOwner.add(l);
        }

        update LeadLstXYZOwner;

        for(Lead l : LeadLstXYZOwner) {

            Task B = new Task();
            B.Subject= 'Send Birthday Card';
            B.ActivityDate = date.today();
            B.OwnerId = l.OwnerId;
            B.WhoId=l.Id;
            listOfTask.add(B);
        }

Anudeep