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
chriseustacechriseustace 

Apex Trigger Complete, need help deploying - testing?

I have written my first apex trigger, tested it, and it works in my sandbox.  However when i try to push to production it is saying I need to run unit tests up to 75%.  I am unsure on how to do this and scouring the boards has got me nowhere.  Can anyone assist?  My code is as follows:

 

trigger createSupplierMemberLink on Opportunity (after insert) {
   
    Opportunity oppty = trigger.new[0];
    if(oppty.Payroll_Type__c <> ''){
   
    SupplierMemberLink__c sml = new SupplierMemberLink__c();
    sml.RelatedProgram__c = oppty.Id;
    sml.SalesDate__c = oppty.CloseDate;
    sml.SalesAmount__c = oppty.Amount;
    sml.Name = oppty.Name + ' Payroll';
   
    insert sml;
}
}

MSVRadMSVRad

Here is a link to some information about Testing Apex Code that may help you out.

 

You pretty much just need to write out unit tests to replicate what you are testing in the sandbox UI. So create dummy Opportunities and Dummy SupplierMemberLink__c records and use assert methods to see if the values in the fields you are populating in your trigger are correct.

 

Here is an article about how to write good unit tests.

 

Hope this helps.

Rick_NookIndRick_NookInd

In Eclipse, click on File, New, Apex Class, type in a name and select Template as "Test Class".

 

If you aren't using Eclipse yet, I'm not sure how you can create an Apex Class through the standard UI to put the test code, but you can't have a trigger without test code.

 

You should then be able to write Apex code that "tests" your trigger by doing an insert. E.g. (untested):

@isTest
private class Trigger_Tests {

static testMethod void myUnitTest() {

Account act = [SELECT Id from Account LIMIT 1];

Opportunity opp = new Opportunity();

opp.Name = 'Test';

opp.AccountId = act.Id;

opp.CloseDate = date.newInstance(2009,1,1);

opp.Payroll_Type__c = 'BLAA';

}

}

 

Please note however that your trigger does not function correctly when more than one record is inserted at once.

try:

for(Opportunity oppty : trigger.new) {

instead of

Opportunity oppty = trigger.new[0];

 

Good luck!