+ Start a Discussion
SF NinjaSF Ninja 

First Apex Trigger(Nearly there!)

dFirst time poster.

 

So I created my first Apex Trigger, which is relatively simple and creates a case record whenever an opportunity is set to 'closed(won)'. It carries in some custom information over to the fields (code is below). The trigger works fine in my sandbox, but when moving it to production I ran into the test coverage needed by force.com. I should say that I used to program many years ago and this is my reintroduction to programing, so I'm a bit rusty. I have downloaded eclipse and the IDE tools (although I'm not certain I need them) and have been researching test methods. I created this so far, but was looking to the community for some assistance. I have always learned best off example code (vs reading large data sheets).

 

 

My Trigger:

 

}

trigger NewDeploymentCaseFromOpp on Opportunity (after update) {

for (Opportunity opp : Trigger.new

 { 

    if ( (opp.stagename == 'Closed Won' ) && (opp.booking_type__c != 'Renewal') && (trigger.oldMap.get(opp.id).stagename != 'Closed Won' ) )

         {

         list<Account> act=[select id,name from Account where Id=:opp.AccountId];

         list<contact> ct=[select id from Contact where AccountId=:act[0].id];

                       Case c = new Case

                           AccountID = opp.AccountId, 

                           Status = 'New'

                           Origin = 'Email',

                           ContactID = ct[0].id,

                           Products__c = opp.Products__c,

                           Subject = 'Deployment Case for '+act[0].name, 

                           OwnerId= '00GS0000000uYbm'

                           RecordTypeId = '012S00000004UAS'

                           Account_Executive__c = opp.OwnerId

                           );   

                           

                       insert c; 

                       break;

                    }

           

    

 } 

 

 

My test method in Eclipse, which is a Class 

 

}

privateclass Test {

statictestMethodvoid myTest(){

Account account = new Account (name='some name account');

insert account; 

Opportunity opportunity = new Opportunity (name='some name', Account=account);

insert opportunity;

 

}

 

A few things:

A) when I try to run my test method i get the error "unexpected token: testMethod. From what I can tell, I'm using this method correctly.

B) when copying out of eclipse, i notice that preceeding all my code I have a } bracket, which does not appear in the editor. Is this normal?  

 

SF NinjaSF Ninja

So I updated my test method in salesforce (not in eclipse) and it runs fine there (although only gives me 5% coverage?). 


So I'm a bit confused by eclipse/ide seems to hate it.

 

Updated code from Salesforce

 

public class Test {    

static testMethod void myTest() {       

Account account = new Account (name='some name account');       

insert account;        

DateTime dT = System.now();       

Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());       

Opportunity opportunity = new Opportunity (name='some name',CloseDate=myDate, booking_type__c='New', stagename='Introductions', Account=account);       

insert opportunity;               

}        

}

 

I'm confused on my next steps from here. I need to get to 75% coverage and sketchy on my best plan to achieve that. 

acrozieracrozier
private static testMethod void myTest()
{

   Account account = new Account (name='some name account');

   insert account; 

   Opportunity opportunity = new Opportunity (name='some name',Account=account);

   insert opportunity;
}

 

try this

 

 

SF NinjaSF Ninja

Receiving the same error in Eclipse.

 

Question: Do I have to use eclipse to promote this trigger with its test coverage to my production enviroment. If I dont have to I can skip it for now (in which case I only need some help in making a good test for my apex code that would cover the 75% needed). 

 

If I do happen to have to use Eclipse, then this is troublesome. 

SF NinjaSF Ninja

        

My original trigger has received a minor update; primarily I'm mapping the ID of the opportunity to the case record in a custom lookup field.

(code at the bottom)

 

Also below is my first attempt at my test method. I'm attempting to do the following with it

A) make a new account/contact/opp. 

B)  My trigger applies when a opportunity is updated to closed won, i default my opportunity to another stage at creation.

C) I'm setting the opportunity stage name to closed won, starting my test and then updating it. I have a feeling I'm not doing this correctly though?

D)  I'm trying to search for the case that SHOULD have been created. The best method I know of is LIST, so I'm using LIST to look for a case where the Opportunity__c field (which is the before mentioned custom case lookup field) equals the id of the opportunity field.

 

After that i do a bunch of asserts to compare values.

Issues: In my LIST command, I'm receiving the following error: Error: Compile Error: unexpected token: 'opportunity.Id' at line 13 column 92


trigger NewDeploymentCaseFromOpp on Opportunity (after update)

{

 for (Opportunity opp : Trigger.new)

 {

    if ( (opp.stagename == 'Closed Won' ) && (opp.booking_type__c != 'Renewal') && (trigger.oldMap.get(opp.id).stagename != 'Closed Won' ) )

         {

         list<Account> act=[select id,name from Account where Id=:opp.AccountId];

         list<contact> ct=[select id from Contact where AccountId=:act[0].id];

                       Case c = new Case (

                           AccountID = opp.AccountId,

                           Opportunity__c = opp.id,

                           Status = 'New',

                           Origin = 'Email',

                           ContactID = ct[0].id,

                           Products__c = opp.Products__c,

                           Subject = 'Deployment Case for '+act[0].name,

                           OwnerId= '00GS0000000uYbm',

                           RecordTypeId = '012S00000004UAS',

                           Account_Executive__c = opp.OwnerId

                           );   // This is my id for testing purposes

                          

                       insert c;

                       break;

                    }

          

   

 }

}

 

 

}

@isTest

publicclass Test {

    statictestMethodvoid myTest() {

        Account account = new Account (name='some name account');

        insert account; 

        Contact contact = new Contact (name='some name contact', email='rawr@rawr,com'); 

        insert contact; 

        DateTime dT = System.now();

        Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());

        Opportunity opportunity = new Opportunity (name='some name',CloseDate=myDate, booking_type__c='New', stagename='Introductions'Account=account);

        insert opportunity;

        opportunity.stagename='Closed Won';   //is this correct? will this update kick off my above trigger?

        Test.startTest();    //is this needed?

        update opportunity;  

        test.stopTest();  //is this needed?

        List<Casecase = [SELECT id, name, Opportunity__c FROM Case WHERE Opportunity__c = opportunity]; //receiving errors here

        System.assertEquals(case.Opportunity__c, opportunity.id);

        System.assertEquals(case.AccountId, opportunity.AccountId);

        System.assertEquals(case.Status, 'New'); 

        System.assertEquals(case.Origin, 'Email');

        System.assertEquals(case.Products__c, opportunity.Products__c);

        System.assertEquals(case.RecordTypeId, '012S00000004UAS');

        }

SF NinjaSF Ninja

Update: after a bit of playing around, I think I'm going down the complete wrong path.

 

I could really use some guidance here if anyone has some free time today. My trigger works perfectly fine, but I'm having a tough time with these test methods and really was hoping to get this up on production today:( 

Rahul SharmaRahul Sharma

Hello,

After observing your trigger i noticed that query and dml statements are made inside a for loop which is a bad practice and will cause exception when you try to insert bulk opportunity records using dataloader.

good that its working for you. 

one more thing , you just have to insert account, contact and opportunity with the mandatory conditions, case will be inserted automatically.

removed unnecessary part from your code:

@isTest
publicclass Test {
    statictestMethodvoid myTest() {
        Account objAccount = new Account (name='some name account');
        insert objAccount; 
        Contact objContact = new Contact (Lastname='some name contact', email='test@gmail.com'); 
        insert objContact; 
        DateTime dT = System.now();
        Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());
        Opportunity objOpportunity = new Opportunity (name='some name',CloseDate=myDate, booking_type__c='New', stagename='Introductions', AccountId=objAccount.Id);
        insert objOpportunity;
        objOpportunity.stagename='Closed Won';   //is this correct? will this update kick off my above trigger?, Yeah
        Test.startTest();    //Its needed
        update objOpportunity;  
        test.stopTest();  // Its needed
        }

 let me me know if you face any issues.