+ Start a Discussion
saariko.ax241saariko.ax241 

Newbie Question

I am new to force.com

 

I am learning, and for some reasong, nothing works.

 

I have created a trigger:

 

//update Fax if LastName equals saar trigger TestContactNEw on Contact (after insert) { if (Trigger.new[0].LastName == 'saar') Trigger.new[0].Fax = '555'; }

 

I have created a testclass

 

@isTest private class TestNewContact { static testMethod void myUnitTest() { // TO DO: implement unit test Contact c = new Contact(); c.LastName = 'saar'; test.startTest(); insert c; test.stopTest(); } }

 

I am getting 2 errors

1. on the test class I get on the insert c line:

 

// Error Severity and Description Path Resource Location Creation Time Id System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TestContactNEw: execution of AfterInsert caused by: System.Exception: Record is read-only Trigger.TestContactNEw: line 6, column 24 Test/src/classes TestNewContact.cls line 31 1237986283019 35

 

 2. on the trigger I get

 

Severity and Description Path Resource Location Creation Time Id Average test coverage across all Apex Classes and Triggers is 50%, at least 75% test coverage is required Test line 1 1237985915541 32

 

 

I don't have ANY other trigger or class in my organization, and if these small simple just work out for me, ill know how to move on (hopefully)

 

Appreciate your input.

 

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae

It looks better, not sure about the coverage. You don't need to add the updated records to the new list, since you aren't processing it anyway.  This should be enough.  If you still get no coverage, look at the details of the coverage (usually at the bottom of the results and see which lines are not getting covered. My guess is the top message is a bit deciving, and you are actually getting 100% coverage on this trigger.

 

 

trigger test1 on Contact (before insert) { for (Contact c : Trigger.new) { if (c.LastName == 'saar') { c.Fax = '4444'; } } }

 

 

 

All Answers

JimRaeJimRae

You should use a before insert trigger for this type of update, not after.  Once the object is inserted, it becomes read only for that transaction.  Changing the trigger to a before insert should fix both of your errors.

 

note that your trigger was not designed to be bulk safe. If for some reason more than one contact were inserted at the same time, for example: with the data loader, or even with Outlook syncronization, your trigger would only effect the first record in the array being passed in.

It is good practice to always treat the Trigger.new as an array and build to process multiple records in a loop.

saariko.ax241saariko.ax241

Thanks Jim for the prompt reply, I have done the following changes:

 

trigger (which is now bulk safe)

trigger test1 on Contact (before insert) {

List<Contact> updContact = new List<Contact>();
// Loop through Contacts
for (Contact c : Trigger.new)
{
if (c.LastName == 'saar')
{
c.Fax = '4444';
}
updContact.add(c);
}
}

 

 

 Class changed to:

 

@isTest
private class TestContact {

static testMethod void myUnitTest() {
// TO DO: implement unit test
Contact[] contacts = new Contact[]{new Contact(LastName = 'saar')};

insert contacts;
}
}

 

 Now I get that the average test is 0% when 75% is required.

 

I also get an error:

 

Severity and Description Path Resource Location Creation Time Id System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, test1: execution of BeforeInsert caused by: System.Exception: Cannot start testing while executing a trigger Trigger.test1: line 5, column 5 Test/src/classes TestContact.cls line 29 1237990198666 53

 

 

 

 

Appreciate your help

 

Message Edited by saariko on 03-25-2009 04:11 PM
Message Edited by saariko on 03-25-2009 04:24 PM
JimRaeJimRae

It looks better, not sure about the coverage. You don't need to add the updated records to the new list, since you aren't processing it anyway.  This should be enough.  If you still get no coverage, look at the details of the coverage (usually at the bottom of the results and see which lines are not getting covered. My guess is the top message is a bit deciving, and you are actually getting 100% coverage on this trigger.

 

 

trigger test1 on Contact (before insert) { for (Contact c : Trigger.new) { if (c.LastName == 'saar') { c.Fax = '4444'; } } }

 

 

 

This was selected as the best answer
saariko.ax241saariko.ax241

Thanks Jim,

 

It works.

 

Now as this was my "Hello World" I will go and start my real issue. That will be in a new thread.

 

Thanks a lot for your help.

 

*I am still a bit confused about all this DaaS, but I hope I will get it.