2

So I encapsulated the objects within the test, but still having an issue that doesn't make sense...

To recap, the trigger is: 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;
}

}

The new test class is:

@isTest
private class RecruiterAppReviewTest {

    static testMethod void myUnitTest() {
        // 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 = [Select c.Id from Contact c where c.LastName = 'TestStudent' limit 1],
               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();
    }
}

Problem is with trying to assign Student_c to [Select c.Id from Contact c where c.LastName = 'TestStudent' limit 1]. I am getting Invalid initial expression type for field Application_c.Student__c, expecting: Id. I thought that is what I was giving it...

Any help would be greatly appreciated. Thank you.

1

You are assigning the Object itself, not the Id.

Try this

Student__c = [Select c.Id from Contact c where c.LastName = 'TestStudent' limit 1].Id;

This will work, but as Rao points out, you really dont need this SOQL expression. You already have the Contact c1.

So you simply need to assign c1 as the Student like this

Student__c = c1.Id;
  • I dont think he needs a soql here. Though I would agree tha what you said is another way of doing this. – Rao Sep 20 '13 at 21:25
  • @user2569499 can you please mark the answer solved – Rao Sep 20 '13 at 21:29
2

Since student is a lookup once you are done inserting the new student, all you have to do is associate the ID of the newly created record to the application

so this

 Application__c a1 = new Application__c(
           Student__c = [Select c.Id from Contact c where c.LastName = 'TestStudent' limit 1],
           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;

becomes

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;
  • This is a great point and much more efficient. I was just trying to adjust his existing code, wasn't looking very closely for ways to improve. Good catch +1 – Chris Duncombe Sep 20 '13 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.