function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RahulRahul 

Getting only 30% code coverage for the following code. Need Help

trigger updateFundraiser on Account (before update) { 
    set<id> conIds = new set<id>(); 
    for(account a1: trigger.new) {
        if(a1.Contact_Associated__c != null) {
            conIds.add(a1.Contact_Associated__c);
        }
    }
    system.debug('conIds::'+conIds);
    map<id,contact> conMap = new map<id,contact>([select id,Name, Beneficiary_Name__c,Email, MobilePhone from contact where id IN :conIds]);
    system.debug('conMap::'+conMap);
    for(account a1:trigger.new){
        system.debug('a1.Contact_Associated__c::'+a1.Contact_Associated__c);
        system.debug('!!!!!!'+trigger.oldMap.get(a1.id).Contact_Associated__c);
        //if(a1.Contact_Associated__c != trigger.oldMap.get(a1.id).Contact_Associated__c) {
            system.debug('herere!!!');
           a1.Mobile_Number__c = conMap.get(a1.Contact_Associated__c).MobilePhone;
            a1.Created_By_Email__c = conMap.get(a1.Contact_Associated__c).Email;   
            a1.Name = conMap.get(a1.Contact_Associated__c).Name;   

            system.debug('herere!!!');
            system.debug('a1'+a1);   
        //}
    }
   
    

}
Best Answer chosen by Rahul
Abdul KhatriAbdul Khatri
Sorry Summit, actually the code is correct I just messed up with type. Here is 100% coverage
 
@isTest   
public class UpdateFundraiserTest {

    static testMethod void updateAccountTest() {
        
        Account account = new Account (Name = 'Test Account');
        insert account;
        
        Contact contact = new Contact (FirstName = 'Test', LastName = 'Contact');
        insert contact;
        
        Contact contactAssociated = new Contact (FirstName = 'Test', LastName = 'Associated');
        contactAssociated.MobilePhone = '(111) 111-1111';
        contactAssociated.Email = 'associatec@test.com';
        insert contactAssociated;

        account.Contact_Associated__c = contactAssociated.Id;
        update account;
        
        Account accountResult = [SELECT Id, Name, Mobile_Number__c, Created_By_Email__c FROM Account WHERE Id = :account.Id];
        Contact contactResult = [SELECT Id, Name, MobilePhone, Email FROM Contact WHERE Id = :contactAssociated.Id];        

        System.assert(accountResult.Name == contactResult.Name);
        System.assert(accountResult.Mobile_Number__c == contactResult.MobilePhone);
        System.assert(accountResult.Created_By_Email__c == contactResult.Email);        

    } 
}

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Can you please share your test class. So that we can help in updating same test class
Abdul KhatriAbdul Khatri
Can you check if this can help you. I just made as per the code.
 
@isTest   
public class UpdateFundraiserTest {

    static testMethod void updateAccountTest() {
        
        Account account = new Account (Name = 'Test Account');
        insert account;
        
        Contact contact = new Contact (FirstName = 'Test', LastName = 'Contact');
        insert contact;
        
        Contact contactAssociated = new Contact (FirstName = 'Test', LastName = 'Associated');
        contactAssociated.MobilePhone = '(111) 111-1111';
        contactAssociated.Email = 'associatec@test.com';
        insert contactAssociated;

        account.Contact_Associated__c = contactAssociated.Id;
        update contact;
        
        Account accountResult = [SELECT Id, Name, Mobile_Number__c, Created_By_Email__c FROM Account WHERE Id = :account.Id];
        Contact contactResult = [SELECT Id, Name, MobilePhone, Email FROM Contact WHERE Id = :contact.Id];        
        
        System.assert(accountResult.Name == contactResult.Name);
        System.assert(accountResult.Mobile_Number__c == contactResult.MobilePhone);
        System.assert(accountResult.Created_By_Email__c == contactResult.Email);        

    } 
}

 
RahulRahul
Hi Abdul, Thanks for the Reply but your code is not Working.
devedeve
Hi Sumit,

Please try this code:

@isTest   
public class UpdateFundraiserTest {

    @isTest    
     static void updateAccountTest() {

        Contact contactAssociated = new Contact (FirstName = 'Test', LastName = 'Associated');
                                                 contactAssociated.MobilePhone = '(111) 111-1111';
                                                 contactAssociated.Email = 'associatec@test.com';
        insert contactAssociated;

       Account account = new Account (Name = 'Test Account', Contact_Associated__c = contactAssociated);
        insert account;

        account.Name= 'New Name';
        update account;
        Account updatedAccount = [SELECT Id, Name, Mobile_Number__c,Created_By_Email__c FROM Account];

        System.assert(updatedAccount.Name == contact.Name);
        System.assert(updatedAccount.Mobile_Number__c == contact.MobilePhone);
        System.assert(updatedAccount.Created_By_Email__c == contact.Email);        

    }
}
Abdul KhatriAbdul Khatri
Sorry Summit, actually the code is correct I just messed up with type. Here is 100% coverage
 
@isTest   
public class UpdateFundraiserTest {

    static testMethod void updateAccountTest() {
        
        Account account = new Account (Name = 'Test Account');
        insert account;
        
        Contact contact = new Contact (FirstName = 'Test', LastName = 'Contact');
        insert contact;
        
        Contact contactAssociated = new Contact (FirstName = 'Test', LastName = 'Associated');
        contactAssociated.MobilePhone = '(111) 111-1111';
        contactAssociated.Email = 'associatec@test.com';
        insert contactAssociated;

        account.Contact_Associated__c = contactAssociated.Id;
        update account;
        
        Account accountResult = [SELECT Id, Name, Mobile_Number__c, Created_By_Email__c FROM Account WHERE Id = :account.Id];
        Contact contactResult = [SELECT Id, Name, MobilePhone, Email FROM Contact WHERE Id = :contactAssociated.Id];        

        System.assert(accountResult.Name == contactResult.Name);
        System.assert(accountResult.Mobile_Number__c == contactResult.MobilePhone);
        System.assert(accountResult.Created_By_Email__c == contactResult.Email);        

    } 
}

 
This was selected as the best answer
RahulRahul
Thanks deve and Abdul for your Answers. Yes Abdul this code has 100% coverge. Thanks Again.