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
Mike Lee PCLMike Lee PCL 

Apex data processing help

Hello. I'm new to Apex and am looking for some advice regarding working with data and avoiding governor limits.

Here's what I'd like to accomplish:

1. Perform a SOQL query and grab some data from Contacts (roughly 500 records in total). The 500 records will be owned by 10 people.
2. With that data in hand further divide that data into groups sorted by ownerid. For example if 50 records are owned by ownerid = 'abc' I want to somehow be able to process those 50 records
3. In this scenario processing consists of emailing out a message to the email address associated with the ownerid
3. Once the email is sent move on to the next set of records owned by the next ownerid

I'd like to schedule this to run once a day.

I was thinking I could use a map with the ownerid as the key and a Contact sObject List as the values. 

However not sure if this is something that can be scheduled???

Any help would be greatly appreciated!

Thanks,

Mike
Best Answer chosen by Mike Lee PCL
JeffreyStevensJeffreyStevens
Lots there to consider - I'll just bite a bit.

If you get your contacts in a construct like this - map<OwnerID,list<Contact>>   (defined like map<id,list<Contact>> mOwnerIdContacts = new map<id,list<Contact>>();

Then you could process that map and do stuff with it.

Here is the code to load the map...
map<id,list<Contact>> mOwnerContacts = new map<id,list<Contact>>();
list<Contact> contacts = new list<Contact>([SELECT id,email,ownerId FROM Contact WHERE Field__c = 'Some Criteria' LIMIT 500);

// populate the list into the map...
For(Contact c :contacts) {
  list<Contact> iterContacts = new list<Contact>();
  if(mOwnerContacts.containsKey(c.ownerId)) {
    iterContacts = mOwnerContacts.get(c.ownerId);
  }
  iterContacts.add(c);
  mOwnerContacts.put(c.ownerId,iterContcts);
}

 

All Answers

JeffreyStevensJeffreyStevens
Lots there to consider - I'll just bite a bit.

If you get your contacts in a construct like this - map<OwnerID,list<Contact>>   (defined like map<id,list<Contact>> mOwnerIdContacts = new map<id,list<Contact>>();

Then you could process that map and do stuff with it.

Here is the code to load the map...
map<id,list<Contact>> mOwnerContacts = new map<id,list<Contact>>();
list<Contact> contacts = new list<Contact>([SELECT id,email,ownerId FROM Contact WHERE Field__c = 'Some Criteria' LIMIT 500);

// populate the list into the map...
For(Contact c :contacts) {
  list<Contact> iterContacts = new list<Contact>();
  if(mOwnerContacts.containsKey(c.ownerId)) {
    iterContacts = mOwnerContacts.get(c.ownerId);
  }
  iterContacts.add(c);
  mOwnerContacts.put(c.ownerId,iterContcts);
}

 
This was selected as the best answer
Mike Lee PCLMike Lee PCL
Thanks for the help Jeffrey! This will definitely help me get started.

Mike