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
LloydSLloydS 

Help Deploying First Apex Trigger

I've written a simple Apex Trigger to change the Opportunity Type based upon the Opportunity Record Type. Works fine in the Sandbox so now it's time to deploy and I could use some help.

 

I created an outbound change set in the Sandbox and uploaded it into my Production site where it was deployed. However, it's stuck "In Progress".

 

So I created an Eclipse project and added an Apex Trigger there. But it fails in testing when i try to deploy it to the server. I've got no clue how to write a proper test for this and so far what I've found here and via Google is a bit confusing.

 

Here's the trigger code:

 

 

trigger recordtype on Opportunity (before update) { Opportunity myOpportunity = trigger.new[0]; if (myOpportunity.RecordTypeID == '012A0000000L9p0' && myOpportunity.Type <> 'New Agent or Agency') { myOpportunity.Type = 'New Agent or Agency'; } else if (myOpportunity.RecordTypeID == '012A0000000L9oW' && myOpportunity.Type <> 'Life Insurance') { myOpportunity.Type = 'Life Insurance'; } else if (myOpportunity.RecordTypeID == '012A0000000L9ol' && myOpportunity.Type <> 'Critical Illness') { myOpportunity.Type = 'Critical Illness'; } else if (myOpportunity.RecordTypeID == '012A0000000L9ov' && myOpportunity.Type <> 'Deferred Annuity') { myOpportunity.Type = 'Deferred Annuity'; } else if (myOpportunity.RecordTypeID == '012A0000000L9og' && myOpportunity.Type <> 'Disability') { myOpportunity.Type = 'Disability'; } else if (myOpportunity.RecordTypeID == '012A0000000L9ob' && myOpportunity.Type <> 'Long Term Care') { myOpportunity.Type = 'Long Term Care'; } else if (myOpportunity.RecordTypeID == '012A0000000LAzh' && myOpportunity.Type <> 'Managed Concept') { myOpportunity.Type = 'Managed Concept'; } else if (myOpportunity.RecordTypeID == '012A0000000L9oq' && myOpportunity.Type <> 'SPIA') { myOpportunity.Type = 'SPIA'; } }

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
LloydSLloydS
I answered my own question. Yes, the above is what I needed to do. Everything worked perfectly. Thanks.

All Answers

JGreoryJGreory

You have to write an Apex class starting with :

 

@isTest

private class cls_Test_Triggers_Actions {

 

    static testMethod void myUnitTest() {

 

Test.startTest();

 

 

Test.StopTest();

 

}

}

 

Between Start and Stop, you have to cover 1% Code, and if you trigger is the only one, 75%.

 

You should need to create new opportunity in your code, testing all possibilities :

 

Opportunity o = new Opportunity();

o.name='';

o.RecordTypeId='012A0000000L9p0';

o. Type = 'Deferred Annuity';

insert o;

 

you should then ensure results are ok :

 

Opportunity o_new = [select id from opportunity where id=:o.id];

 

system.assertequals(o_new.Type='New agent or agency', o_new.Type);

 

You develop your class in Eclipse and then Deploy your class to your production.

Then, you will be able to deploy your trigger. 

LloydSLloydS

Thanks. This was exactly what I needed.

 

Is everything between Opportunity o = new . . . and system.assertequals . . . within the test? I created several new opportunities testing each record ID. I changed o.???? to other characters (like bo and co as I couldn't have a duplicate).

 

The class uploaded just fine but the trigger doesn't pass testing. I'm assuming it's because my trigger is before update and no where in the class is the record being updated. Right?

 

If that's the case, do I insert something like this after "insert o;"

 

     o.Type = 'SPIA';

     update o;

 

So the opportunity is created, then the type is changed and the record is updated which should fire the trigger for testing.

 

Message Edited by LloydS on 02-16-2010 02:12 AM
LloydSLloydS
I answered my own question. Yes, the above is what I needed to do. Everything worked perfectly. Thanks.
This was selected as the best answer