I wrote one trigger for sync Account and Contact, it is working perfect but when i am going to create TEST case for that and not able to update (Insertion is working).
Here is my trigger code :
trigger SyncAccToCon on Account (after insert, before update) {
List<Contact> newCon = new List<Contact>();
Account ac = Trigger.New[0];
if(Trigger.isInsert && checkRecursive.checkOneTime()){
for (Account a: Trigger.New) {
Contact c = new Contact();
c.Name = a.Name;
c.Type = a.Company__c;
newCon.add(c);
}
insert newCon;
}
if(Trigger.isUpdate && checkRecursive.checkOneTime()){
Account acOld = Trigger.Old[0];
Contact c = new Contact();
if(acOld != null){
c = [select Id, Name, Type from Contact where Name =: acOld.Name];
c.Name = ac.Name;
c.Type = ac.Company__c;
}
if(c.Id != null){
Update c;
}
}
}
Test Class :
@isTest
Private Class TestSyncAccToContact{
Static testMethod void myUnitTest(){
Account a = new Account();
a.Name = 'Test sync';
insert a;
update a;
Contact c = new Contact();
c.Name = 'Test sync';
insert c;
c = [select Id, Name from Contact where Name = 'Test sync' Limit 1];
c.Name = 'Testing';
update c;
}
}