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
fatmonkfatmonk 

Test trigger help

I am writing my first trigger test: cover =0% 

Message:

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You Must Enter A Charge: []

 

Stack Trace

Class.TestUpdateCharge.testChargeContractTrigger: line 6, column 1

 

 

 

@isTest
  public Class TestUpdateCharge {

  static testMethod void testChargeContractTrigger(){
 Charge__c c = new Charge__c (City__c = 'Oakland');
  insert c; <-- line 6


c = [select id,contract__c,Opportunity__c from Charge__c where City__c = 'Oakland'];

System.assertEquals(null, c.contract__c);
System.assertEquals(null, c.Opportunity__c);

}

}

Thanks,
JitendraJitendra

Hi,

 

You have custom Validation for some field, so before inserting the object "c", populate it with all the required values.

HariDineshHariDinesh

Hi,

 

The Error message clearly says the Insert is Failed, Because of some validation.

Here by the insert statement you have written in test method didn’t insert the record.

The reason might be a validation rule exists on that object or some other mandatory fields on that object to form a record.

Please check those and make sure to insert record (charge__c) with required/proper data.

 

updated same at below

http://boards.developerforce.com/t5/General-Development/Test-trigger-help/td-p/484671

 
fatmonkfatmonk

Thanks,

When i insert the Requiment  I have a different error msg  but Stack trace is line 6.

 

Msg:

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Opportunity__c]: [Opportunity__c]

 

 

Stack Trace:

Class.TestUpdateCharge.testChargeContractTrigger: line 6, column 1

 

@isTest
public Class TestUpdateCharge {

    static testMethod void testChargeContractTrigger(){
        Charge__c c = new Charge__c (Monthly_Recurring_Charge__c = 111.00);


        insert c; <-- line 6
     
     
     c = [select id,contract__c,Opportunity__c  from Charge__c where Monthly_Recurring_Charge__c = 111.00];

     System.assertEquals(null, c.contract__c);
     System.assertEquals(null, c.Opportunity__c);     
   
    }

}

 

Thanks again

SamuelDeRyckeSamuelDeRycke

Again, as the exception message states, there is a missing field missing. I expect your Charge__c custom object has a required lookup to an Opportunity record.  You'll have to make one in your test method, insert it and assign a reference to the relation field in your Charge__c record before you insert that.