Created
June 4, 2012 00:08
-
-
Save chexxor/2865529 to your computer and use it in GitHub Desktop.
pcon Trigger Template - automated VSC insertion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
/** | |
* Vendor Scorecard auto creation | |
* | |
* After Update, checks SRF is 'In Review', inserts list of VSCs with respective parent IDs | |
*/ | |
public void doAwesomeness() { | |
if (isUpdate) { | |
RecordType[] rec = | |
[SELECT Id | |
FROM RecordType | |
WHERE Name = 'Vendor Scorecard - Appraisals' AND SObjectType = 'Vendor_Scorecard__c' | |
AND IsActive = true]; | |
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' | |
) { | |
//Set for unique IDs for records that pass conditionals above | |
objIds.add(srf.Id); | |
} | |
} | |
//List is to house records that have passed conditions above | |
List <Service_Request__c> srfList = | |
[SELECT Status__c, Id, Vendor_Name__c | |
FROM Service_Request__c | |
WHERE Status__c = 'In Review' | |
AND Id IN: objIds]; | |
//List for update of VSCs added, for each of the srf in the list above | |
List<Vendor_Scorecard__c> vscList = new List<Vendor_Scorecard__c>(); | |
//for records in list, iterate through and use values from those records to create new child VSCs | |
for (Service_Request__c srf: srfList){ | |
if(rec.size() > 0) { | |
Vendor_Scorecard__c vsc = new Vendor_Scorecard__c ( | |
Service_Request__c = srf.Id, | |
Vendor__c = srf.Vendor_Name__c, | |
RecordTypeId = rec[0].Id); | |
vscList.add(vsc); | |
} | |
} | |
if (!vscList.isEmpty()) { | |
try { | |
insert vscList; | |
} catch (DmlException e) {} | |
} | |
//End Routing Boolean | |
} else { | |
return; | |
} | |
} | |
/** | |
* 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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static testMethod void addVendorIdTest() { | |
//Create test Task Order | |
Task_Order__c t = new Task_Order__c(Name = 'Test Task Order'); | |
Insert t; | |
//Create test Account | |
Account a = new Account(Name = 'Test', Task_Order__c = t.Id, Lin__c = '9999'); | |
Insert a; | |
//Create test Contact | |
Contact c = new Contact(LastName = 'Test', AccountId = a.Id, Vendor_Id__c = '123ABC'); | |
Insert c; | |
//Create test Service Request BULK | |
List <Service_Request__c> srfTestList = new List <Service_Request__c>(); | |
for (integer i=0; i<5; i++) { | |
Service_Request__c sr = new Service_Request__c( | |
Task_Order__c = t.Id, | |
Vendor_Id__c = '123ABC', | |
Status__c = 'New' | |
); | |
srfTestList.add(sr); | |
} | |
insert srfTestList; | |
for(Service_Request__c srf: srfTestList) { | |
srf.Status__c = 'In Review'; | |
} | |
test.startTest(); | |
update srfTestList; | |
test.stopTest(); | |
Map <Id, Service_Request__c> SRFmap = new Map <Id, Service_Request__c>(srfTestList); | |
List <Service_Request__c> insertedSRFs = | |
[SELECT Vendor_Name__c, Id | |
FROM Service_Request__c | |
WHERE Id IN: SRFmap.keySet() ORDER BY CreatedDate Asc]; | |
List <Vendor_Scorecard__c > insertedVSCs = | |
[SELECT Service_Request__c, Id | |
FROM Vendor_Scorecard__c | |
WHERE Service_Request__c IN: SRFmap.keySet() ORDER BY CreatedDate Asc]; | |
Map <Id, Vendor_Scorecard__c> VSCmap = new Map <Id, Vendor_Scorecard__c>(insertedVSCs); | |
String testVendor = | |
[SELECT Vendor_Name__c | |
FROM Service_Request__c | |
WHERE Id = :srfTestList LIMIT 1].Vendor_Name__c; | |
for (Service_Request__c s: insertedSRFs) { | |
System.assertEquals( | |
c.Id, | |
testVendor | |
); | |
} | |
for (Vendor_Scorecard__c vsc : insertedVSCs) { | |
Service_Request__c srf = serviceRequestIndexMap.get(v.Id); | |
System.assertEquals(vsc.Id, srf.Service_Request__c); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment