I have a requirement to Auto populate a field in my Sample__c object from my Product object whenever there is an Application Engineer name present. I started off thinking I could do a formula but since these are Picklists values, I couldn't figure out a way to do it, so I wrote a before/after Insert Trigger which I believe I did a decent job. Now I need help writing a test class, because I can wrap my head around it and my rookieness isn't helping me with the logic. The following is my trigger
trigger SampleRVBAutoPop on Sampling__c(before insert, after insert) {
//Generating data.
Product2 prodObj = [SELECT AE2__c, Name, Id FROM Product2];
system.debug('Product Object === ' +prodObj);
List < Sampling__c > lstParentprodObj = new List < Sampling__c > ([Select sampling__c.Responsible_for_EVB_Build__c From Sampling__c s where sampling__c.Product__c = : prodObj.id]);
if (trigger.isAfter) {
List < Sampling__c > lstUpdateSamp = new List < Sampling__c > ();
for (Sampling__c s: Trigger.new) {
Sampling__c sObj = new Sampling__c(Id = s.id, Responsible_for_EVB_Build__c = s.Responsible_for_EVB_Build__c);
if (lstParentprodObj.size() > 0) {
//Found a match
if (s.Responsible_for_EVB_Build__c == null && prodObj.AE_Name__c != null) {
//Is it populated?
system.debug('@@@@BEFORE EVB' + s.Responsible_for_EVB_Build__c);
s.Responsible_for_EVB_Build__c = prodObj.AE_Name__c;
system.debug('@@@@EVB' + s.Responsible_for_EVB_Build__c);
} else {
if (lstParentprodObj.size() == 0) {
Sampling__c parentprodObj = lstParentprodObj.get(0);
system.debug('inside else.');
//sObj.OwnerId=parentprodObj.id;
}
}
}
lstUpdateSamp.add(sObj);
}
try {
update lstUpdateSamp;
} catch (Exception e) {
System.debug('Error in updating Sampling' + e);
}
}
if (trigger.isBefore) {
for (integer i = 0; i < Trigger.new.size(); i++) {
if (Trigger.new[i].Responsible_for_EVB_Build__c == null && prodObj.AE_Name__c != null) {
Trigger.new[i].Responsible_for_EVB_Build__c = prodObj.AE_Name__c;
system.debug('@@@@EVB' + Trigger.new[i].Responsible_for_EVB_Build__c);
}
}
}
}
So with the help of Keith and sfdcfox they gave me some really good input, but some of what they said doesn't make a great deal of sense to me so I decided to start a new thread. The following is my attempt at a test class. Which I thought was going to be as straight forward as my first test class but it isn't working out that way. Any help is appreciated.
@isTest
private class AutoPopulateSampleTest {
static testMethod void rsmUpdate() {
Product2 p = new Product2(item_number__c = 'SC700WLTRC',
Name = 'SC700WLTRC',
AE2__c = '005E0000000RYQBIA4',
Business_Unit__c ='Murica',
Item_class__c ='CK',
AE_Text__c='Michael ',
PME_Text__c='Michael '
);
insert p;
Contact c = new Contact(id = '003E0000007BaQtIAK');
Sampling__c s = new Sampling__c(Quantity_of_Samples__c = 2,
//s.Date_Sample_Needed__c = (2014,3,18),
Product__c = p.id, //This is the product that is being tested, it is a Lookup(Product)
Override__c = 'No',
Country__c = 'US',
State_Province__c = 'CA',
Zip_Postal_Code__c = '93003',
Contact__c = c.id,
Responsible_for_EVB_Build__c = '');
try {
insert S;
} catch (DmlException e) {
system.assertEquals(true, e.getMessage().contains('my expected error'));
}
}
}
Now I'm getting an error cause by a different trigger on Product.
Trigger.PriceBookEntryUpdate: line 42, column 1: []
14:50:25.692 (9692644000)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, PriceBookEntryUpdate: execution of AfterInsert