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
adiazadiaz 

Test Class

Okay I've written my code and tested it in my sandbox; works as expected. When I tried to move it to production, I ran a the deployement test and I received the following msg: "Coverage of selected Apex Trigger is 0%...." . Can someone help me write a test class?

 

Below is my Trigger Code:

 

 

trigger CreateTicket on Maintenance_Request__c(after update)

{

    if(Trigger.isUpdate){
        for(Maintenance_Request__c m: Trigger.new)

       {

          if(m.Status__c == 'Pending Support')

          {

               Support_Team_Request__c st = new Support_Team_Request__c();

               //do your stuff, for ex

                  st.Your_name__c = m.Your_Name__c;
                  st.Client_reported_issue__c = 'No';
                   st.Short_description__c = m.Short_Description__c;
                   st.Client_Name__c = m.Client_Name__c;
                   st.Description_of_Issue__c = m.Detailed_Description__c;
                    st.Mailbox__c = m.Mailbox__c;
                    st.Priority__c = 'medium';
                    st.Created_From_MTR__c = 'Yes';
                    st.Request_Type__c = 'production' ;  
                    st.Participant_Name__c = m.Participant_Name__c;
                    st.Participant_ID__c = m.Participant_ID__c;
                    st.CC_Contact__c = m.CC_Contact__c;

                insert st;

          }
         
        }
    }  

}
Best Answer chosen by Admin (Salesforce Developers) 
Tim__mTim__m

No, you can't deploy a trigger to production without a unit test.

 

Here is what you need to get started. This unit test  will satisfy the requirement for production and will allow you to deploy your code. However you should add logic to the unit test to check that the trigger did what it should have done.

 

 

@isTest
private class CreateTicketTriggerTest {

static testMethod void myUnitTest() {
// setup a test Maintenance_Request__c sobject
Maintenance_Request__c mr = new Maintenance_Request__c();
mr.Status__c = 'New';
mr.Your_Name__c = 'Test User';
mr.Short_Description__c = 'Test request';
mr.Client_Name__c = 'Test Client';
mr.Detailed_Description__c = 'Test maintenance request';
mr.Mailbox__c = 'test@example.org';
mr.Participant_Name__c = 'Test Participant';
mr.Participant_ID__c = '123';
mr.CC_Contact__c = 'Test cc contact';

insert mr;

mr.Status__c = 'Pending Support';

update mr; // this will fire your tirgger.

// now you need use System.assert() to check that
// your trigger did what it should have done.
  }
}

 

 

All Answers

Tim__mTim__m

Did you include your unit test in the deployment package? You said you tested your tirgger in the sandbox so you have a unit test already right? If not then you didn't test it.

adiazadiaz

This is my first trigger and am very unfamiliar with demployement from sandbox to production. If the unit test is not auto created by the trigger, then the answer is no. Instructions on how to include it in the deployement is greatly appreciated.

Tim__mTim__m

It would be nice if the unit test was auto created but no dice! You need to write it your self. Here is a wiki you should read: http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods 

 

After you write your unit test then deploy your trigger and your unit test to production. What tool are you using to deploy?

adiazadiaz

I will give it a try. Thanks!

 

I'm using Force IDE on eclipse.

Tim__mTim__m

Eclipse is what I use most of the time.

 

So from Eclipse just add the trigger and the unit test for the trigger to the deploy package. You can multi select the files from the package explorer using the Ctrl key then right click one of the files and select Force.com --> Deploy to server. Good luck!

adiazadiaz

I read the guide in the link provided. Not sure what to do after the first few lines.

 

Can the code be doployed without being tested?

Tim__mTim__m

No, you can't deploy a trigger to production without a unit test.

 

Here is what you need to get started. This unit test  will satisfy the requirement for production and will allow you to deploy your code. However you should add logic to the unit test to check that the trigger did what it should have done.

 

 

@isTest
private class CreateTicketTriggerTest {

static testMethod void myUnitTest() {
// setup a test Maintenance_Request__c sobject
Maintenance_Request__c mr = new Maintenance_Request__c();
mr.Status__c = 'New';
mr.Your_Name__c = 'Test User';
mr.Short_Description__c = 'Test request';
mr.Client_Name__c = 'Test Client';
mr.Detailed_Description__c = 'Test maintenance request';
mr.Mailbox__c = 'test@example.org';
mr.Participant_Name__c = 'Test Participant';
mr.Participant_ID__c = '123';
mr.CC_Contact__c = 'Test cc contact';

insert mr;

mr.Status__c = 'Pending Support';

update mr; // this will fire your tirgger.

// now you need use System.assert() to check that
// your trigger did what it should have done.
  }
}

 

 

This was selected as the best answer
adiazadiaz

You sir are a genious! Thanks!