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
TiborMTiborM 

Basic test method help

Hi everyone,

 

i'm new to Apex code and i hope that somebody helps me.

 

I need write test method for apex trigger for object Account, Contact and Lead.

 

I have trigger:

 

trigger AccountCountryPush on Account (before insert, before update) {

...
...

	if (Trigger.isUpdate)
	{
		for (Account item : Trigger.old)
		{ 	
			item.BillingCountry = item.Country__c;
	         }
	}
}

 

and I have "test" method which doesn't work:

 

@isTest
private class CountryPUSHTests {
    static testMethod void newAccountCountryUpdateTest()
    {
    	Account[] items = [Select id, name, Country__c from Account where Country__c = 'Slovakia'];
    	for (Account i : items)
    	{
    		i.Country__c = 'Russia';
    	}
    }
}

 

Country__c is custom obejct - picklist.

We are using it for territory management and we need when this field is changed on objects above then start trigger and in trigger change some others field to same value.

 

 

Please could anybody explain me what I'm doing bad ?

Ispita_NavatarIspita_Navatar

Your trigger fires on :-

1. Insert

2. Update

Hence in order to attain complete coverage for your trigger one needs to do the following in the test method code:-

1. Insert some account records say 1-2

2. Change the value of any field of Account and then update

 

The should give appropriate coverage to your test method.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 

bob_buzzardbob_buzzard

You should also assert that the trigger is doing what it is supposed to - i.e. that billing country has been updated with the value from Country__c.

 

TiborMTiborM

forgot beforeInsert ... this is covevered 100% by another test method when beforeUpdate part is commented... (test method created 2 accounts..)

 

focus only on beforeUpdate trigger...

 

What you mean  "Change the value of any field of Account and then update"????

 

if you look on my test methot above.. what is wrong? could you rewrite to usable form?

Ispita_NavatarIspita_Navatar

 

        Account inst = new Account(RecordTypeId = RecInst,Familiar_Name__c='@isTest1',Name='@isTest1');
        insert inst;
        Account inst2 = new Account(RecordTypeId = RecInst,Familiar_Name__c='@isTest2',Name='@isTest2');
        insert inst2;

       inst.Familiar_Name__c='@isTest11';

       update inst;

       inst2.Familiar_Name__c='@isTest11';

       update inst;

       Something like above should give appropriate coverage to your trigger.

        Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

TiborMTiborM

Ispita_Navatar wrote:

 

        Account inst = new Account(RecordTypeId = RecInst,Familiar_Name__c='@isTest1',Name='@isTest1');
        insert inst;
        Account inst2 = new Account(RecordTypeId = RecInst,Familiar_Name__c='@isTest2',Name='@isTest2');
        insert inst2;

       inst.Familiar_Name__c='@isTest11';

       update inst;

       inst2.Familiar_Name__c='@isTest11';

       update inst;

       Something like above should give appropriate coverage to your trigger.

        Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.


THIS HELPS with 100% coverage!!!!!

 

 

But when I want to deploy trigger above I got this error:

 

 

Run Failures:
  CountryPUSHTests.AccountCountryUpdateTest System.DmlException: Update failed. First exception on row 0 with id 0013000000XDkiYAAT; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountCountryPush: execution of BeforeUpdate

caused by: System.Exception: Record is read-only

Trigger.AccountCountryPush: line 13, column 4: []

 

 

Why is record READ-ONLY??? I deploying it with System Administrator account....

 

 

Ispita_NavatarIspita_Navatar

Have you written the trigger on After Update event and then in it tried to change/ set  the value of a field , because in after update or after insert the records are read-only so in order to change the triggered instance of record you have to fire the before insert and before update.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.