im trying to write tests for this afterUpdate contact trigger:
private void mergeSlaveToMaster(){
map<Id, Contact> mapOldContact = (Map<Id,Contact>) Trigger.oldMap;
for(Contact c: (List<Contact>) Trigger.new){
try
{
if(c.Merge_With__c != mapOldContact.get(c.id).Merge_With__c)
{
Id masterId = [SELECT Merge_With__r.Id from Contact where id=:c.Merge_With__c][0].Id;
Contact masterContact = [SELECT ID from Contact where id=:masterId];
merge masterContact c;
}
}
catch (DmlException e)
{
System.debug('An unexpected error has occurred: ' + e.getMessage());
}
}
}
that's the test:
@isTest private static void migrateSlaveContactToMaster() {
Account accountTest = TestUtil.getPartnerAccountByDunsAndName('AccountTest','123456789');
accountTest.Partner_Type__c = 'Full Partner';
accountTest.DunsNumber = '123456789';
insert accountTest;
String acctId = [SELECT Id from Account][0].Id;
Contact contactTest = TestUtil.getContactByName('ContactTest1');
contactTest.AccountId = acctId;
insert contactTest;
string firstContactId = [SELECT Id FROM Contact WHERE Name='ContactTest1FirstName ContactTest1LastName'][0].Id;
Contact contactTest2 = TestUtil.getContactByName('ContactTest2');
contactTest2.AccountId = acctId;
insert contactTest2;
string secondContactId = [SELECT Id FROM Contact WHERE Name='ContactTest2FirstName ContactTest2LastName'][0].Id;
contactTest2.Merge_With__c = firstContactId;
update contactTest2;
integer sum = [SELECT count() FROM Contact WHERE AccountId =:acctId];
System.assertEquals(1,sum);
}
when i run it, i receive this error in the debug log:
An unexpected error has occurred: Merge failed. First exception on row 0 with id 0036E00000AH2DjQAL; first error: ENTITY_IS_DELETED, entity is deleted: []
im not sure why this error occures, what am i doing wrong? please help
for
loop and that too; two SOQL's – Rohit Mourya May 10 '17 at 9:33Merge_With__c
is not empty before proceed. – Ashwani May 10 '17 at 10:51