You need to sign in to do that
Don't have an account?
Leebies
Deploy Trigger
I wrote the following trigger (based on the one from the Cook Book)
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!
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; }
Would appreciate any help!
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):
- 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.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);
Hopefully this will work for you.
Mike
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? :):):)
Message Edited by JimRae on 11-07-2008 10:46 AM
Thanks so much :)
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:
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:
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: