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
Colin LoretzColin Loretz 

Unable to deploy Apex Triggers

Hi all,

I've been working with Apex triggers in a development environment but I'm unable to deploy them to a production environment. I have been able to deploy Apex classes with their respective testMethods but am unable to deploy the Triggers.

I created a trigger on an Opportunity and created a testMethod in an Apex class, however it does not appear as though the test is working.

I've even tried to deploy the LeadDupeTrigger from the Apex Cookbook - and the tests don't appear to be running. (Again the tests with just Apex Classes work fine).

The Code Coverage Results say:
myTriggerTester (Class) - 0 Lines not tested, 100% covered

However, the debug log (set to max verbosity) displays the following:
Code:
Debug Log:

Cumulative profiling information:

No profiling information for SOQL operations.

No profiling information for SOSL operations.

No profiling information for DML operations.

No profiling information for method invocations.

 
I created a simple trigger to test deployment, it is below:
Code:
trigger populateDates on Opportunity (before insert, before update) {

 Opportunity [] opp = Trigger.new;
 
 for (Integer i = 0; i < opp.size(); i++)
 {
  opp[i].Arrival_Date__c = date.today();
 }

}

 And the testMethod:

Code:
public class myTriggerTester {
 
 static testMethod void TEST_populateDates() {
 
   Account testAccount = new Account (
               Name = 'Testing'
       );
       insert testAccount;

       Opportunity firstOpp = new Opportunity (
               Name = 'Test',
               Account = testAccount,
               CloseDate = date.today(),
               StageName = 'Strong Potential',
               Proposal_Deadline__c = date.today()
       );
  
  System.assertEquals(null, firstOpp.Arrival_Date__c);
  
  insert firstOpp;
  
  System.assertEquals(date.today(), firstOpp.Arrival_Date__c);
 }
}

 


jrotensteinjrotenstein
Colin,

What development environment are you using? If you're using Eclipse, then you start the test by right-clicking the Test class and choosing Force.com -> Run Tests.

You can also choose this command by right-clicking the "classes" folder, which will run ALL tests.

In your tests, you'll probably need to retreive a new copy of the object you created before you can test the field values. Something like:

Code:
public class myTriggerTester {
 
 static testMethod void TEST_populateDates() {
 
   Account testAccount = new Account (Name = 'Testing');
 insert testAccount;

Opportunity firstOpp = new Opportunity (
Name = 'Test',
Account = testAccount,
CloseDate = date.today(),
StageName = 'Strong Potential',
Proposal_Deadline__c = date.today()
);

System.assertEquals(null, firstOpp.Arrival_Date__c);

insert firstOpp;

newOpp = [select Id, Arrival_Date__c from Opportunity where Id = :firstOpp.Id];

System.assertEquals(date.today(), newOpp.Arrival_Date__c);
}
}

 
Also, you can simplify some of your Trigger code by using a Java-style iterator:

Code:
trigger populateDates on Opportunity (before insert, before update) {

 for (Opportunity o : Trigger.new) {
  o.Arrival_Date__c = date.today();
 }
}

 See how all that goes!

Colin LoretzColin Loretz
Thank you for your help. At first I commented out the assertEquals statements to get it load to the production environment. I've done what you recommended and it works out now.