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
gowtham murugesangowtham murugesan 

This is my trigger and test class, but test class code coverage is 73% i don't know where i did mistake

Trigger :

trigger AccountUpdateTrigger on Invoice__c (after insert) {
    Set<String> customerLeadon = new Set<String>();
    Set<Id> accountId = new Set<Id>();
    Map<Id,String> invoiceMap = new Map<Id,String>();
    Map<String,Id> accountMap = new Map<String,Id>();
    Map<Id,String> accountNameMap = new Map<Id,String>();
    List<Invoice__c> invoiceUpd = new List<Invoice__c>();
    if(Trigger.isAfter && Trigger.isInsert && RecursiveStopping.isRecursive == True){
        RecursiveStopping.isRecursive = false;
        for(Invoice__c inv:Trigger.new){
            if(inv.AccountNumber__c.contains('12868') && inv.LeadOn__c.contains('000')){
                accountId.add(inv.Account__c);
                
              }
          }
        System.debug('ACCId=='+accountId);
        if(accountId.size()>0){
            for(Account acc:[SELECT Id,Name FROM Account WHERE Id IN:accountId]){
                accountNameMap.put(acc.Id,acc.Name);
            }
            for(Invoice__c inv:Trigger.new){
                System.debug('Acc='+inv.Account__c);
                String accName = accountNameMap.get(inv.Account__c);
                if(accName.contains('Echuca Camera House') && inv.AccountNumber__c.contains('12868') && inv.LeadOn__c.contains('000')){
                    customerLeadon.add(inv.AccountNumber__c+'   '+inv.LeadOn__c);
                    invoiceMap.put(inv.Id,inv.AccountNumber__c+'   '+inv.LeadOn__c);
                }
            }
            System.debug('CUST=='+customerLeadon);
            
            if(customerLeadon.size()>0){
                for(Account acc:[SELECT Id,Name,Customer_Account_and_Lead_On__c FROM Account WHERE Customer_Account_and_Lead_On__c IN: customerLeadon]){
                    accountMap.put(acc.Customer_Account_and_Lead_On__c,acc.Id);
                } 
            }
            if(accountMap.size()>0){
                for(Invoice__c inv:Trigger.new){
                    Invoice__c invUpd = new Invoice__c();
                    invUpd.Id = inv.Id ;
                    if(accountMap.get(invoiceMap.get(inv.Id)) != null){
                        invUpd.Account__c = accountMap.get(invoiceMap.get(inv.Id));
                    }
                    invoiceUpd.add(invUpd);
                }
                update invoiceUpd;
            }
        }
    }
}




My Test Class:


@istest
public class AccountUpdateTriggerTest {
    
    @istest
    public static void testData(){
       Account acc = new Account();
       acc.Name = 'Raleru Account';
       acc.Shipping_Address_Maps_To__c = '12868   000';
       acc.Customer_Account_and_Lead_On__c = '12868   000';
       insert acc;
       
       Account acc1 = new Account();
       acc1.Name = 'Echuca Camera House';
       acc1.Shipping_Address_Maps_To__c = '12868   600';
       acc1.Customer_Account_and_Lead_On__c = '12868   600';
       insert acc1;
        
      System.debug('ACC=='+acc);
      System.debug('Acc1=='+acc1);
        
      Invoice__c inv  = new Invoice__c();
      inv.Name = '332456';
      inv.InvoiceNumber__c = '332456';
      inv.Account__c = acc1.Id;
      inv.AccountNumber__c = '12868';
      inv.LeadOn__c = '000';
      insert inv;
      System.debug('CUST SQL='+[SELECT Id,Name,Customer_Account_and_Lead_On__c FROM Account WHERE Customer_Account_and_Lead_On__c =: '12868   000']);  
      
    }
    
}

 
Sunil RathoreSunil Rathore
Hi Gowtham,

Can you please highlight which sections are getting covered and which not of your trigger? It will help to identify the exact problem?

Many Thanks,
Sunil Rathore
Sunil RathoreSunil Rathore
Hi Gowtham,

Please replace the 
acc1.Customer_Account_and_Lead_On__c = '12868   600';
with the following line:
acc1.Customer_Account_and_Lead_On__c = '12868 000';
Adjust space as per your code between 12868  and 000.
Hope this will solve your problem. If does then mark it as best answer so it can also help others in the future.

Many Thanks,
Sunil Rathore
Sunil RathoreSunil Rathore
Hi Gowtham,

Please check the debug log for the below line after the execution of the test class and let me know the output in the debug log.
System.debug('CUST=='+customerLeadon);
Many Thanks,
Sunil Rathore
Sunil RathoreSunil Rathore
Hi Gowtham,

It seems you have not replace
acc1.Customer_Account_and_Lead_On__c = '12868   600';
with the following line:
acc1.Customer_Account_and_Lead_On__c = '12868  000';
 in your test class

Please make sure and replace this or update the 600 to 000

Many Thanks,
Sunil Rathore