You need to sign in to do that
Don't have an account?
carmant
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
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
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.
This is what I have so far :
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!
We need to see the trigger code
Its in the first post!
Thanks
So is this line 11:
Yep - but struggling to work out what the issue is?
The trigger works perfectly - its only the test class that gives that error..
Looks obvious to me.
Your test class first does this:
Which will fire your trigger.
The trigger will then reach this line:
And the error you are given is pretty descriptive - List has no rows for assignment.
Is this making any sense yet?
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.
I dont know anything about PersonAccounts. You could try adding some error trapping to your trigger.
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.
Great spot!
Changing:
acc.Name to acc.LastName
resolved it.
Thank you.