Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I am trying to create a test class for my apex trigger but I am not able to reach the code coverage. does anyone how I can reach the code coverage?

Here is trigger

trigger AccountInformation on Charter__c (Before Update) 
{
for(Charter__c c: trigger.new)
{
   List<Account> accts = [SELECT Invoice_Contact_1_Email__c,
                                 Invoice_Contact_2_Email__c,
                                 Invoice_Contact_3_Name__c,
                                 Invoice_Contact_2_Name__c,
                                 Invoice_Contact_1_Name__c,
                                 Invoice_Contact_3_Phone__c,
                                 Invoice_Contact_2_Phone__c,
                                 Invoice_Contact_1_Phone__c,
                                 Invoice_Contact_3_Email__c
                         FROM account WHERE ID =:c.Account__c];
   for (Account a: Accts)
   {
       c.Invoice_Contact_1__c = a.Invoice_Contact_1_Name__c;
         c.Invoice_Contact_2__c = a.Invoice_Contact_2_Name__c;
       c.Invoice_Contact_3__c = a.Invoice_Contact_3_Name__c;
       c.Invoice_Contact_1_Phone__c = a.Invoice_Contact_1_Phone__c;
       c.Invoice_Contact_2_Phone__c = a.Invoice_Contact_2_Phone__c;
       c.Invoice_Contact_3_Phone__c = a.Invoice_Contact_3_Phone__c;
       c.Invoice_Contact_1_Email__c = a.Invoice_Contact_1_Email__c;
       c.Invoice_Contact_2_Email__c = a.Invoice_Contact_2_Email__c;
       c.Invoice_Contact_3_Email__c = c.Invoice_Contact_3_Email__c;

   }
}
}

This trigger is suppose to insert values from an account to my custom object called charter before an update to the record. I have tested this in the sandbox and it is working well.

Here is my test class

@isTest
private class AccountingInforTesting {
  static testMethod void Test_Infor(){

  charter__c con = new charter__c(name='Test Charter 10', Account__c = '001Z000000eX3xlIAC', DCS__c = True);
  insert Con;
  con.name = 'Test';
  update con;
  delete con;
   }
}

The test class has given me only 25% of code coverage.

Any help on how I can make this work is appreciated. Thanks

share|improve this question
add comment

1 Answer

You're not using @isTest(SeeAllData=true), but you're using a hard-coded ID value. Change your test as follows:

@isTest
private class AccountingInforTesting {
  static testMethod void Test_Infor(){
  account acc = new account(name='test');
  insert acc;
  charter__c con = new charter__c(name='Test Charter 10', Account__c = acc.id, DCS__c = True);
  insert Con;
  con.name = 'Test';
  update con;
  delete con;
   }
}

Edit

Your trigger isn't bulk-safe, either. Use a map instead:

trigger AccountInformation on Charter__c (Before Update) 
{
  map<id, account> accs = new map<id, account>();
  for(charter__c c: trigger.new)
    accs.put(c.account__c, null);
  accs.remove(null);
  accs.putall([SELECT Invoice_Contact_1_Email__c,
                                 Invoice_Contact_2_Email__c,
                                 Invoice_Contact_3_Name__c,
                                 Invoice_Contact_2_Name__c,
                                 Invoice_Contact_1_Name__c,
                                 Invoice_Contact_3_Phone__c,
                                 Invoice_Contact_2_Phone__c,
                                 Invoice_Contact_1_Phone__c,
                                 Invoice_Contact_3_Email__c
                         FROM account WHERE ID in :accs.keyset()]);
for(Charter__c c: trigger.new)
{
   if(accs.containskey(c.account__c)) {
       account a = accs.get(c.account__c);
       c.Invoice_Contact_1__c = a.Invoice_Contact_1_Name__c;
       c.Invoice_Contact_2__c = a.Invoice_Contact_2_Name__c;
       c.Invoice_Contact_3__c = a.Invoice_Contact_3_Name__c;
       c.Invoice_Contact_1_Phone__c = a.Invoice_Contact_1_Phone__c;
       c.Invoice_Contact_2_Phone__c = a.Invoice_Contact_2_Phone__c;
       c.Invoice_Contact_3_Phone__c = a.Invoice_Contact_3_Phone__c;
       c.Invoice_Contact_1_Email__c = a.Invoice_Contact_1_Email__c;
       c.Invoice_Contact_2_Email__c = a.Invoice_Contact_2_Email__c;
       c.Invoice_Contact_3_Email__c = c.Invoice_Contact_3_Email__c;
    }
  }
}
share|improve this answer
 
This works now, Thank you so much. –  Yom130 Oct 26 '13 at 3:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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