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
David GreenbergDavid Greenberg 

unable to insert Contract for testMethod on trigger


I have a before update trigger on Contract that sets the associated Account status to Active when the Contract is activated.  The business goal is to keep these values in sync.

I have written the below test method, but I am only able to obtain 57% test coverage, because an insert statement is failing in the method.

Here is the error:

20080202002751.821:Class.TriggerTests.testContractTrigger: line 22, column 17: Insert failed. First exception on row 0; first error: FAILED_ACTIVATION, null: [Status]

Our hunch is that the insert fails because of some special back-end logic around Contract status, which appears to be locked in some way by the platform. 

However, I am not certain if and how to bypass this logic (if that's the issue) for the test.

Here is the test method.  The failure occurs at the attempt to insert relatedCon.

Any suggestions would be appreciated:

public class TriggerTests {
    public static testmethod void testContractTrigger (  ) {
         //Insert Account for test
         Account testAcc = new Account( Name = 'Test For Contract');
                 insert testAcc;
             catch (DmlException e) {
        // Obtain reference to new related Contract
        Contract relatedCon = new Contract (AccountId = testAcc.Id, ContractTerm = 12, Name = 'Test', Status = 'Draft');      
                 insert relatedCon;
             catch (DmlException e) {
    // This is the Contract that will change as part of the test     
    // field values should cause the trigger to fire
    Contract updatedCon = new Contract(Id = relatedCon.Id, Status = 'Activated' );
    // update in database
                // this should fire the after update trigger   
                 update updatedCon;
             catch (DmlException e) {

    // Iterate through acct objects, make sure trigger set status to active   
    for ( Account a : [select Name, Id, AccountStatus__c from Account where Id = :testAcc.Id ] ) 
        System.debug(a.Name + ' ' + a.AccountStatus__c);
        //System.assertEquals( 'Active' , a.AccountStatus__c );
    Here is the trigger:

trigger setAcctActive on Contract (before update) {

// Create a list of accounts to update
List<Account> acctList = new List<Account>();

for (Contract contract : {

  // If the contract has been activated...
  if (contract.Status == 'Activated') {

    Account a = new Account(Id = contract.AccountId);
    // ... set the associated Account as active
    a.AccountStatus__c = 'Active';

    acctList.add ( a );

  // update the database with the account status changes
  update acctList;

Message Edited by David Greenberg on 02-01-2008 04:49 PM
try using upsert relatedCon instead of insert relatedCon...