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
JameslongJameslong 

NEED HELP IN A SIMPLE TEST CLASS....PLEASE

Hi buddies,

 

I have started writing the test class but I am facing error when trying to insert the opportunity line items as below:

 

Please help me out on how to inset opportunity lineitems for the opportunities

 

Compile Error: Invalid field Name for SObject OpportunityLineItem at line 16 column 57


@istest
public class testAnOVIIDClass
{
static TestMethod void testAnOVIIDClass(){
//data preparation
list<opportunitylineitem> olilist=new list<opportunitylineitem>();
list<opportunity> opplist=new list<opportunity>();
list<id> pricebookidlist= new list<ID>();
list<String> productlist=new list<string>();

for(integer i=0;i<2;i++){
opportunity opy = new opportunity(Name = 'test opp' + i);
opplist.add(opy);
}
for(integer i=0;i<2;i++){
opportunitylineitem aol= new opportunitylineitem(Name = 'test opp line item' + i);
olilist.add(aol);
}
}
}

 

Thanks

JL

Best Answer chosen by Admin (Salesforce Developers) 
Alex.AcostaAlex.Acosta

First you'll need to insert your Opportunity so you can have an Id to form your relationship. Then you'll make your OpporutnityLineItem as you created your Opportunities and tie them together by putting OpportunityLineItem.OpportunityId = <insertedOpportunity>.Id

 

ie:

Account a = new Account();

a.Name = 'test';

insert a;

 

Opportunity o = new Opportunity()

o.Name = 'test';

o.AccountId = a.Id;

insert o;

 

OpportunityLineItem oli = new OpportunityLineItem();

oli.OpportunityId = o.Id;

insert oli;

All Answers

Alex.AcostaAlex.Acosta

First you'll need to insert your Opportunity so you can have an Id to form your relationship. Then you'll make your OpporutnityLineItem as you created your Opportunities and tie them together by putting OpportunityLineItem.OpportunityId = <insertedOpportunity>.Id

 

ie:

Account a = new Account();

a.Name = 'test';

insert a;

 

Opportunity o = new Opportunity()

o.Name = 'test';

o.AccountId = a.Id;

insert o;

 

OpportunityLineItem oli = new OpportunityLineItem();

oli.OpportunityId = o.Id;

insert oli;

This was selected as the best answer
LakshmanLakshman

Alex is correct but you also need to enter pricebookentry id in opplineitem before inserting. You can query for existing Pricebook Id or insert a fresh one(recommended).

 

Regards,

Lakshman

JameslongJameslong

Hi Alex , Hi Lakshman,

 

Thank you for the solution. It worked. But still having a problem with inserting "pricebook2id".

 

Please look at my code and suggest what should be the correct statement in place marked in red.

Also please add any thing else if its required in the code fo rthis to succeed....

 

CODE:

 

@istest
public class testAnOVIIDClass
{
static TestMethod void testAnOVIIDClass(){

//data preparation
list<opportunity> opplist=new list<opportunity>();
list<opportunitylineitem> olilist=new list<opportunitylineitem>();
list<id> pricebookidlist= new list<ID>();
list<pricebookentry> pbelist = new list<pricebookentry>();
list<product2> prolist = new list<product2>();
list<ovi_value__c> ovclist = new list<ovi_value__c>();

 

//insert opportunity test records
for(integer i=0;i<2;i++){
opportunity opy = new opportunity(Name = 'test opp' + i,stagename = 'closed won',closedate=date.parse('1/1/2020'));
opplist.add(opy);
}
insert opplist;
//Assert the inserted test records
list<opportunity> insertedopplist = new list<opportunity>([SELECT Name,Id,stagename FROM opportunity WHERE Id IN: opplist]);
for(opportunity opy : insertedopplist){
System.assertEquals(insertedopplist.size(),2);
}

 

//insert product test records
for(integer m=0;m<2;m++){
product2 prd = new product2();
prd.name = 'test product' + m;
prolist.add(prd);
}
insert prolist;
//Assert the inserted test records
list<product2> insertedprolist = new list<product2>([SELECT Name,Id FROM product2 WHERE Id IN: prolist]);
for(product2 prd : insertedprolist){
System.assertEquals(insertedprolist.size(),2);
}

 

//insert pricebookentry test records
for(integer l=0;l<2;l++){
pricebookentry pbo = new pricebookentry();
pbo.unitprice = 500 + l;
pbo.product2id = prolist[l].id;
pbo.pricebook2id =                    //since this is a required field if I run the test to check the assertion it fails and points here
pbelist.add(pbo);
}
insert pbelist;
//Assert the inserted test records
list<pricebookentry> insertedpbelist = new list<pricebookentry>([SELECT Name,Id FROM pricebookentry WHERE Id IN: pbelist]);
for(pricebookentry pbo : insertedpbelist){
System.assertEquals(insertedpbelist.size(),2);
}

 

//insert opportunity line item test records
for(integer k=0;k<2;k++){
opportunitylineitem aol= new opportunitylineitem();
aol.opportunityid = opplist[k].id;
aol.local_mrs__c = 100+k;
aol.local_nrs__c = 200+k;
aol.mrc__c = 300+k;
aol.nrc__c = 400+k;
aol.Local_Currency__c = 'test currency' + k;
olilist.add(aol);
}
insert olilist;
//Assert the inserted test records
list<opportunitylineitem> insertedolilist = new list<opportunitylineitem>([SELECT Id,opportunityid,local_mrs__c,local_nrs__c,mrc__c,nrc__c,Local_Currency__c
FROM opportunitylineitem WHERE Id IN: olilist]);
for(opportunitylineitem aol : insertedolilist){
System.assertEquals(insertedolilist.size(),2);
}

 

//insert ovi product test records
for(integer j=0;j<2;j++){
ovi_value__c ovv = new ovi_value__c(Name = 'test ovi value' + j, Product__c = 'testproduct'+j);
ovclist.add(ovv);
}
insert ovclist;
//Assert the inserted test records
list<ovi_value__c> insertedovclist = new list<ovi_value__c>([SELECT Name,ID FROM ovi_value__c WHERE Id IN: ovclist]);
for(ovi_value__c ovv : insertedovclist){
System.assertEquals(insertedovclist.size(),2);
}

 

//test.startTest();

//test.stopTest();

//create an instance
AnOVIIDClass anovi = new AnOVIIDClass();


}
}

Alex.AcostaAlex.Acosta

You're gonna have to create Pricebook2 records before creating pricebook entries. If you're using Eclipse with the Force.com IDE Plugin, you can look at the schema and see what the relationships are tied to by explanding the fields section. from here you can expand each field and see what type they are. If they are look ups you can expand reference to and see what sObject they are looking at. Or you can just google for the "Salesforce _<sobject Name>__"

 

example this is what i got when googling Salesforce Pricebook2 on the first result.

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_pricebook2.htm

JameslongJameslong

Hi Alex,

 

It worked good...although some minor changes were needed from my environment point of view.

 

Thanks a ton Alex.

 

JL