0

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];

    System.RunAs(u1)
    {
        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',
            LastName='User',
            Phone='01234 567890',
            Email='[email protected]',
            AccountId=acc.id
            );
        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,
            Status__c='valid'
            );
        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.

1 Answer 1

2

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

...
dis.Status__c='valid';
dis.someotherfield__c='test';

update dis;

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

System.assert(Limits.getEmailInvocations()==1);
1
  • 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
    Commented Jun 9, 2014 at 10:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .