0

I have created a trigger to calculate number of Contract on Account.

Trigger- /* Provide summary of Number of Contacts on Account record */

trigger ContactSumTrigger on Contact (after delete, after insert, after undelete, after update) {

    Contact[] cons;
    if (Trigger.isDelete) 
        cons = Trigger.old;
    else
        cons = Trigger.new;

    // get list of accounts
    Set<ID> acctIds = new Set<ID>();
    for (Contact con : cons) {
            acctIds.add(con.AccountId);
    }

    Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([select Id, AccountId from Contact where AccountId in :acctIds]);

    Map<ID, Account> acctsToUpdate = new Map<ID, Account>([select Id, Number_of_Contacts__c from Account where Id in :acctIds]);

    for (Account acct : acctsToUpdate.values()) {
        Set<ID> conIds = new Set<ID>();
        for (Contact con : contactsForAccounts.values()) {
            if (con.AccountId == acct.Id)
                conIds.add(con.Id);
        }
        if (acct.Number_of_Contacts__c != conIds.size())
            acct.Number_of_Contacts__c = conIds.size();
    }

    update acctsToUpdate.values();

}

Test Class-

@isTest
private class ContactSumTriggerTest {

    static testMethod void contactSumTest() {
        Account acct = [select Id ,Number_of_Contacts__c from Account LIMIT 1];

        Contact tstCon = new Contact ( LastName = 'Test',
                                AccountId = acct.id
                                  );
        insert tstCon;

        tstCon.FirstName = 'Test';

        update tstCon;

        delete tstCon;

        undelete tstCon;
    }
}

ERROR Message - ContactSumTriggerTest.contactSumTest(), Details: System.QueryException: List has no rows for assignment to SObject Class.ContactSumTriggerTest.contactSumTest: line 26, column 1

6

Here you are querying for an account:

Account acct = [select Id ,Number_of_Contacts__c from Account LIMIT 1];

But there are NO account created at this point so your query will error.

To fix the error create an account and insert it. Should be no need for the query

1
  • 2
    I agree with Eric about the account. I would also suggest that you do some type of validation (via assertion) that the trigger is actually working.
    – JimRae
    Jan 11 '15 at 20:27
0

Try doing this :

@isTest
private class ContactSumTriggerTest {

static testMethod void contactSumTest() {

    Account testAct = new Account();
    testAct.Name = 'Test Act';
    insert testAct;

    Account acct = [select Id ,Name from Account WHERE Name='Test Act' LIMIT 1];
    Contact tstCon = new Contact ( LastName = 'Test',
                            AccountId = acct.id
                              );
    insert tstCon;

    tstCon.FirstName = 'Test';

    update tstCon;

    delete tstCon;

    undelete tstCon;
  }
}

Hope this helps :)

-1

The error is in test class method. You have not created any Account records but are trying to access it. Hence, the error - List has no rows for assignment. You have to create an Account record and insert it first. This will create data for test method.

3
  • Original........
    – Eric
    Jan 12 '15 at 14:24
  • Azinq, before answering it may be interesting to read the answers which are already posted. There is little value in posting an answer which states the same as an answer that's already there. If you agree with an answer, you can rather upvote it to show support. Jan 12 '15 at 15:59
  • Thanks Samuel. I am new here! Will keep that in mind next time. :)
    – ajinkyah
    Jan 13 '15 at 5:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.