0

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
8
  • In case it helps, the earlier question referred to is salesforce.stackexchange.com/questions/30935/….
    – Keith C
    Commented Mar 26, 2014 at 18:37
  • 1
    One key point is that your test must create every object because the testing framework automatically isolates you from data in the org (with a few exceptions that are not relevant here). So hard-coded ids will not work and you need to replace them with the id of an object you have inserted in the test. But I will leave it to others this time to write a detailed answer.
    – Keith C
    Commented Mar 26, 2014 at 18:44
  • Your s variable (Sampling__c) uses the correct syntax to define the values of the fields of an object that you then insert into the database. You need to use that approach for pp and c too. The query expression in square brackets invokes a query and as already described will return nothing here. (If only one object is returned it can be directly assigned to a variable.)
    – Keith C
    Commented Mar 26, 2014 at 21:00
  • I don't understand what pp is suppose to be doing
    – EricSSH
    Commented Mar 26, 2014 at 21:09
  • Well I've no idea - it is your test and using your knowledge of what your trigger does you should create the objects and relate them together by assigning the new object id values to the lookup fields. If its not needed get rid of it.
    – Keith C
    Commented Mar 26, 2014 at 21:13

1 Answer 1

2

you may need to change these lines in your test class to be as generic as possible instead of hard coded ids as what Keith referred.

Product__c = '01tE0000000hNN1IAM', //should be p.id

because you created a product. and then, this for this, you have to create a contact record inside the test context just the same of what you did for creating a product but for contact and use its id.

Contact__c = '003E000000A7tKrIAJ', //should be Contact.Id

in order to get the contact id you need to crate a contact in your test class itself.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .