Skip to content

Instantly share code, notes, and snippets.

@pcon
Created May 31, 2012 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pcon/2845269 to your computer and use it in GitHub Desktop.
Save pcon/2845269 to your computer and use it in GitHub Desktop.
Checking previous status != 'In Review' and then switched to 'In Review' - field expression now allowed for generic SObject line 33
public class ServiceRequestTrigger {
private final Map<Id, Service_Request__c> oldMap;
private final Map<Id, Service_Request__c> newMap;
private final List<Service_Request__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 ServiceRequestTrigger(Map<Id, Service_Request__c> xoldMap, List<Service_Request__c> xnewObjs, Boolean isBefore) {
oldMap = xoldMap;
newObjs = xnewObjs;
if (!isBefore && newObjs != null) {
newMap = new Map<Id, Service_Request__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() {
set<Id> objIds = new Set<Id>();
for (Service_Request__c srf: newObjs) {
if (
oldMap.get(srf.ID) != null &&
oldMap.get(srf.ID).Status__c != srf.Status__c &&
srf.Status__c == 'In Review'
) {
//build up the list/map in here
objIds.add(srf.Id);
}
List <Service_Request__c> srfList =
[SELECT Status__c, Id
FROM Service_Request__c
WHERE Status__c = 'In Review'
AND Id IN: objIds];
}
List<Vendor_Scorecard__c> vscList = new List<Vendor_Scorecard__c>();
//do the logic and processing here
for (Service_Request__c srf: srfList){
Vendor_Scorecard__c vsc = new Vendor_Scorecard__c (vsc.Service_Request__c = srf.Id);
vscList.add(vsc);
}
if (!vscList.isEmpty()) {
try {
insert vscList;
} catch (DmlException e) {
ApexPages.addMessages(e);
return null;
}
}
}
/**
* 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, Service_Request__c> oldMap, List<Service_Request__c> newObj, Boolean isBefore) {
final ServiceRequestTrigger myTrigger = new ServiceRequestTrigger(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