+ Start a Discussion
Eager-2-LearnEager-2-Learn 

Test Script - Inserting records?

Hello everyone,

 

Everything seems to be so scatter when it comes to getting a simple solution to an when it comes to Apex and SF in general.  Am I the only one feeling this pain?

 

Could someone give me push start on a test script that will insert 201 records into the Opportunity object?  There a many required fields, some of which are drop downs and some are pick lists, not sure if that matters.

Best Answer chosen by Admin (Salesforce Developers) 
Nick1746323Nick1746323

I'm thinking the reason for that is the "all or none" behaviour of  the standard apex "insert" command - meaning it won't insert anything if any exceptions arise from any of the inserts. This is described in the docs under "Bulk DML Exception Handling" section.

 

You could test using two separate inserts or look at Database.insert, which has an option to allow for partial inserts.

Message Edited by Nick1746323 on 09-11-2009 10:10 AM

All Answers

lvivaninlvivanin

@isTest
private class OpportunityTests
{
static testMethod void testOpportunityInserts()
{


Opportunity[] opps = new Opportunity[0];

for(integer i=0;i<202;i++)
{

opps.add(new Opportunity(

Name ='2009-09-08_'+ 'opp:'+i,
StageName='Prospecting',
CloseDate=System.Today(),
ForecastCategoryName='Pipeline'
));
}

insert opps;


}


}

 

Eager-2-LearnEager-2-Learn

Thanks for the jump start; however, I am having an issue with a date fields!  When I try to set the ClosedDate = 2009-09-03T23:58:24.000Z, I get the an error message stating that it is an unexpected token and a red X next to the line in question.  If I put quotes (') around it I get the same error message; however, I do not get the red X, I get a yellow triangle at on the first line.

 

Any ideas what I am doing wrong?

lvivaninlvivanin

@isTest
private class OpportunityTests
{
static testMethod void testOpportunityInserts()
{
Date ClosedDate = date.newinstance(2009, 1, 1);

Opportunity[] opps = new Opportunity[0];

for(integer i=0;i<202;i++)
{

opps.add(new Opportunity(

Name ='2009-09-08_'+ 'opp:'+i,
StageName='Prospecting',
CloseDate=ClosedDate,
ForecastCategoryName='Pipeline'
));
}

insert opps;


}


}

 

Eager-2-LearnEager-2-Learn

Thanks for the date help.  I have tested my first loop test and it passed as expected.  I did have to change the for loop from 202 to 201 and the assert stated that 201 records were created!  I would have thought the for loop would have need 202 because of the i<202 statement.  No big deal at this point.

 

My issue now is when I am testing for duplicates (see dup method test below).  My trigger does work for testing duplicates when I manually enter a duplicate from the Opportunity screen; therefore per the code below I was expecting at least the first record to be created.  I was expecting the assert to to be true but instead I get the message that zero record where created!

 

I am guessing that it has something to do with the exception routine getting hit over and over during the dup catch in my trigger.  To give you an understanding of my trigger--if a dup is entered I have an error message show on the screen under the Opp name field.

 

When I change the for loop to i<1, one record is created but if I set it to even to the assert states that zero record where created!  I think it has to do with the exception routine fire and doing something but I am not certain.  I appreciate your support.

static testMethod void InsertDupTest() { Opportunity[] opps = new Opportunity[0]; Date closedDate = date.newinstance(2009, 1, 1); Date effectiveDate = date.newinstance(2009, 1, 1); for(integer i=0;i<201;i++) { opps.add(new Opportunity(Name ='United Oil & Gas, Singapore' + effectiveDate, ACCOUNTID = '001A0000001uqNHIAY', STAGENAME = 'Sold', PROBABILITY = 100, TYPE = 'Dental Only', AVP__C = 'Carl Repetti', ACCOUNT_IN_JEOPARDY__C = 'No', CLOSEDATE = closedDate, EFFECTIVE_DATE__C = effectiveDate, BUSINESS__C = '51-199 Renewal Sales', COMPETITOR__C = 'UNKNOWN', ENROLLMENT__C = 'DBE/Admin', EST_ACTUAL_CONTRACTS__C = 0, FULLSERVICEPRODUCER__C = 'Kelly & Associates Insurance Group', GENERALPRODUCER__C = 'BenefitMall', LEAD_REP_NAME__C = 'June Swears', MARKET_SEGMENT__C = 'Jan-50', POTENTIAL_CONTRACTS__C = 5, PRODUCTS_OFFERED__C = 'Dental', RADAR__C = FALSE, RISK_NON_RISK__C = 'Risk', SERVICE_REP__C = 'Lisa Quigg', SYSTEM__C = 'NASCO', TRANSITIONED__C = FALSE, UNDERWRITING_ENTITY__C = 'DC', DUP_NAME_CHECKER__C = TRUE )); } Test.startTest(); try { insert opps; }catch(DMLException e){ } Test.stopTest(); Opportunity[] opportunities = new Opportunity[0]; opportunities = [select id,name from opportunity where id in :opps]; // 1 opportunities should have been created. System.assertEquals(1,opportunities.size()); }

 

Nick1746323Nick1746323

I'm thinking the reason for that is the "all or none" behaviour of  the standard apex "insert" command - meaning it won't insert anything if any exceptions arise from any of the inserts. This is described in the docs under "Bulk DML Exception Handling" section.

 

You could test using two separate inserts or look at Database.insert, which has an option to allow for partial inserts.

Message Edited by Nick1746323 on 09-11-2009 10:10 AM
This was selected as the best answer