I'm having difficulties creating a Unit Test for an Apex Trigger to pass so I can push the trigger into production. I'm hoping someone can help me or point me in the right direction to where I'm falling down.
I've created a trigger to re-assign newly inserted Leads to the appropriate sales rep using a custom object (Zip_Code__c) that contains all zips, territory names, and sales reps associated. The trigger works great in the sandbox. However, I cannot port this to production because I cannot get the Unit Test to pass.
I'm obviously missing something critical in creating the Unit test class to appropriately target my code. It's been 3 years since I've touched code, and I'm definitely out of practice. :P
My Trigger:
trigger LeadAssignmentTrigger on Lead (before insert){
List<Lead> leadsToUpdate = new List<Lead>();
for (Lead lead : Trigger.new) {
if (lead.PostalCode != NULL) {
// Find the sales rep for the current zip code
List<Zip_Code__c> zip = [select Sales_Rep__c from Zip_Code__c where Name = :lead.PostalCode limit 1];
// if you found one
if (zip.size() > 0) {
//assign the lead owner to the zip code owner
lead.OwnerId = zip[0].Sales_Rep__c;
leadsToUpdate.add(lead);
}
}
}
}
And my Test Class
@isTest
private class AssignLeadOnCreate{
static testMethod void AssignLeadTest() {
// Setup the lead record
Lead testLead = new Lead(lastname='LAST',firstname='FIRST',company='CUSTOMER',OwnerId='00515000006ShAp',PostalCode='46038');
System.debug('OwnerID before inserting new Lead: ' + testLead.OwnerId);
// Insert Lead
insert testLead;
// Trigger should have changed the record owner to that found in Zip_Code__c
// Retrieve the new Lead
testLead = [SELECT OwnerId FROM Lead WHERE Id = :testLead.Id];
System.debug('OwnerID after inserting new Lead: ' + testLead.OwnerId);
//Grab ownerid for ZIP code from ZIP_code__c object
//List<Zip_Code__c> ZipList = [select Sales_Rep__c from Zip_Code__c where Zip__c= '46038'];
//System.debug('Size of Zip Array: ' + Ziplist.size());
//String SalesID = ZipList[0].Sales_Rep__c;
// String SalesID = lead.OwnerId;
// Verify SalesID matches record owner
//System.assertEquals('00515000006TnIN',testLead.OwnerId);
}
}
I'm receiving 0 records back from the retrieve call. Ideally, I'd like this to work as a unit test should, but essentially, I need this to pass so I can place it into production.
I've tried many iterations of hard coding, but I'm not even good at trying to 'cheat!' :)
Any advice you can give will be much appreciated!
Best regards, Steve