You need to sign in to do that
Don't have an account?
ArunKumar 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.');
}
}
}
}
}
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.');
}
}
}
}
}
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
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;
}
}
1) In Your Trigger Code I have added a line at line no. 30, and put the addError() method inside that.
3) It will work for bulk insertion of accounts also.
//Now Your Trigger Code is like this:
//And The Test Class For the above Code is:
Hope this may help you.
Regards,
Akshay
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’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: <b>An unhandled fault has occurred in this flow</b><br>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.
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.
It was really helpful. I have inactivated the process builder associated with accounts and it is executing fine.
Is there any option to by pass the process builder for test class ?
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.