+ Start a Discussion
carmantcarmant 

Test Class for Trigger (insert Case when Account created)

Surprise surprise...after hacking a trigger together I am struggling with the test class...

 

The trigger is pretty simple - it creates a Case when an Account is created with certain criteria (may be worth pointing out our org uses PersonAccounts)

 

Here is the trigger:

 

//define trigger and when it fires
trigger CreateCase on Account (after insert) {
  
  //create list for cases to be inserted
  List<Case> newcase = new List<Case>();

  //find the ID of the correct record type
  Id rtId = [select Id, name from RecordType where name ='My Record Type' and   SObjectType='Case' limit 1].Id;

  //find contact ID which relates to the Account created to attach to the to case
  Id personContact = [select id, Name from Contact where Contact.AccountId in : Trigger.New].Id;

  //define criteria when the trigger fires
  for (Account acc: Trigger.New) {
    if (acc.Corporate_Account_Link_Text__c == 'My Corporate Account') {
      

      //define what values to put against the new case
      newcase.add(new Case(
            AccountId = acc.Id,
            ContactId = personContact,
            RecordTypeId = rtId,
            Type='My Type',
            Origin='My Origin',
            Status='My Status',
            Call_Attempt__c='1'
          )
      
        );
        }
    
      //insert the new case(s)
      insert newcase;  
    }
}

 

 

 

Have had a go with the test class, but not come up with much useful...any pointers appreciated!

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
carlocarlo

Found this in the Apex Programming pdf

 

Note: If your organization has enabled person accounts, you have two different kinds of accounts: business accounts

and person accounts. If your script creates a new account using name, a business account is created. If the script uses

LastName, a person account is created.

All Answers

Rajesh SriramuluRajesh Sriramulu

Hi

 

@is test

private class CreateCase_test{

static testmethod void CreateCase_test()

{

 

Account acc = new Accoutn();

acc.name= 'test';

insert acc;

 

like this insert for cases and contact

 

and insert the values according to if conditions 

 

}}

 

Let me know any issue and if it solves ur problem plz accept it solution.

carmantcarmant

This is what I have so far :

 

@isTest

public class testCreateCase {

  static testMethod void testCC () {


    Id rtId = [select Id, name from RecordType where name = 'My Record Type' and SObjectType = 'Case' limit 1].Id;
    

    //create account
    Account acc = new Account();
    //enter details  
    acc.Name = 'Test Account';
    acc.Corporate_Member__c = TRUE;
    acc.Corporate_Account_Link__c = '001W0000004Hu64';
    insert acc;
  
    
    Id personContact = [select Id, Name from Contact where Contact.Name = 'Test Account'].Id;

    //create case
    Case c = new Case();
    //enter details
    c.AccountId = acc.Id;
    c.ContactId = personContact;
    c.RecordTypeId = rtId;
    c.Type = 'My Type';
    c.Origin = 'My Origin';
    c.Status = 'My Status';
    c.Call_Attempt__c = '1';    
    insert c;

    }

    }

 

 

But I am only getting 37% coverage, and its failing with this error -

 

Message:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateCase: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.CreateCase: line 11, column 1: []

 

Stack Trace:

Class.testCreateCase.testCC: line 17, column 1

 

 

Stuck!

carlocarlo

We need to see the trigger code

carmantcarmant

Its in the first post!

 

Thanks

carlocarlo

So is this line 11:

 

Id personContact = [select id, Name from Contact where Contact.AccountId in : Trigger.New].Id;
carmantcarmant

Yep  - but struggling to work out what the issue is?

 

The trigger works perfectly - its only the test class that gives that error..

carlocarlo

Looks obvious to me.

 

Your test class first does this:

 

//create account
    Account acc = new Account();
    //enter details  
    acc.Name = 'Test Account';
    acc.Corporate_Member__c = TRUE;
    acc.Corporate_Account_Link__c = '001W0000004Hu64';
    insert acc;

 

Which will fire your trigger.

 

The trigger will then reach this line:

 

Id personContact = [select id, Name from Contact where Contact.AccountId in : Trigger.New].Id;

And the error you are given is pretty descriptive - List has no rows for assignment.

 

Is this making any sense yet?

 

 

carmantcarmant

OK...so its complaining because it can't find a Contact with the AccountId of the Account I insert, right?

 

But we use PersonAccounts - so we never actually make Contacts. I am not 100% sure how it works behind the scenes, but I was under the impression that Person Accounts are an amalgamation of a Contact and an Account? and when you create a Person Account, the Contact is normally created automatically?

 

Thanks.

carlocarlo

I dont know anything about PersonAccounts.  You could try adding some error trapping to your trigger.

carlocarlo

Found this in the Apex Programming pdf

 

Note: If your organization has enabled person accounts, you have two different kinds of accounts: business accounts

and person accounts. If your script creates a new account using name, a business account is created. If the script uses

LastName, a person account is created.

This was selected as the best answer
carmantcarmant

Great spot!

 

Changing:

 

acc.Name to acc.LastName

 

resolved it.

 

Thank you.