You need to sign in to do that
Don't have an account?
Franco
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
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 });
}
}
}