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
Vijay A 8Vijay A 8 

Please help me test class

Hi All,
Below is my trigger code with test class. Please help me test class for code coverage
trigger PropUpdate on D_D__c (after insert, after update) {
    
    set<id> accids = new set<id>();
    set<D_D__c> amc_recs = new set<D_D__c>();
    List<D_D__c>Amc_Lst_Upd = New List<D_D__c>();
    map<Id, Id> mp = new map<Id, Id>();
    if((Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)) && checkRecursive.runOnce()){
        try{
            for (D_D__c am : Trigger.new){
                accids.add(am.Account__c);
                amc_recs.add(am);
                //mp.add(am.Id, am.Account__c);
            }
            if(accids.size() >= 1){
                for(Property__c pr : [SELECT Id,Account__c  FROM Property__c where Account__c IN:accids]){
                    mp.put(pr.Account__c, pr.Id);
                }
                
                if(!mp.isEmpty() && !amc_recs.isEmpty()){
                    D_D__c amc = new D_D__c();
                    system.debug('amc_recs.size()====='+amc_recs.size());
                    for(D_D__c amc_lst : amc_recs){
                        
                        amc.Id = amc_lst.Id;
                        amc.Property__c = mp.get(amc_lst.Account__c);
                        Amc_Lst_Upd.add(amc);
                    }
                    Update Amc_Lst_Upd;
                }
            }
        } 
        catch (Exception e) {
            System.debug(e);
        }
    }
}

-----------------------------------------------------------------

@isTest
public class PropUpdateTest {
    
    public static testmethod void PropUpdateTestmeth(){
        test.startTest();
        
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
        
        Account acc = new Account();
            acc.user__c = u.id;
            acc.AccountSource = 'Walk-in';
            acc.Name = 'testaccount';
        	
            insert acc;
        
        D_D__c dd = new  D_D__c();
            dd.account__c = acc.id;
            dd.Status__c = 'Active';
            dd.Actual_Amount__c = 2000;
            insert dd;   
         Test.stopTest();
        
    }
}

Thank you Advance.
AnudeepAnudeep (Salesforce Developers) 
Hi Vijay - Can you highlight the lines of code that are not covered with your test class? Thanks
Vijay A 8Vijay A 8
Hi Anudeep thank you for reply. Below lines are not covering
try{
            for (D_D__c am : Trigger.new){
                accids.add(am.Account__c);
                amc_recs.add(am);
                //mp.add(am.Id, am.Account__c);
            }
            if(accids.size() >= 1){
                for(Property__c pr : [SELECT Id,Account__c  FROM Property__c where Account__c IN:accids]){
                    mp.put(pr.Account__c, pr.Id);
                }
                
                if(!mp.isEmpty() && !amc_recs.isEmpty()){
                    D_D__c amc = new D_D__c();
                    system.debug('amc_recs.size()====='+amc_recs.size());
                    for(D_D__c amc_lst : amc_recs){
                        
                        amc.Id = amc_lst.Id;
                        amc.Property__c = mp.get(amc_lst.Account__c);
                        Amc_Lst_Upd.add(amc);
                    }
                    Update Amc_Lst_Upd;
                }
            }
        } 
        catch (Exception e) {
            System.debug(e);
        }