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
streetstreet 

Testcase error

trigger Assert_Creation on OpportunityLineItem (after insert)
 {
 for(OpportunityLineItem oli:trigger.new)
 {
 opportunity o=[select id,Accountid,Name,Manager__r.name,Sales_Consultan__r.name,Sales_Consultan__c,
                       Process_Consultant_User__r.name,Process_Consultant_Manager__r.name,
                       Branch__c,Payment_Option__c,Sales_Date__c,Process_Stage__c from opportunity where id=:oli.opportunityid];

 asset a=new asset();
 a.name=o.name;

 a.Accountid=o.Accountid;
 a.Sales_Consultant1__c=o.Sales_Consultan__c;
 a.Sales_Consultant_Manager1__c=o.Manager__c;
 a.Process_Consultant1__c=o.Process_Consultant_User__c;
 a.Process_Consultant_Manager1__c=o.Process_Consultant_Manager__c;
 a.Branch__c=o.Branch__c;
 a.Payment_Option__c=o.Payment_Option__c;
 a.Expected_Sale_Date__c=o.Sales_Date__c;
 a.Process_Stage__c=o.Process_Stage__c;
 a.opportunity__c=o.id;

 insert a; 
 }
 }


I have written Like this 

@isTest
private class Assert_Creation_TC {
static testMethod void validateabc() {
Branch__c b= new Branch__c();
b.Name='sdf';
b.Branch_Code__c='xc';
b.City__c='df';
b.City__c='ww';
b.Pin_Code__c='wewe';
b.Invoice_Number_Cash__c=23;
b.Invoice_Number_General__c=23;
b.Invoice_Number_Online__c=23;
insert b;


account a= new account();
a.name='dsad';
insert a;
user u = [select id from user limit 1];

 


//Pricebook2 standardPB = [select id from Pricebook2 limit 1];


Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true);
insert pb;
Product2 prod = new Product2(Name = 'Anti-infectives 2007', Family = 'Best Practices', IsActive = true);

PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb.Id,Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
insert pbe;

 

opportunity o = new opportunity();
o.Name='ssd';
o.CloseDate=date.ValueOf('2011-09-21');
o.Branch__c=b.id;
o.Manager__c=u.id;
o.Payment_Option__c='Full Payment';
o.StageName='Follow up for Evaluation';
o.Accountid=a.id;

insert o;
update o;

opportunitylineitem op=new opportunitylineitem();
op.Quantity=1;
op.UnitPrice=76;
op.OpportunityId=o.id;
op.PricebookEntryId = pbe.id;
insert op;


asset ass=new asset();
ass.name=o.name;

}
}

 

Here is the error:

Method NameTotal Time (ms)MessageStack Trace

Assert_Creation_TC.validateabc461.0System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Product2Id]: [Product2Id]Class.Assert_Creation_TC.validateabc: line 32, column 1
Rajesh SriramuluRajesh Sriramulu

Hi

 

try to insert the required fields in it i.e

in account object lastname is mandatory.

 

Let me know any issue and if it solves ur problem plz accept it solution.

streetstreet

added but same error!!

MandyKoolMandyKool

Hi,

 

Instead of creating the "Standard Price Book" you can query the standard price book (as each org has StandardPriceBook) and use the same for your test class.