2

I am learning how to write unit tests for my email trigger. I want to test it with 100 emails, please see comments in code.

public static testMethod void testEmail() {
  User u1 = [SELECT Id FROM User WHERE profileID IN (SELECT id FROM Profile WHERE Name = 'System Administrator') AND IsActive = true LIMIT 1];
  System.RunAs(u1) {
    ApexTrigger myTrigger = [SELECT Id, Status FROM ApexTrigger WHERE name = 'EmailTrigger'];
    Boolean isActive = (myTrigger.Status == 'Active');

    //here I am now inserting 100 contacts
    for (Integer count = 0; count++; count < 100) {
      insert new Contact [] { new Contact (FirstName = 'John'+count, LastName ='Doe'+count) };
    }
    //now this is where the problem occurs, I now need to create my patients, which lookup 
    //my contacts, but how do I do that? 
    //to create a single patient, I would write:
    Patient__c p = new Patient (Description = 'descr', Contact__c = 'not sure how to assign from above');
    insert p;
  }
}

Does this make sense? So, I know how to create 100 contacts, but I do not know how to create the 100 patients. Let's just say that each patient maps to exactly one contact. How would I do that? TIA.

user8737
  • 823
  • 2
  • 12
  • 21

1 Answers1

2

First off, you should 'bulkify' your Contact creation so that you are not performing DML in a loop.

There is a great article on the Salesforce Developers site which covers how and why to bulkify your code in more detail. This blog post by David Liu is also a good reference.

//here I am now inserting 100 contacts
List<Contact> contacts = new List<Contact>();

for (Integer count = 0; count < 100; count++) {
    contacts.add(new Contact (FirstName = 'John'+count, LastName ='Doe'+count));
}

insert contacts;

Since you now have a list of Contact objects you can interate over it to create your Patient objects easily:

List<Patient__c> patients = new List<Patient__c>();

for(Contact c : contacts)
{
    patients.add(new Patient (Description = 'descr', Contact__c = c.Id));
}

insert patients;
user8737
  • 823
  • 2
  • 12
  • 21
Alex Tennant
  • 13,571
  • 4
  • 58
  • 85
  • Thanks, that makes sense. Now I ran my test and I get an error message: System.DmlException: Insert Failed. Any ideas? – user8737 Jun 23 '14 at 10:02
  • @user8737 Does the error message give you any more detail than that, a line number for example? Is the error occurring in your test class or your trigger code? – Alex Tennant Jun 23 '14 at 10:05
  • yes, it says: System.DmlException: Insert failed. First exception on row 0 with id 003m000 Class.EmailTrigger_UnitTest: line 194, column 1 --- that line is the line where I say: insert contacts; – user8737 Jun 23 '14 at 10:13
  • @user8737 There should be some more detail in there telling you the exact error, for example INVALID_FIELD_FOR_INSERT_UPDATE or CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY. It sounds like there is a problem somewhere within your trigger code, which likely means it is outside of the scope of this question. This should help you out: http://salesforce.stackexchange.com/questions/36582/how-do-i-start-to-debug-my-own-apex-code – Alex Tennant Jun 23 '14 at 10:19
  • now I found the complete error message: System.DmlException: Insert failed. First exception on row 0 with id 003m0000004IYLbAAO; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] --- Do you have any ideas? Thanks for the link above, I will also have a look through that. – user8737 Jun 23 '14 at 10:29
  • Oh dear - I had a duplicate insert contacts; statement in my code! It works well now. Thanks! – user8737 Jun 23 '14 at 10:38