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
Evan Maher 3Evan Maher 3 

test class contact field copy to account field

So I'm new to writing apex triggers and need to push a few into production. My trigger code is: 
 
trigger Days_Open on Contact (after insert,after update) {
    List<account> li=new list<Account>();
    List<Id> ids = new List<Id>();
    for(Contact c: trigger.new)
        ids.add(c.AccountId);
    Map<Id, Account> accountMap = new Map<Id, Account>([Select Id, Phone From Account Where Id In :ids]);
    for(Contact c: trigger.new)
    {
        Account a = accountMap.get(c.AccountId);
        if(a != null)
        {
            a.Days_Open__c = c.Days_Open__c;
            li.add(a);
        }
    }
    update li;
}
How do I write a test class? Trigger works in the environment. I need to have it check that when a contact record is saved it copies it to the account that the contact is related too. I currently cannot push into production as code coverage is at 0%
 
Best Answer chosen by Evan Maher 3
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi evan,
As per your logic you have to give valu to days_open__c fields also while inserting contact inorder to copy it to account.
try this code
@istest
private class testtrigger {
    @istest
     static void createRcds(){
        
        account acc = new account();
        acc.name = 'unit test';
        insert acc;
        
        contact con = new contact();
        con.lastname = 'contact1';
        con.days_open__c = 3;
        con.AccountId = acc.id;
        insert con;
        
        account a = [select id,days_open__c from account where id =:acc.id];
        system.assertEquals(con.days_open__c,a.days_open__c);
        
    }
    
}

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards

All Answers

Evan Maher 3Evan Maher 3
This is as far as I've gotten: 
@isTest
private class TestTrigger {
    @isTest
    static void testMethodOne() {
        // Create the Account Record.
        Account acc = new Account();
        acc.Name='Test Account' ;
        insert acc;

        // Create the Contact Record.
        Contact cont = new Contact();
        cont.FirstName='Test';
        cont.LastName='Test';
        cont.Email='demo@demo.com';
        cont.Accountid=acc.id;
        cont.Business_Description_Short__c='test';
        insert cont;
        
        // test whether this copies
        List<Account> testAccount = [SELECT Id FROM Account WHERE Id =: acc.id]; 
        System.assert(testAccount.size() == 1); 
        System.assert(acc.Business_Description_Short__c == cont.Business_Description_Short__c);
      

    }
}
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi evan,
As per your logic you have to give valu to days_open__c fields also while inserting contact inorder to copy it to account.
try this code
@istest
private class testtrigger {
    @istest
     static void createRcds(){
        
        account acc = new account();
        acc.name = 'unit test';
        insert acc;
        
        contact con = new contact();
        con.lastname = 'contact1';
        con.days_open__c = 3;
        con.AccountId = acc.id;
        insert con;
        
        account a = [select id,days_open__c from account where id =:acc.id];
        system.assertEquals(con.days_open__c,a.days_open__c);
        
    }
    
}

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
This was selected as the best answer
Evan Maher 3Evan Maher 3
Ahhhh sorry probably should have kept it consistent with the fields. That solution worked and I've managed to get it over to production. Working perfectly now :) thanks for your help