Find your content:

Search form

You are here

Auto population Trigger test class help

 
Share

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

Attribution to: EricSSH

Possible Suggestion/Solution #1

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.


Attribution to: Bforce
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31014

My Block Status

My Block Content