2

I'm new to this process and struggling with a simple trigger. I created a trigger to create a task based on the status of an application. I have it working in the sandbox, but when I tried to create it in production, it stated I needed a test class. Now I am stuck. I am including the code for the trigger as well as the test class.

Trigger:

trigger RecruiterAppReview on Application__c (after update) {
/****************
 * When an application status is changed to 'complete and ready for review' and the 
 * application has not been withdrawn, a task needs to be created for the recruiter to 
 * review the application. 
 ****************/ 

List<Task> followupTasks = new List<Task>();
// Generate a list of tasks to be generated
for (Application__c appl : System.Trigger.new) {
    if (appl.Application_Status__c == 'Complete and Ready for Review' && appl.Withdrawal_Reason__c == null) {

        Task tasks = new Task(
                WhoId = appl.Counselor_Id__c,
                Description = 'Review Application',
                Priority = 'High',
                ReminderDateTime = System.now().addDays(3),
                Status = 'Not Started',
                Subject = 'Application Review');
    }
}
// insert the entire list
if (followupTasks.size() > 0) {
    insert followupTasks;
}
}

Test class:

@isTest
private class RecruiterAppReviewTest {

  static testMethod void myUnitTest() {
    // Get Student
    Contact c1 = [Select Name from Contact where Student_Type__c = 'Freshman' limit 1];

    // Create Application
    Application__c a1 = new Application__c(
           Student__c = c1.Id,
           Student_Type__c = 'First Time UG',
           Full_or_Part_Time__c = 'Full-Time',
           Active_Application__c = true,
           Application_Status__c = 'Incomplete-Items Missing');

    insert a1;

    // Test Task
    test.startTest();

    a1.Application_Status__c = 'Complete and Ready for Review';
    update a1;

    test.stopTest();
  }
}

The test class is not compiling because it says Contact c1 (Get Student) System.QueryException:List has no rows for assignment to SObject. When I execute the same statement in the schema in Eclipse, it returns the one row that I expected. Any help would be greatly appreciated. Thank you.

Mike Chale
  • 13,364
  • 6
  • 47
  • 89
user2569499
  • 1,025
  • 2
  • 13
  • 21

1 Answers1

3

The problem is this line in your test class:

Contact c1 = [Select Name from Contact where Student_Type__c = 'Freshman' limit 1];

You're trying to query Contacts without inserting any test data. I would recommend inserting a test Contact as opposed to using seeAllData=true.

Update

Based on comments, here is how you could get the new Contact id:

Id contactId = [SELECT Id FROM Contact LIMIT 1].Id;

Or

List<contact> contacts = [SELECT Id FROM Contact LIMIT 1].Id;    
Id contactId = contacts[0].Id;
Mike Chale
  • 13,364
  • 6
  • 47
  • 89
  • So, it will not let me use an existing Contact? My trigger is based on the application for a contact and I am inserting an application and then updating it. It was my understanding the test classes don't actually generate/keep the data. How would I know the contact id for the application insert? Am I making this too hard? – user2569499 Sep 20 '13 at 19:48
  • Just as you created a new Application__c record you should create a new Contact record. You then query to get the Contact's id. – Mike Chale Sep 20 '13 at 19:58
  • See http://salesforce.stackexchange.com/questions/8558/when-testing-can-i-not-use-existing-data for more tips. – Mike Chale Sep 20 '13 at 19:59
  • So I am adding a contact record, but getting a different error now. New test class looks like this: // Create Student Contact c1 = new Contact( FirstName = 'Test', LastName = 'TestStudent');
     insert c1;
    
     Id contact_id = [Select c.Id from Contact c where c.LastName = 'TestStudent' limit 1];
    
     // Create Application
     Application__c a1 = new Application__c(
            Student__c = contact_id,
            Student_Type__c = 'First Time UG',
       ...
    
    – user2569499 Sep 20 '13 at 21:09
  • getting error on Id contact_id = [Select c.Id from Contact c where c.LastName = 'TestStudent' limit 1]; saying it is illegal assignment. – user2569499 Sep 20 '13 at 21:12
  • Answer updated. – Mike Chale Sep 20 '13 at 21:21