+ Start a Discussion
ArunKumar KumkumathArunKumar Kumkumath 

testclasses on triggers

Can you please help me out on this..? I am trying to write a test class for a simple trigger. But some of the lines (italics) are not covered under the code coverage. Can anyone please guide me... Also i have not written any test classes for this trigger but it is showing coverage without a test class..?

trigger AccountDuplicateTrigger on Account (before insert)
{        
    for(Account a:Trigger.new)    
    {
        if(Trigger.isInsert)
        {                   
            if(a.Name !='') 
            {       
                string dupItem = '';
                List<Account> acc= new List<Account>();                   
                if((a.BillingStreet != null) && (acc.isEmpty())) 
                {
                    acc=[select ID from account where Name=:a.Name and BillingStreet=:a.BillingStreet]; 
                    if(acc.isEmpty()==False)                                                              
                        dupItem='Name \''+ String.escapeSingleQuotes(a.Name)+'\' and Billing street \''+
                        String.escapeSingleQuotes(a.BillingStreet)+'\'';

                }
                if((a.BillingCity != null) && (acc.isEmpty())) 
                {
                    acc=[select ID from account where Name=:a.Name and BillingCity=:a.BillingCity];    
                    if(acc.isEmpty()==False) 
                        dupItem='Name \''+ String.escapeSingleQuotes(a.Name)+'\' and Billing city \''+
                        String.escapeSingleQuotes(a.BillingCity)+'\'';

                }                                      
                else{                
                }
                
                //Alerts the user                    
                if(acc.isEmpty()==False) {
                    a.adderror('You cannot create a duplicate account here as the '+dupItem+' already belongs to a different account.');     
                }

            }
        }
    }
}
Best Answer chosen by ArunKumar Kumkumath
Deepak Pandey 13Deepak Pandey 13
Hello Arun,
 Actually you are written a process builder  that's why your trigger through this type of error. So you looked your process builder and insert all the Account other field which are using in process builder or you change the criteria of flow checked with null.

All Answers

Deepak Pandey 13Deepak Pandey 13
@istest
public class acctriggerTest
  { 
    static testMethod void Test()
    {  
        Account ObjAcc = new Account();
        ObjAcc.Name = 'TestAcc';
        ObjAcc.BillingStreet = 'BillingStreet';
        insert ObjAcc;
        
        Account ObjAcc1 = new Account();
        ObjAcc1.Name = 'TestAcc';
        ObjAcc1.BillingStreet = 'BillingStreet';
        insert ObjAcc1;
        
    }   
}
Akshay_DhimanAkshay_Dhiman
Hi Arun,

1)  In Your Trigger Code I have added a line at line no. 30, and put the addError() method inside that.
if(!Test.isRunningTest()) {}
 
2) addError() is one of the method which cannot be covered in any test class.
3) It will work for bulk insertion of accounts also.

//Now Your Trigger Code is like this:
trigger AccountDuplicateTrigger on Account (before insert)
{   
for(Account a:Trigger.new)    
{
    if(Trigger.isInsert)
    {              
        if(a.Name !='')
        {  
            string dupItem = '';
            List<Account> acc= new List<Account>();              
            if((a.BillingStreet != null) && (acc.isEmpty()))
            {
                acc=[select ID from account where Name=:a.Name and BillingStreet=:a.BillingStreet];
                if(acc.isEmpty()==False)                                                         
                    dupItem='Name \''+ String.escapeSingleQuotes(a.Name)+'\' and Billing street \''+
                    String.escapeSingleQuotes(a.BillingStreet)+'\'';
            }
            if((a.BillingCity != null) && (acc.isEmpty()))
            {
                acc=[select ID from account where Name=:a.Name and BillingCity=:a.BillingCity];    
                if(acc.isEmpty()==False)
                    dupItem='Name \''+ String.escapeSingleQuotes(a.Name)+'\' and Billing city \''+
                    String.escapeSingleQuotes(a.BillingCity)+'\'';
            }                                 
            else{           
            }
          
            //Alerts the user               
            if(acc.isEmpty()==False) {
                if(!Test.isRunningTest())              
                {
                    a.adderror('You cannot create a duplicate account here as the '+dupItem+' already belongs to a different account.');
                }
              
            }
        }
    }
}
}

 //And The Test Class For the above Code is:
 
@isTest
public  class TestAccountDuplicateTrigger {
@isTest public static void TestDuplicacy()
{
   List<Account> acclist=new List<Account>();
  
   Account acc=new Account();
   acc.Name='Test Account';
   acc.BillingStreet='Park Street';
   acc.BillingCity='New York';
   insert acc;
  
    Account acc1=new Account();
   acc1.Name='Test Account';
   acc1.BillingStreet='Park Street';
   acc1.BillingCity='New York';
   acclist.add(acc1);
    insert acclist;
   
}
@isTest public static void TestDuplicacy1()
{
   List<Account> acclist=new List<Account>();
  
   Account acc=new Account();
   acc.Name='Test Account';
   acc.BillingCity='New York';
   insert acc;
  
    Account acc1=new Account();
   acc1.Name='Test Account';
   acc1.BillingStreet='Park Street';
   acc1.BillingCity='New York';
   acclist.add(acc1);
    insert acclist;
 }
}

Hope this may help you.

Regards,
Akshay
ArunKumar KumkumathArunKumar Kumkumath
Thanks Deepak & Akshay for the reply..
When i use your code in my test class, it failed and throw an error - 

"System.DmlException: Insert failed. 
First exception on row 0; first error: CANNOT_EXECUTE_FLOW_TRIGGER, The record couldn&rsquo;t be saved because it failed to trigger a flow. A flow trigger failed to execute the flow with version ID 301j0000000bv9s. 
Flow error messages: &lt;b&gt;An unhandled fault has occurred in this flow&lt;/b&gt;&lt;br&gt;An unhandled fault has occurred while processing the flow.  Please contact your system administrator for more information.  Contact your administrator for help.: []"

It shows error on the insert Account.. 

Any ideas ?

Thanks in advance.
Deepak Pandey 13Deepak Pandey 13
Hello Arun,
 Actually you are written a process builder  that's why your trigger through this type of error. So you looked your process builder and insert all the Account other field which are using in process builder or you change the criteria of flow checked with null.
This was selected as the best answer
ArunKumar KumkumathArunKumar Kumkumath
Thanks Deepak.

It was really helpful. I have inactivated the process builder associated with accounts and it is executing fine. 
Deepak Pandey 13Deepak Pandey 13
Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.
ArunKumar KumkumathArunKumar Kumkumath
Deepak,
Is there any option to by pass the process builder for test class ?
 
Deepak Pandey 13Deepak Pandey 13
Arun ,
Whose field you are using in process builder insert record  above test class, then it should be pass.If this will not pass share your process builder.
ArunKumar KumkumathArunKumar Kumkumath
User-added image
ArunKumar KumkumathArunKumar Kumkumath
The above are the three processes. If i deactivate three test class is working fine.
 
Deepak Pandey 13Deepak Pandey 13
please insert all these field which are using in your all process builder in account object on test class .