You need to sign in to do that
Don't have an account?
KevinRussell
Trigger Test has no code coverage, how to cover code?
So, new at this..... Having said that...
I wrote a trigger that sets a Contact field checkbox to True. I created a test for the trigger and the test ran but there is no code coverage. What am I doing wrong, how do I connect my trigger test to my real trigger to get coverage?
Thanks!
Here is my trigger:
// Update Contact field: "Guest_Speaker__c" to TRUE when a new Interaction__c record is inserted or updated. // Interaction__c is not a directly related child object of Contact. trigger Contacts_Guest_Speaker_Checkbox on Interaction__c (after insert, after update) { // Will store Contact record ID map< id, contact > contacts = new map< id, contact >(); // Create trigger for new or selected Interaction__c record for(Interaction__c record:trigger.new) if(record.Selected_Sub_type__c == 'Speaker') // Update checkbox field on the Contact record to TRUE contacts.put(record.contact__c, new contact(id=record.contact__c, Guest_Speaker__C = TRUE)); update contacts.values(); }
Here is my attempt at writing a test. The test does compile, run and passes but it has no code coverage.
@isTest private class Contacts_Guest_Speaker_CheckboxTest { //set condition for True statement. static TestMethod void Test0_StatementTrue() { string test0Value = 'Speaker'; updateCheckBox(test0Value); } //set condition for False statement. static TestMethod void Test1_StatementFalse() { string test1Value = 'Instructor'; updateCheckBox(test1Value); } //set checkbox to True if value is 'Speaker', False otherwise. private static void updateCheckBox(String inputString) { contact con = new contact(); con.Id = '003J000000O2KR0'; if (inputstring == 'Speaker') { con.Guest_Speaker__C = True; } else { con.Guest_Speaker__C = False; } update con; } }
Results:
Apex Test Result Detail Time Started 3/26/2013 10:40 AM Class Contacts_Guest_Speaker_CheckboxTest Method Name Test1_StatementFalse Pass/Fail Pass Error Message Stack Trace
Code Coverage:
Code Coverage Contacts_Guest_Speaker_Checkbox (Code Covered: 0%) line source 1 // Update Contact field: "Guest_Speaker__c" to TRUE when a new Interaction__c record is inserted or updated. 2 // Interaction__c is not a directly related child object of Contact. 3 trigger Contacts_Guest_Speaker_Checkbox on Interaction__c (after insert, after update) { 4 5 // Will store Contact record ID 6 map< id, contact > contacts = new map< id, contact >(); 7 8 // Create trigger for new or selected Interaction__c record 9 for(Interaction__c record:trigger.new) 10 11 if(record.Selected_Sub_type__c == 'Speaker') 12 13 // Update checkbox field on the Contact record to TRUE 14 contacts.put(record.contact__c, new contact(id=record.contact__c, Guest_Speaker__C = TRUE)); 15 16 update contacts.values(); 17 18 }
Hi NaiduPothini and sfdcfox,
Thank you both very, very,very much!
The test for this trigger finally passed. I have a lot to learn....
Here is the test code as it is now, before I tweak anything else:
All Answers
Thanks sfdcfox,
I'm refactoring my trigger test.
I'm trying to write a new Interaction record to cause the trigger to fire, but I can't get it to compile:
"Error: Compile Error: Field is not writeable: Interaction__c.Interaction_Type__c at line 20 column 9"
The fields in question are selectable picklist values.
Both of these fields are required so I need to populate them to insert a new Interaction record.
How do I set the values for those fields?
(Account and Contact IDs, I'll figure how to code these after I get the test working)
Here is what I've written so far.
Interaction_Type__c can't be required if it's a formula; don't worry, the system will calculate it for you. Subtype, however, needs to have a value. Also note, if there's no ID, you need to use insert or upsert, not update (because it doesn't exist in the database).
try this.
Sill gives the "Field is not writable" message when I try to compile it...
Error: Compile Error: Field is not writeable: Interaction__c.Selected_Sub_type__c
try this.
Same deal.
Error: Compile Error: Field is not writeable: Interaction__c.Interaction_Type__c at line 12 column 114
Thanks NaiduPothini, I'm getting closer, the code compiled, the test failed, but, this time it must be due to the required pick list field value:
This is a Interaction object pick list where the value would be set to 'Speaker'. Is there an equivelent query I can use for picklists?
Here is the saved/compiled code:
I guess you are getting the error message from the third query. can you check the recordtype name?
Can you post the debug log here?
I think this is the log:
Let me know if there is a more detailed log I can retrieve.
(0/1) Test Methods Passed.
on the Interaction object. I'll get the Pick List values.
CDC_Interaction: Posting, Company Briefing, Visit, Speaker, etc..... didn't think you needed all.
Field Name: Sub_type
API Name: Sub_type__c
Object Name: Interaction
Data Type: Picklist
Record Type Name: CDC_Interaction
check the above record type names... if you could get the recordtypeIDs then i guess this would work.
you can get more detailed logs from the log sheet in the debug logs.
Your Name > Setup > Administrative Setup > Monitoring > debug logs.
Click on new and add your user name so that you can see the debug logs generated...
NaiduPothini, I finally saw what you needed me to check on. I ended up deleting the 3rd row because I kept getting errors on it. I could not exactly identify why that record was failing. Now I'm hoping the issue is the sub_type pick list....
Here is the current test code:
Here is the log from the test:
Hi NaiduPothini and sfdcfox,
Thank you both very, very,very much!
The test for this trigger finally passed. I have a lot to learn....
Here is the test code as it is now, before I tweak anything else: