• abirla19
  • NEWBIE
  • 90 Points
  • Member since 2014
  • Ankur Birla

  • Chatter
    Feed
  • 3
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies
Hi, I'm struggling with test class to increase test coverage, currently got only 68%, so all suggestions welcome

my trigger:
/* Provide summary of Number of Contacts on Account record
https://success.salesforce.com/answers?id=90630000000h3mNAAQ */ 

trigger ContactSumTrigger on Contact (after delete, after insert, after undelete, 
after update) {

    Contact[] cons;
    if (Trigger.isDelete) 
        cons = Trigger.old;
    else
        cons = Trigger.new;

    // get list of accounts
    Set<ID> acctIds = new Set<ID>();
    for (Contact con : cons) {
            acctIds.add(con.AccountId);
    }
    
    Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([select Id
                                                            ,AccountId
                                                            from Contact
                                                            where AccountId in :acctIds]);

    Map<ID, Account> acctsToUpdate = new Map<ID, Account>([select Id
                                                                 ,Number_of_Contacts__c
                                                                  from Account
                                                                  where Id in :acctIds]);
                                                                 
    for (Account acct : acctsToUpdate.values()) {
        Set<ID> conIds = new Set<ID>();
        for (Contact con : contactsForAccounts.values()) {
            if (con.AccountId == acct.Id)
                conIds.add(con.Id);
        }
        if (acct.Number_of_Contacts__c != conIds.size())
            acct.Number_of_Contacts__c = conIds.size();
    }

    update acctsToUpdate.values();

}

my class:
 
@isTest
public class ContactSumTriggerTest {

    static testmethod void TestContactTrgr() {
    
           Test.startTest();
        // Create account
           Account ac = new Account();
           ac.Name = 'Test Account';
           ac.Mailing_Country__c = 'United Kingdom';
           Insert ac;
           system.debug('Completed Account Creation'); 
		   
		// Update account
          Database.SaveResult sr2 = Database.update(ac, true);
          System.assert(sr2.isSuccess());
          system.debug('Account updated');

           delete ac;
           undelete ac;
		   
        // Create 1st contact
           Contact ct1 = new Contact();
           ct1.FirstName = 'FirstName 1';
           ct1.LastName = 'LastName 1';
           ct1.Email = 'test1@test.com';
           ct1.Mailing_Country__c = 'United Kingdom';
           ct1.Account = ac;
           Insert ct1;  
		   system.debug('Completed Contact Creation'); 
		   
		// Update 1st contact
          Database.SaveResult sr2 = Database.update(ct1, true);
          System.assert(sr2.isSuccess());
          system.debug('Contact updated');
		   
		   delete ct1;
           undelete ct1;
		   
		// Create 2nd contact
		   Contact ct2 = new Contact();
           ct2.FirstName = 'FirstName 2';
           ct2.LastName = 'LastName 2';
           ct2.Email = 'test2@test.com';
           ct2.Mailing_Country__c = 'United Kingdom';
           ct2.Account = ac;
           Insert ct2;
           system.debug('Completed Contact Creation'); 
		   
		// Update 2nd contact
          Database.SaveResult sr2 = Database.update(ct2, true);
          System.assert(sr2.isSuccess());
          system.debug('Contact updated');
           
		   delete ct2;
           undelete ct2;
           
           Test.stopTest();
          
    }    
    
}

thank you in advance
I am trying to extract records in an object which has around 940K records using Data Loader and I was not able to open the extracted CSV file, is there a way where I can extract all the records ....
Hi..
please tell me about salesforce 1 .
i saw some pdf about salesforce 1 but i didn't understand from this pdf.

please explain simple way
Thanks
satheesh
Hi, I'm struggling with test class to increase test coverage, currently got only 68%, so all suggestions welcome

my trigger:
/* Provide summary of Number of Contacts on Account record
https://success.salesforce.com/answers?id=90630000000h3mNAAQ */ 

trigger ContactSumTrigger on Contact (after delete, after insert, after undelete, 
after update) {

    Contact[] cons;
    if (Trigger.isDelete) 
        cons = Trigger.old;
    else
        cons = Trigger.new;

    // get list of accounts
    Set<ID> acctIds = new Set<ID>();
    for (Contact con : cons) {
            acctIds.add(con.AccountId);
    }
    
    Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([select Id
                                                            ,AccountId
                                                            from Contact
                                                            where AccountId in :acctIds]);

    Map<ID, Account> acctsToUpdate = new Map<ID, Account>([select Id
                                                                 ,Number_of_Contacts__c
                                                                  from Account
                                                                  where Id in :acctIds]);
                                                                 
    for (Account acct : acctsToUpdate.values()) {
        Set<ID> conIds = new Set<ID>();
        for (Contact con : contactsForAccounts.values()) {
            if (con.AccountId == acct.Id)
                conIds.add(con.Id);
        }
        if (acct.Number_of_Contacts__c != conIds.size())
            acct.Number_of_Contacts__c = conIds.size();
    }

    update acctsToUpdate.values();

}

my class:
 
@isTest
public class ContactSumTriggerTest {

    static testmethod void TestContactTrgr() {
    
           Test.startTest();
        // Create account
           Account ac = new Account();
           ac.Name = 'Test Account';
           ac.Mailing_Country__c = 'United Kingdom';
           Insert ac;
           system.debug('Completed Account Creation'); 
		   
		// Update account
          Database.SaveResult sr2 = Database.update(ac, true);
          System.assert(sr2.isSuccess());
          system.debug('Account updated');

           delete ac;
           undelete ac;
		   
        // Create 1st contact
           Contact ct1 = new Contact();
           ct1.FirstName = 'FirstName 1';
           ct1.LastName = 'LastName 1';
           ct1.Email = 'test1@test.com';
           ct1.Mailing_Country__c = 'United Kingdom';
           ct1.Account = ac;
           Insert ct1;  
		   system.debug('Completed Contact Creation'); 
		   
		// Update 1st contact
          Database.SaveResult sr2 = Database.update(ct1, true);
          System.assert(sr2.isSuccess());
          system.debug('Contact updated');
		   
		   delete ct1;
           undelete ct1;
		   
		// Create 2nd contact
		   Contact ct2 = new Contact();
           ct2.FirstName = 'FirstName 2';
           ct2.LastName = 'LastName 2';
           ct2.Email = 'test2@test.com';
           ct2.Mailing_Country__c = 'United Kingdom';
           ct2.Account = ac;
           Insert ct2;
           system.debug('Completed Contact Creation'); 
		   
		// Update 2nd contact
          Database.SaveResult sr2 = Database.update(ct2, true);
          System.assert(sr2.isSuccess());
          system.debug('Contact updated');
           
		   delete ct2;
           undelete ct2;
           
           Test.stopTest();
          
    }    
    
}

thank you in advance
I am trying to extract records in an object which has around 940K records using Data Loader and I was not able to open the extracted CSV file, is there a way where I can extract all the records ....
Hi..
please tell me about salesforce 1 .
i saw some pdf about salesforce 1 but i didn't understand from this pdf.

please explain simple way
Thanks
satheesh