You need to sign in to do that
Don't have an account?

How to write a test class for merge delete
test class for merge delete
I have my trigger to capture the deleted record during merge and insert into new custom obj. I have a test class for it, but its code coverage is only 46%? how this can be increased?
trigger MergeAccounts on Account (after delete)
{
if(trigger.isafter && trigger.isdelete)
{
List<Account_Audit__c> deletedaccount = new List<Account_Audit__c>();
for (Account acct:trigger.old)
{
If(acct.masterRecordId!=null)
{
Account_Audit__c Aaudit = new Account_Audit__c();
Aaudit.Losing_Account_Name__c= acct.name;
Aaudit.Losing_Owner__c=acct.OwnerId;
Aaudit.Merge_date_time__c=date.today();
Aaudit.Merged_by_user__c=UserInfo.getName() ;
deletedaccount.add(Aaudit);
}
}
if(deletedaccount.size()>0)
{
database.insert(deletedaccount);
}
}
testclass
@istest
Public class MergeAccountstest
{
public static testMethod void mergetest()
{
Account a = new Account(name='test');
insert a;
delete a;
}
public static testMethod void Auditmergetest()
{
Account_Audit__c b = new Account_Audit__c(
Losing_Account_Name__c = 'test',
Losing_Owner__c = '005q00000012y6X',
Merge_date_time__c = date.today(),
Merged_by_user__c = UserInfo.getName());
insert b;
}
}
I have my trigger to capture the deleted record during merge and insert into new custom obj. I have a test class for it, but its code coverage is only 46%? how this can be increased?
trigger MergeAccounts on Account (after delete)
{
if(trigger.isafter && trigger.isdelete)
{
List<Account_Audit__c> deletedaccount = new List<Account_Audit__c>();
for (Account acct:trigger.old)
{
If(acct.masterRecordId!=null)
{
Account_Audit__c Aaudit = new Account_Audit__c();
Aaudit.Losing_Account_Name__c= acct.name;
Aaudit.Losing_Owner__c=acct.OwnerId;
Aaudit.Merge_date_time__c=date.today();
Aaudit.Merged_by_user__c=UserInfo.getName() ;
deletedaccount.add(Aaudit);
}
}
if(deletedaccount.size()>0)
{
database.insert(deletedaccount);
}
}
testclass
@istest
Public class MergeAccountstest
{
public static testMethod void mergetest()
{
Account a = new Account(name='test');
insert a;
delete a;
}
public static testMethod void Auditmergetest()
{
Account_Audit__c b = new Account_Audit__c(
Losing_Account_Name__c = 'test',
Losing_Owner__c = '005q00000012y6X',
Merge_date_time__c = date.today(),
Merged_by_user__c = UserInfo.getName());
insert b;
}
}
Can you try the below test class,
You have to merge Accounts, then only masterRecordId will get populated on deleted record.
Try below code
Regards,
Bhanu Mahesh
Mark this as 'SOLVED' if your issue resolved