function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NonickNonick 

Trigger Test Method doesn't work

I'm just learning how to make triggers and have finally made one that works in dev environment to update a custom lookup field in an opportunity. This is so that in a mail merge I can call details of the billing contact.

Code: Primary Contact Update Field Trigger
trigger SetPrimaryContact on Opportunity (before update) {
   for (Opportunity o : Trigger.new) {
//     if (o.IsClosed && !Trigger.oldMap.get(o.id).IsClosed) {
          OpportunityContactRole contactRole = [select ContactID from OpportunityContactRole where IsPrimary = true and OpportunityId = :o.id];
 

  if (contactRole != null) {
  o.Primary_Contact__c = contactRole.ContactID;
//  }
  }
  }
}

 The reason for the commenting out of that 'if' function is because I have a rather intensive validation process on closed opportunities, I just want to get the thing working at this point without having to create 3 related objects for what should be a simple field update formula.

The test method class is here
Code:TestTriggers class
public class TestTriggers {
  static testMethod void myTest() {
   Date d = date.newInstance(2050, 6, 6);
    Opportunity o = new Opportunity(name='test2', StageName='1.0 - Territory', CloseDate = d);
    insert o;
    o.name = 'test3';
    update o;
    System.assert((o.Primary_Contact__c == null) || (o.Primary_Contact__c != null));
    if(o.Primary_Contact__c != null){
     Contact testC = [select name from Contact where id=:o.Primary_Contact__c];
     System.assert(testC != null);
    }
    System.assert(o.CloseDate == d);
    try {
      delete o;
    } catch (DmlException e) {
      System.assert(false);
    }
 }

}

 The error thrown is a consistant "Average test coverage across all Apex Classes and Triggers is 50%, at least 75% test coverage is required"

And:
"System.DmlException: Update failed. First exception on row 0 with id 00620000006FcQkAAK; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, SetPrimaryContact: execution of BeforeUpdate

caused by: System.QueryException: List has no rows for assignment to SObject

Trigger.SetPrimaryContact: line 4, column 47    TestTriggers.cls    Primary Contact/src/unpackaged/classes    line 7    Force.com run test failure"

The annoying part is that this error wasn't thrown before.


As an additional question, why does Eclipse throw the error that the trigger xml is not saved, is it not being tested? How does one go about testing
Code:
<—xml version="1.0" encoding="UTF-8"–>
<ApexTrigger xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>13.0</apiVersion>
    <status>Active</status>
</ApexTrigger>

 


PS, bump this thread if you are sick of hearing "Work Around"





Message Edited by Nonick on 10-13-2008 10:48 PM

Message Edited by Nonick on 10-13-2008 10:49 PM

Message Edited by Nonick on 10-13-2008 10:50 PM
David VPDavid VP

First you got to get rid of the DML exception. Your code coverage will not go up as long as the test doesn't get any further then the offending line.

The 'List has no rows for assignment to SObject' points to :


Code:
OpportunityContactRole contactRole = [select ContactID from OpportunityContactRole where IsPrimary = true and OpportunityId = :o.id];

 
and is basically saying that your query returned no results. (there's no OpportunityContactRole that satisfies your where clause)

You'll need to set up some more test Objects and values to make sure that all objects exist and are related before running the rest of your test.


Hope this helps,


David