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
megsumamegsuma 

Apex Trigger Not Respecting testMethod Code

Hi all, once again Apex has left me scratching my head. I have a trigger that I'm trying to test, here is my test method for that trigger:

 

 

	static testMethod void testCompanyGoalUpdate(){
		Company_Goal__c cmp = new Company_Goal__c();
		Company__c myCompany = new Company__c();
		cmp.Test__c = 'Test';
		cmp.Company__c = myCompany.Id;
		System.debug(cmp);
		insert cmp;
		delete cmp;		
	}

 Eclipse tells me:

 

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

 

In the snippet above I was sure I set Company__c properly with "cmp.Company__C = ..."

 

I am definitely open to suggestions, I'm not even sure I'm writing the test method quite right as this is my first test method for a trigger (after insert, after update).

 

 

 

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

krishnagkrishnag

here the problem is there is some validation rule hitting while u r trying to insert.check if u r missing something or else check ur company is having some recordtypes u need to mention that tooo while u r creating a test company.

krishnagkrishnag

the test methhod u have written is little bit confusing.

 

any test method u need to create a test account and insert it and then do some operation on testa ccount and insert again.

for wxample.

 

 

 

@isTest
private class Testupdateaccount{
public static testMethod void testaccUpdate(){
    
Account testAccount1 = new Account(RecordTypeId ='01280000000Pxf7',Name ='Test Accounts1',Company_Relationship__c = 'Customer',Relationship_Type__c = 'Current',dnb__D_U_N_S_Number_reg__c = '123456');insert testAccount1;
Account testAccount1 = new Account(RecordTypeId ='01280000000Pxf7',Name ='Test Accounts1');
insert testAccount1;

Account tempAccount1 = [select Id,Phone from Account where Id=:testAccount1.Id];

tempAccount1.Phone = '5124567890';
update tempAccount1;
}
}

 

 

 

krishnagkrishnag

sorry small change in above script

 

 

@isTest
private class Testupdateaccount{
public static testMethod void testaccUpdate(){
    
Account testAccount1 = new Account(RecordTypeId ='01280000000Pxf7',Name ='Test Accounts1');
insert testAccount1;

Account tempAccount1 = [select Id,Phone from Account where Id=:testAccount1.Id];

tempAccount1.Phone = '5124567890';
update tempAccount1;
}
}

 

 

Jeremy.NottinghJeremy.Nottingh

The problem is that myCompany is never inserted, so myCompany.id is null. You need to "insert myCompany" before adding its ID to cmp.Company__c. 

 

Jeremy