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
LeebiesLeebies 

Deploy Trigger

I wrote the following trigger (based on the one from the Cook Book)

Code:
trigger NewCase on Service_Reports__c (after insert) {
    List<Case> cases = new List<Case>();
    for (Service_Reports__c newCase: Trigger.New) {
        if (newCase.Name != null) {
            cases.add(new Case(Subject = 'Remedial Case', Type = 'Remedial Work'));
        }
    }

    insert cases;
}

 
Problem is I dont know how to deploy it from Sandbox to Live.  In Eclipse there is no deploy button.  I read a lot of comments about developing a test class, then running it before a Deploy, but I dont know a) what one of those is, or how to go about it!

Would appreciate any help!
JimRaeJimRae
Use the eclipse right click context menu, either at the project level or on the src folder.  One of the options is "Deploy to server"
make sure you have your testmethod created as well and can achieve 75% coverage at a minimum.
Force2b_MikeForce2b_Mike
Leebies,

I struggled with this up until yesterday when I discovered two important things:
  • You need to create a Test (Apex) Class that manually triggers your trigger. I did this by writing some simple code to select a single record by ID, made a change to a field used by my trigger, and then saved the record. The Summer '09 release of the Force IDE for Eclipse will create a Test Class structure for you. My testing code is as follows (make sure the ID you uses exists in Production if you hardcode it like I did):
    String cTestSessionID = 'a0930000001YZ0q';
    System.debug('Get the training session for testing');
    final Integer nCanceledCount = 1;
    integer totalCanceled;

    totalCanceled = 0;
    List<DF_TrainingSession__c> updatedSession = new List<DF_TrainingSession__c>();
    for(DF_TrainingSession__c ts:[SELECT ID, Status__c FROM DF_TrainingSession__c
    WHERE ID = :cTestSessionID]) {
    System.debug('Mark as Canceled');
    ts.Status__c = 'Canceled';
    updatedSession.add(ts);
    totalCanceled += 1;
    }
    update updatedSession ;
    System.assertEquals(nCanceledCount, totalCanceled);
  • After running the tests in the Sandbox environment, I had to deploy BOTH the TestClass and the Trigger from my SandBox to Production. This was done by selecting both items in the Eclipse Navigator on the right side, right-click and select Force->Deploy To Server. Then you should be able to select the Production server and deploy the items.
The requirement to deploy both the test class and the trigger doesn't make a lot of sense to me and I could not find this documented anywhere in the Apex docs. Luckily I saw a passing comment about it in a discussion thread somewhere.

Hopefully this will work for you.

Mike
JonPJonP
In addition to the right-click > Force.com > Deploy to Server command, in the Winter '09 Force.com IDE (released on Monday) there's also a Deploy to Server button on the toolbar, to the left of the salesforce.com icon.


LeebiesLeebies
Hi!

Thanks everyone for the advice.  Found where I need to deploy etc, but really struggling with writing a test class and how to associate it.  Any geinuses out there wanna pm me some tips? :):):)
JimRaeJimRae
Save the test class in the development environment, and then deploy both to production at the same time.
Could be something as simple as this (might even work with a little tweaking):
 
Code:
@isTest
public class testServiceReportTrg{
 static testMethod void validateServiceReportTrg() {

 //create a test object to fire the trigger include all of the required values needed to create the record
 Service_Reports__c testnewcase = new Service_Reports__c(Name='foo', var1=1);
 try{
  insert testnewcase;
 }catch(DMLException e){
 system.assert(false,'An Error occurred inserting the test record:'+getDMLmessage(0));
 }
 //put some test here to validate that your case was created use some SOQL to get the case that was inserted
 case testcase = [select id,name from case where ServiceReportName = 'foo'LIMIT 1];
 system.assert((testcase.Subject=='Remedial Case'&&testcase.Type=='Remedial Work'),'Trigger fired successfully');

 }
}

 


Message Edited by JimRae on 11-07-2008 10:46 AM
LeebiesLeebies
Hi!  thanks for the code.  My trigger is called NewCase.  Do I need to call the test class the same thing?  How does deply understand that the test class in the class folder relates to the trigger?

Thanks so much :)
JimRaeJimRae
The class doesn't have to have the same name.  When you deploy an object, all testmethods you have get executed to test for coverage.  So you will see results for all of your apex objects.
LeebiesLeebies
Hi!  Firstly thanks SO much for taking the time to help me with this!!!

With a tiny bit of change, that covered 100% code which is awesome!  Problem on deploy now is I get this error:

Run Failures:
  testServiceReportTrg.validateServiceReportTrg System.QueryException: List has no rows for assignment to SObject

I have read up on this, but being a total noob I still have no idea what it means!  Here is my test class to date:

Code:
@isTest
private class testServiceReportTrg{
 static testMethod void validateServiceReportTrg() {

 //create a test object to fire the trigger include all of the required values needed to create the record
 Service_Reports__c testnewcase = new Service_Reports__c(Name='Test123');
 try{
  insert testnewcase;
 }catch(DMLException e){
 system.assert(false,'An Error occurred inserting the test record:');
 }
 //put some test here to validate that your case was created use some SOQL to get the case that was inserted
 case testcase = [select id,subject from case where Service_Report__r.Name = 'Test123' LIMIT 1];
 system.assert((testcase.Subject=='Remedial Case'&&testcase.Type=='Remedial Work'),'Trigger fired successfully');

 }
}

 





JimRaeJimRae
It looks like your SOQL query is returning no data, does your create an new Service object actually work? I don't know if maybe you have more required fields that would need to be included in addition to the name.  Are you seeing the asserting from the insert record section that 'An Error occurred inserting the test record'?
LeebiesLeebies
Hi!

Yes you are right, there are a couple of required fields I did not consider!  Also, how in my original trigger code @ the top of this post, how would I make it so that the case is related to the Service Report that triggered it in the first place?

THANKS!! :smileyvery-happy:
JonPJonP
A couple of comments...

1. On your insert statement, you don't necessarily need the try/catch/assert.  If your insert throws an uncaught DMLException, your test will fail anyway.  The question is just whether you want to catch the exception and continue executing, or just let the test method fail and stop at that point.

2. Apart from the underlying trouble with your insert statement (and required fields), the lvalue in your query line is what's causing the "List has no rows for assignment to SObject" error.  If you assigned your query results to an object of type Case[] or List<Case>, rather than simply Case, it wouldn't throw an exception if 0 results were returned.  This is mostly academic since the insert line has to work correctly for your test to pass, but it's useful to understand what these errors mean.

3. To relate the case to your Service_Reports__c object, you need to put the Service_Reports__c.Id field into the lookup field on Case.  From looking at your code, I'm guessing you have a field called Case.Service_Reports__c, so your trigger code would look like:

Code:
for (Service_Reports__c newCase: Trigger.New) {
   if (newCase.Name != null) {
      cases.add(
new Case(Service_Report__c = newCase.Id, Subject = 'Remedial Case', Type = 'Remedial Work')
); } }