• SF Ninja
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 13
    Replies

Hello,

 

Hopefully this is a quick question for you geniuses. I'm wrtting a test method(first one) and have an interesting error I cant reconcile.

 

Error:

 

System.DmlException: Update failed. First exception on row 0 with id 006S0000005AsJJIA0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, NewDeploymentCaseFromOpp: execution of AfterUpdate caused by: System.ListException: List index out of bounds: 0 Trigger.NewDeploymentCaseFromOpp: line 8, column 1: []

 

My test Code

 

}

 

@isTest
public class Test {
    static testMethod void myTest() {
        Account account = new Account (name='some name account');
        insert account; 
        Contact contact = new Contact (lastname='some', Account=account, email='rawr@rawr.com'); 
        insert contact; 
        DateTime dT = System.now();
        Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());
        Opportunity NewOpportunity = new Opportunity (name='some name',CloseDate=myDate, booking_type__c='New', stagename='Introductions', Account=account);
        insert NewOpportunity;
        
        NewOpportunity.stagename='Closed Won';
        //test.startTest();
        update NewOpportunity;
        //test.stopTest();
        Case newCase= [SELECT Id, Opportunity__c, Status, Origin, Products__c, RecordTypeId FROM Case WHERE Account.Name = 'some name account'];
        System.assertEquals(newCase.Opportunity__c, NewOpportunity.id);
        System.assertEquals(newCase.AccountId, NewOpportunity.AccountId);
        System.assertEquals(newCase.Status, 'New'); 
        System.assertEquals(newCase.Origin, 'Email');
        System.assertEquals(newCase.Products__c, NewOpportunity.Products__c);
        System.assertEquals(newCase.RecordTypeId, '012S00000004UAS');
        }
        

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?  

 

Hello,

 

Hopefully this is a quick question for you geniuses. I'm wrtting a test method(first one) and have an interesting error I cant reconcile.

 

Error:

 

System.DmlException: Update failed. First exception on row 0 with id 006S0000005AsJJIA0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, NewDeploymentCaseFromOpp: execution of AfterUpdate caused by: System.ListException: List index out of bounds: 0 Trigger.NewDeploymentCaseFromOpp: line 8, column 1: []

 

My test Code

 

}

 

@isTest
public class Test {
    static testMethod void myTest() {
        Account account = new Account (name='some name account');
        insert account; 
        Contact contact = new Contact (lastname='some', Account=account, email='rawr@rawr.com'); 
        insert contact; 
        DateTime dT = System.now();
        Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());
        Opportunity NewOpportunity = new Opportunity (name='some name',CloseDate=myDate, booking_type__c='New', stagename='Introductions', Account=account);
        insert NewOpportunity;
        
        NewOpportunity.stagename='Closed Won';
        //test.startTest();
        update NewOpportunity;
        //test.stopTest();
        Case newCase= [SELECT Id, Opportunity__c, Status, Origin, Products__c, RecordTypeId FROM Case WHERE Account.Name = 'some name account'];
        System.assertEquals(newCase.Opportunity__c, NewOpportunity.id);
        System.assertEquals(newCase.AccountId, NewOpportunity.AccountId);
        System.assertEquals(newCase.Status, 'New'); 
        System.assertEquals(newCase.Origin, 'Email');
        System.assertEquals(newCase.Products__c, NewOpportunity.Products__c);
        System.assertEquals(newCase.RecordTypeId, '012S00000004UAS');
        }
        

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?