+ Start a Discussion
Chris PulliamChris 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.
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);

        }

}

 
Best Answer chosen by Chris Pulliam
sharathchandra thukkanisharathchandra thukkani
so it is not executing line number 9 of your test class. insert the lead in you test class and then run you test class.

All Answers

sharathchandra thukkanisharathchandra thukkani
task.Whoid you can not hardcode it will be specific to the organization it should be of production.
Chris PulliamChris Pulliam

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
trigger attachLeadInTaskToAccount 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);
          }
  }

}

Test Class 
@isTest
public class attachLeadInTaskToAccount_Test
{
      public static testmethod void testinsert()
      {
            
list<Lead> leads = [SELECT id, Lead_Accounts__c FROM Lead WHERE Lead_Accounts__c !=null AND Lead_Accounts__r.Name = 'test'];

if(leads.size() > 0) {

String lead1 = leads[1].id;
String lead2 = leads[2].id;
String lead3 = leads[3].id;

Task task= new task();
task.Subject='Message Sent 1';
task.status='Completed';
task.Whoid= lead1;
insert task;

Task task1= new task();
task1.Subject='Call';
task1.status='Completed';
task1.Whoid= lead2;
insert task1;

Task task2= new task();
task2.Subject='Message Sent 3';
task2.status='Completed';
task2.Whoid= lead3;
insert task2;

task.Subject = 'Closed';
update(task);

task1.Subject = 'Closed';
update(task1);

task2.Subject = 'Closed';
update(task2);

}
        }

}

 
sharathchandra thukkanisharathchandra thukkani
Here first you need to insert the leads who belong to "Test" Account. i dont see you have done that.
sharathchandra thukkanisharathchandra thukkani
so it is not executing line number 9 of your test class. insert the lead in you test class and then run you test class.
This was selected as the best answer
Chris PulliamChris Pulliam
thanks for your help!