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
AnonTestQuestAnonTestQuest 

Making two lists out of a scope of records

How would I go about making two separate lists of items in my scope in a batch that return different records based on criteria? I want to send out separate emails to each list.
Best Answer chosen by AnonTestQuest
AnonTestQuestAnonTestQuest
For anyone else who reads this, my solution was to bring in all records with my query in my sched batch into my batch class. Then I sent out the email as usual with an added 'if' statement at the end of my for loop before reiterating through the loop. The if statement contained one of the criteria and the different HTML body and subject correlating to that. So anything that ran through the for loop that met the first criteria, wouldn't get stopped by the if statement and send correctly. If it did get caught by the criteria in the if statement then it would change the body of the email and the subject of the email and send that one while continuing to iterate through the for loop until every return, now in the array I'm looping through, has been sent out.

All Answers

viruSviruS
As per syntex 

global void execute(Database.BatchableContext BC, List<sObject> scope){   
}

You can send sObject list and identify the Object with Prefix of there id .. might be helful for you
AnonTestQuestAnonTestQuest
I have that in my code. I already had the batch setup to send one email for anyone who had a record that expired in <= 2 days from the time the batch ran. I just need to alter that code into 2 seperate lists so I can email each of them seperate emails. One list for if the expiration = Next_N_Days:2 and one list of the records that meet the criteria expiration <= TODAY.
AnshulVermaAnshulVerma
A way can be to set a variable within batch job wherein you can conditionally fire different queries based on different values of the variable. Fire this batch twice, one each with different value of the variable so your batch can process two lists.
AnonTestQuestAnonTestQuest
I need to use the query that returns all records that expire in <= Next_N_Days:2 then in my batch, I return the scope of all those records. Within the scope, I need to make two lists:

1) any records that expire = Next_N_Days:2

&

2) any records that expire <= TODAY

Once I have those two lists I need to do a for if else statement that runs throug the lists and sends the approapriate email.
AnonTestQuestAnonTestQuest
For anyone else who reads this, my solution was to bring in all records with my query in my sched batch into my batch class. Then I sent out the email as usual with an added 'if' statement at the end of my for loop before reiterating through the loop. The if statement contained one of the criteria and the different HTML body and subject correlating to that. So anything that ran through the for loop that met the first criteria, wouldn't get stopped by the if statement and send correctly. If it did get caught by the criteria in the if statement then it would change the body of the email and the subject of the email and send that one while continuing to iterate through the for loop until every return, now in the array I'm looping through, has been sent out.
This was selected as the best answer