You need to sign in to do that
Don't have an account?

Help Writing Test Class for Trigger
So I've gotten pretty good at writing triggers, by simply altering coding I've found on here and salesforce.com. So I understand how to write them, but not necessarily WHY they work. So because I'm a novice, I'm having a bit of trouble figuring out the test codes. I've read the Introduction to Apex Test Methods, but it's not quite clicking for me. I'm hoping this is a really easy trigger that someone can help me figure out how to write the test code for it? If it isn't too much work to write it for me, I can probably figure out all of the other triggers.
trigger AutoCreateGeneralIntake on Contact (after insert) { List<General_Intakes__c> GeneralIntake = new List<General_Intakes__c>(); for (Contact newContact: Trigger.New) { if (newContact.Screening_ID__c != null) { GeneralIntake.add(new General_Intakes__c( Name = '1', Primary_Contact__c = newContact.ID, Screening_ID__c = newContact.Screening_ID__c, Primary_Household_ID__c = newContact.Household_ID_del__c)); } } insert GeneralIntake; }
Thank you in advance for any help.
Hi brielea1984,
I have written a test class for you as shown below just copy and past into your org ,I believe it will work.
NOTE: In this test class you need to put value of the field "Screening_ID__c",If you need more clarification please let me know.
--
Thanks
Usman
All Answers
Hi brielea1984,
I have written a test class for you as shown below just copy and past into your org ,I believe it will work.
NOTE: In this test class you need to put value of the field "Screening_ID__c",If you need more clarification please let me know.
--
Thanks
Usman
It worked! I had to add more for the parent records to the Contact record, but copied what you did for Accounts. It was so helpful to be able to see a test code for my actual coding to get an understand as to what has to happen, i.e. writing code that inserts all parent objects/required fields. Thank you so much! I really appreciate it!
Thanks brielea1984!!
I was able to get 11 out of my 12 triggers covered, but it seems like Update needs something else. I tried copying what I'd done for all of the other triggers, but got 0% coverage each time. Any ideas? Essentially all this trigger does is update the status on the Service_Plan_Indicator__c to have the same status as the Goals__c.
Create a record of Goals__c,Service_Plan_Indicator__c sobjects and Client_Goal__c field should be Id of the Goals__c try to update in test method it will cover your complete code.
Note : Record should be created as per your logic written in the trigger on Goals__c.
--
Thanks
Usman