You need to sign in to do that
Don't have an account?
Chris Pulliam
Inbound Change Set Error for Test Class Apex for Trigger on Task
Hi there.
Working on my first trigger. The trigger works, and I can get 80% code coverage with the test class below. However, when I try to bring it into production it fails.
My test class gives me 80% code coverage, but when I send everything through a change set the class fails with an INVALID_CROSS_REFERENCE_KEY Exception. My hunch is that this is because the lead IDs I use in WhoId are to leads that exist in sandbox, but not production.
How would I write this in a way that it works when I deploy?
Working on my first trigger. The trigger works, and I can get 80% code coverage with the test class below. However, when I try to bring it into production it fails.
trigger attachToAccount on Task (before update) { // Get Lead Ids from all of the triggered Tasks set<id> leadIds = new set<id>(); for(Task myTask :trigger.new) { leadIds.add(myTask.whoId); } // Get Leads and build a map of <LeadId,Lead_Accounts__c> map<id,id> mLeadAccountIds = new map<id,id>(); list<Lead> leads = [SELECT id, Lead_Accounts__c FROM Lead WHERE Id IN :leadIds]; for(Lead l :leads) { mLeadAccountIds.put(l.id,l.Lead_Accounts__c); } // Update triggered tasks with Account Id for(Task t :trigger.new) { if(mLeadAccountIds.containsKey(t.whoId)) { t.whatId = mLeadAccountIds.get(t.whoId); } } }
My test class gives me 80% code coverage, but when I send everything through a change set the class fails with an INVALID_CROSS_REFERENCE_KEY Exception. My hunch is that this is because the lead IDs I use in WhoId are to leads that exist in sandbox, but not production.
How would I write this in a way that it works when I deploy?
@isTest public class ActivityCountTest_Test { public static testmethod void testinsert() { Task task= new task(); task.Subject='Message Sent 1'; task.status='Completed'; task.Whoid='00Q55000001r6Bv'; insert task; Task task1= new task(); task1.Subject='Call'; task1.status='Completed'; task1.Whoid='00Q55000001r6C0'; insert task1; Task task2= new task(); task2.Subject='Message Sent 3'; task2.status='Completed'; task2.Whoid='00Q55000001r6C0'; insert task2; task.Subject = 'Closed'; update(task); task1.Subject = 'Closed'; update(task1); task2.Subject = 'Closed'; update(task2); } }
All Answers
I "bulkified" my test class, but now it gives me 0% code coverage on the trigger. I don't undertand why it's not picking it up.
Trigger
Test Class