I am new to Apex coding and I am currently working on an example where I have two custom objects:

Disease__c and Patient__c

These two custom object have a Master Detail Relationship (DiseaseR__c), where Disease__c is the Master Side.

I now want to write a test method to check the following scenario.

Disease__c has a status field "Status__c". I want to send an email once the status field has changed from "under review" to "valid". Below is my code for the test method.

public static testMethod void NotifyPatients_UnitTest()
    User u1 = [SELECT Id FROM User WHERE profileId IN (SELECT id FROM Profile WHERE Name = 'System Administrator') LIMIT 1];

        ApexTrigger myTrigger = [Select Id, Status from ApexTrigger where name='diseaseTrigger'];
        Boolean isActive = (myTrigger.Status == 'Active');

        Account acc = new Account(Name='Test Account');
        insert acc;

        Contact con = new Contact(  FirstName='Test',
            Phone='01234 567890',
        insert con;

        RecordType rt = [SELECT Id FROM RecordType WHERE Name='contageous' and SObjectType='Disease__c'];

        Disease__c dis= new Disease__c( RecordType = rt,
            Status__c='under review'
        insert dis;

        Patient__c pp = new Patient__c(  Description__c = 'Test',
            DiseaseR__c = dis.Id,
            Contact__c = con.Id
        insert pp;

        /*this is where I am starting to fail - how do I create an update in my test?
          my code is obviously wrong, but I cannot figure out the correct syntax*/
       dis= ( RecordType = rt,
        update dis;

        /*The other thing is now, how do I assert that an email is sent after the field was updated?*/

Thank you so much for your help in advance. Reena.


If you want to update "dis" all you have to do is


update dis;

To test that the email was sent you can assert it doing something like this

  • Hello - thanks for your reply. This makes sense, but now I get a new error when trying to run my test: System.DmlException: Insert failed. First exception on row 0 with id 003m00000045x2PAAQ; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] - do you have any idea what is wrong? tia – user8737 Jun 9 '14 at 10:47

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.