Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- }
- }
- }
Add Comment
Please, Sign In to add comment