2

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;
        }
    }

1 Answer 1

2

Reason:

The reason you are not able to cover the update part is, you are doing that in same test method.

checkRecursive.checkOneTime() will be checked when inserting the account and i assume, it will change the boolean to reverse(false) so that it won't work again. So when you do an update, the boolean is still in reversed position(false) and won't enter the if condition.

Suggestion:

Create another test method and have the same code as the current one. But surround the updation of Account with Test.startTest() and Test.stopTest() which will reset the Governor limit as well as any static values.

Hope it helps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .