+ Start a Discussion

I'm getting Confused, Querying too many objects.

Here is my delema, 


I am working with a third part print vendor to do mailings.  They have created two objects:

  1. printsf__Collateral_Order_History__c - For every order I place they create one entry in this table.
  2. printsf__Collateral_Send_History__c - this table is a child of the previous table and for every order I place they create one entry per person that receives the mailing, roughly 500 per week. 
  3. I have a thrid table SD_Member__c - this contains all the information regarding the people being mailed too
  4. Finally, I have a fourth table - SD_Member_Mailing_History__c that links the printsf__Collateral_Send_History__c and the SD_Member__c.  This way I can click on a SD_Member__C record and see all the mailing he/she has been a part of.  


OK see here's what happens.  I send an order to the printer and he creates an the vendor creates an entry in the printsf__Collateral_Order_History__c.  He verifies each entry and once verified, he creates an entry in the  printsf__Collateral_Send_History__c.  After a day or two he mails all my letters and updates a field on the printsf__Collateral_Order_History__c table printsf__Date_Completed__c with the mailing date.  Once that date is updated, I need to create a follow task for each SD_Member__s for the record owner to make a follow up call.  There is a Date_Mailed__c field on the printsf__Collateral_Send_History__c table, but it's a fomula field looking back at the printsf__Collateral_Order_History__c.printsf__Date_Completed__c field.


So my delema, when the vendor updates the printsf__Collateral_Order_History__c table printsf__Date_Completed__c I need to query all the related printsf__Collateral_Send_History__c, then query the SD_Member_Mailing_History__c table to determine all the SD_Member__C ids so I can create a new task for each SD_Member__C record.


My code so far is as follows:

trigger Not_Sure_If_This_Will_Work on printsf__Collateral_Order_History__c (after update) {
// Set Variables 
printsf__Collateral_Order_History__c [] MailingOrder = new printsf__Collateral_Order_History__c[0];
//printsf__Collateral_Send_History__c [] MailingHistory = new printsf__Collateral_Send_History__c[0];
Task [] newTask = new Task[0];

// Map of SD Member Ids
    Map<Id,SD_Member__c> members = new Map<Id,SD_Member__c>();
// Map All History Itemss
	Map<id,printsf__Collateral_Send_History__c> HistoryEntry = New Map<id,printsf__Collateral_Send_History__c>();

// Identify Correct Mail Order
For (printsf__Collateral_Order_History__c record:trigger.new)

//Obtain all the Correct Mail History Items
For (printsf__Collateral_Order_History__c record:MailingOrder)

//If there are any History Items to query, query them.
	HistoryEntry.putall([select id, printsf__Recipient_ID__c from printsf__Collateral_Send_History__c  where printsf__Order__c in :HistoryEntry.keyset()]);

string strid = HistoryEntry.get(record.id).printsf__Recipient_ID__c;

// For each History Item, Create a task
For(printsf__Collateral_Order_History__c record:MailingOrder){
	newtask.add(new task(Subject='This Worked!!!'));

Insert newtask;


I monkeyed this together to see if could figure it out, but right now it only creates one task.  I'm guessing that's based on the one printsf__Collateral_Order_History__c and not all the related printsf__Collateral_Send_History__c entries.


ANY HELP WOULD BE APPRECIATED as I already spent a week on this.