Skip to content

Instantly share code, notes, and snippets.

@pcon
Created May 22, 2012 18:57
Show Gist options
  • Save pcon/2770954 to your computer and use it in GitHub Desktop.
Save pcon/2770954 to your computer and use it in GitHub Desktop.
Pcon trigger template.java
public class OrderTrigger {
private final Map<Id, Order__c> oldMap;
private final Map<Id, Order__c> newMap;
private final List<Order__c> newObjs;
private final Boolean isInsert;
private final Boolean isUpdate;
private final Boolean isDelete;
private final Boolean isBulk;
/**
* The constructor
*
* @param xoldMap The old map from the trigger
* @param xnewObj The list of new objects from the trigger
* @param isBefore If the trigger is in before or after
*/
public OrderTrigger(Map<Id, Order__c> xoldMap, List<Order__c> xnewObjs, Boolean isBefore) {
oldMap = xoldMap;
newObjs = xnewObjs;
if (!isBefore && newObjs != null) {
newMap = new Map<Id, Order__c>(newObjs);
}
isDelete = (((newObjs == null || newObjs.isEmpty()) && isBefore) || ((newMap == null || newMap.isEmpty()) && !isBefore));
isUpdate = ! (isDelete || oldMap == null || oldMap.isEmpty());
isInsert = ! (isDelete || isUpdate);
isBulk = (newObjs != null && newObjs.size() > 1) ? true : false;
}
public void doAwesomeness() {
List <Order__c> srfList = [
select Status__c
from Order__c
where (
Status__c = 'Appraisal Contracted' or
Status__c = 'Completed'
) and
Order__c.Id in :newMap.keySet()
];
Map<Id, Proposal__c> rfpMap = new Map<Id, Proposal__c>();
for (Proposal__c prop: [
select Name,
Status__c,
Order__c,
Vendor__c,
Vendor__r.Name
from Proposal__c
where Status__c = 'Accepted' and
Order__c = :srfList
]) {
rfpMap.put(prop.Order__c, prop);
}
List Attachment attachList = new List<Attachment>();
for (Order__c srf: srfList) {
//Looking for newly contracted LOEs to create in Attachments
if ( srf.Status__c == 'Appraisal Contracted' ) {
//Create the PDF
PageReference savePDF = new PageReference('/apex/vendorloe?rfpId='+rfp.Id+'&vendorId='+rfp.Vendor__c);
Blob pdfBlob = savePDF.getContent();
if (rfpMap.containsKey(srf.Id)) {
attachment Atch = new Attachment(
ParentId = rfpMap.get(srf.Id).Order__c,
Name = 'Letter Of Engagement - '+rfp.Vendor__r.Name+' - '+rfp.Name+'.pdf',
ContentType = 'pdf',
Body = pdfBlob
);
attachList.add(Atch);
}
}
}
if (attachList.isEmpty()) {
insert attachList;
}
}
/**
* Method to initiate trigger logic
*
* @param oldMap The old map from the trigger
* @param newObj The list of new objects from the trigger
* @param isBefore If the trigger is in before or after
*/
public static void processTrigger(Map<Id, Order__c> oldMap, List<Order__c> newObj, Boolean isBefore) {
final OrderTrigger myTrigger = new OrderTrigger(oldMap, newObj, isBefore);
if (!isBefore) {
myTrigger.doAwesomeness();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment