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
FrancoFranco 

2nd (hopefully) easy question

Does anyone have an example of how they query related list items in a trigger?  I've got a custom object that is similar to an opportunity and opportunity line item.  Assume we need a trigger whenever a new opportunity is created.  When the trigger runs, I want to send an email that a new opportunity was created and include all the line items that were created with the opportunity.  (you don't need to show me how to use the email api, I think I understand that.)
 
a) Would you trigger on "new" opportunity? (can we be sure that all the line items are all created when the trigger on the opportunity runs) parent/child relationship
b) Do you use SOQL to query the line items?  What would that query look like? 
c) How would I loop through the line items in the trigger?
 
Thanks in advance for any help you can provide!
 
Franco
FrancoFranco
I think I have it working.  I am still a bit worried about the timing of parent-child records since I'm triggering on the parent.  I don't have the actual orders showing up in my development environment, so it's off to production to test whether the "child" records are actually available.
 
Next I have to figure out the testmethod and deployment thing.
 
So far, so good.
 
Franco
 
------
 
trigger ShipTrigger on SCRB_SalesOrder__c (after insert, after update) {
 String[] toAddresses;
 String HTMLBody;
 For (SCRB_SalesOrder__c SalesOrder: Trigger.new) {
  if (SalesOrder.Type__c == 'Shipment') {  //We have a shipment
   // start constructing an email message
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();   
   //get account details = Name, Sales Team Email Address, Flag for whether to send shipment email
   Account Acct = [Select Name, Sales_Team_Email_Alias__c, Send_Shipment_Notification__c from Account where Id = :SalesOrder.AccountId__c];
   if (Acct.Send_Shipment_Notification__c == TRUE) { // Check the send shipment notification checkbox
    //toAddresses = new String[] {Acct.Sales_Team_Email_Alias__c,'SalesOps@company.com'};
    toAddresses = new String[] {Acct.Sales_Team_Email_Alias__c}; // remove salesops while debugging
   } else {
    toAddresses = new String[]
{'SalesOps@company.com'};  //Sales Ops is always notified
   }
   // Assign the addresses for the To list to the mail object.
   mail.setToAddresses(toAddresses);
   mail.SetSubject('Shipment Notification - ' + Acct.Name);
   mail.setUseSignature(false);
   mail.setPlainTextBody('Shipment for PO: ' + SalesOrder.Customer_PO_Number__c);
   HTMLBody = 'All,<br><br>';
   HTMLBody = HTMLBody + 'This is to inform you that the following material shipped today:<br><br>';
   HTMLBody = HTMLBody + Acct.Name + '--' + SalesOrder.Name + ', PO: ' + SalesOrder.Customer_PO_Number__c + ', SO: ' + SalesOrder.DocumentNumber__c + '<br>';
   // Now query salesforce to get the line items
   for (SCRB_SalesOrderLineItem__c SOLineItem : [select Quantity__c, ProductId__c from SCRB_SalesOrderLineItem__c where SalesOrderId__c = :SalesOrder.Id]) {
    Product2 prod2 = [select Name from Product2 where Id = :SOLineItem.ProductId__c];
    HTMLBody = HTMLBody + SOLineItem.Quantity__c + ' each ' + prod2.Name + '<br>';
   }
                 mail.setHtmlBody(HTMLBody);
                 Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
  }
 }
}