I'm a total newb when it comes to salesforce and have been learning it for a couple of weeks now.
So I've been plugging away at a few triggers and I've got them working in my sandbox. Now I'm writing test classes for them so I can deploy them to my organization. I need some help in understanding how test classes work. I've read the workbooks and a ton of blogs and other questions and I think I have the basic idea down but there's just something that's not connecting for me. Anyway let me explain my code and hopefully someone can see what I'm missing.
So first here's my trigger:
trigger updatePrMEnCCeO on Programs__c (after update) {
//Create List of old program objects
List<Programs__c> prOld = trigger.old;
//Create List of Apps that need changing
List<Application__c> app = [SELECT Program_Manager__c, Enrollment_Counselor__c, Center_Owner__c, Id from Application__c WHERE Programs__c=:prOld[0].id];
//loop through all edited programs__c object
for (Programs__c edited : trigger.new) {
//checks for a difference in any of the three fields and then changes all three (possibly faster if inner logic placed into a switch or nested if)
if ((prOld[0].program_manager_lookup__c != edited.program_manager__c) || (prOld[0].Enrollment_Counselor__c != edited.Enrollment_Counselor__c) ||(prOld[0].Center_Owner__c != edited.Center_Owner__c) ) {
for (Integer i=0; i<app.size(); i++) {
app[i].Program_Manager__c=edited.Program_Manager_lookup__c;
app[i].Enrollment_Counselor__c=edited.Enrollment_Counselor__c;
app[i].Center_Owner__c=edited.Center_Owner__c;
update app[i];
}
}
}
}
I'm sure you can tell, but what it does is look for any of 3 fields that were changed in the program and then updates all the associated applications with the new values of the fields
And here's my test class:
@isTest(SeeAllData=true)
public with sharing class updatePrMEnCCeOTest {
public static testMethod void updatePrMEnCCeOTest () {
//Begin test
test.starttest();
//New Program
Programs__c Prog = new Programs__c();
prog.Centers_LOOKUP__c='a0wJ0000003x3PIIAY';
prog.start_date__c=date.parse('08/01/2014');
//New Application
Application__c App = new Application__c();
app.Contact__c='003J000000t6Z6bIAE';
//Inserts
insert prog;
insert app;
//set Program values
prog.program_manager_lookup__c='005d0000002xQjZ';
prog.enrollment_counselor__c='005d0000002xQjZ';
prog.center_owner__c='005d0000002xQjZ';
//update Program and set off trigger
update prog;
//check for expected outcome
System.assertEquals('005d0000002xQjZ',app.program_manager_lookup__c);
System.assertEquals('005d0000002xQjZ',app.enrollment_counselor__c);
System.assertEquals('005d0000002xQjZ',app.center_owner__c);
//end test
test.stoptest();
}
}
As I'm understanding test classes, you basically just need to create an instance of the objects that you're working with in your trigger, and then do something that causes the trigger to fire, here, I'm creating Programs and Applications (The hard coded values are because of the multitude of errors I got without them, they're seemingly required).
I'm finally getting 100% code coverage, but the test is failing with this error:
system.dmlexception:insert failed. First exception on row0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id:[]
Now I know I can hardcode an ID in, but apart from that being against best practices (as I understand them outlined in the workbooks/blogs), it still won't matter because when I try to deploy to the server, I get completely different errors and my trigger gets 0% code coverage.
I'm trying to deploy via eclipse with the force.com ide plugin.
thanks for any help and explanations, and I apologize if I'm not explaining it properly, I'm sort of lost here, and really just something isn't connecting for me conceptually about how testing works :)