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
calmarkcalmark 

unable to deploy simple trigger, i have no idea what i am doing!

oh my...you stop programming for 10 years, the world changes, and you have to start from step 1.  I need help :)

 

I created in my sandbox the following trigger:

 

trigger Update_Last_VM_Left on Task (after insert, after update)
{
Task ts= Trigger.new[0];
if(ts.Status =='VM Left'){
Contact contactToUpdate = new Contact(id=ts.WhoID);
contactToUpdate.Last_VM_Left__c = ts.activitydate;
}
}

 

I am not trying to deploy it to production and it fails because:

 

Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
Average test coverage across all Apex Classes and Triggers is 0%, at least 75% test coverage is required

 

I am clueless as to what I need to do next, any help would be greatly appreicated!

Best Answer chosen by Admin (Salesforce Developers) 
Madhan Raja MMadhan Raja M

Hi Calmark,

 

Yes, Test.startTest() and Test.stopTest() method should be outside the for loop.

 

@isTest
private class TaskTestMethod{
static testMethod void TestTasktrigger()
    {
            Task[] CreateTask = new Task[]{};
            for(Integer x=0; x<100;x++){
            Task ts = new Task(Subject='Apex Test',Status='VM Left');
            CreateTask.add(ts);    
            }  
        Test.startTest();
        insert CreateTask;
        Test.stopTest(); 
    }
}

 

Accept as Solution if it serves your question.

 

Madhan Raja M

 

 

All Answers

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

You will need to write test class for the trigger you have written...

 

refer to this link for more on writing test classes..

 

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

calmarkcalmark

Thanks Sam, I don't suppose you can give me a hint/sample as to what my test class would look like for the trigger I created...

Madhan Raja MMadhan Raja M

Hi Calmark,

 

Try this test class: you will get 100% code coverage.

 

@isTest
private class TaskTestMethod{
static testMethod void TestTasktrigger()
    {
            Task[] CreateTask = new Task[]{};
            Contact[] CreateContact = new Contact[]{};
            for(Integer x=0; x<100;x++){
            Task ts = new Task(Subject='Apex Test',Status='VM Left');
            CreateTask.add(ts);
            Test.startTest();
            insert CreateTask;
            Test.stopTest();   
        }  
    }
}

 

Madhan Raja M

calmarkcalmark

Thank you Madhan!

 

I did get 100% coverage but when I run the test I get the following message:

 

System.FinalException: Testing already started

 

 

and

 

(System Code) Class.TaskTestMethod.TestTasktrigger: line 10, column 1

 

Any idea what I have to change to make it run without error?

calmarkcalmark

I think I figured it out, i just had to move the startTest code down one, here's the code that gives me 100% coverage and runs:

 

@isTest
private class TaskTestMethod{
static testMethod void TestTasktrigger()
    {
            Task[] CreateTask = new Task[]{};
            Contact[] CreateContact = new Contact[]{};
            for(Integer x=0; x<100;x++){
            Task ts = new Task(Subject='Apex Test',Status='VM Left');
            CreateTask.add(ts);    
            }  
        Test.startTest();
        insert CreateTask;
        Test.stopTest(); 
    }
}

Madhan Raja MMadhan Raja M

Hi Calmark,

 

Yes, Test.startTest() and Test.stopTest() method should be outside the for loop.

 

@isTest
private class TaskTestMethod{
static testMethod void TestTasktrigger()
    {
            Task[] CreateTask = new Task[]{};
            for(Integer x=0; x<100;x++){
            Task ts = new Task(Subject='Apex Test',Status='VM Left');
            CreateTask.add(ts);    
            }  
        Test.startTest();
        insert CreateTask;
        Test.stopTest(); 
    }
}

 

Accept as Solution if it serves your question.

 

Madhan Raja M

 

 

This was selected as the best answer