You need to sign in to do that
Don't have an account?
SSeb
Please help with the code coverage for the trigger
trigger Product_Schedule on OpportunityLineItem (after update,after insert)
{
for ( OpportunityLineItem o : Trigger.new)
{
Opportunity opps;
// id OppLineItemID = o.Opp_Line_Item_Id__c ;
id OppLineItemID = o.ID ;
system.debug('Opurtunity line item triggered ' + o.Opp_Line_Item_Id__c + 'Opportunity ID ' + o.ID);
opps = [Select o.First_Ship_Date__c ,o.Expected_Return_Ship_Date__c, o.Min_K_QPID__c,
(Select Quantity, productcode From OpportunityLineItems where ProductCode = 'K-QPID') From Opportunity o
Where id IN (SELECT OpportunityId FROM OpportunityLineItem WHERE Quantity > 0 and id =:o.id) ];
OpportunityLineItem prod;
prod = [Select Quantity, productcode, Nav_Inventory__c From OpportunityLineItem where Opp_Line_Item_Id__c =:o.Opp_Line_Item_Id__c];
Datascan_Product_Availability__c Avail = [select K_QPID__c , Count_Date__c From Datascan_Product_Availability__c c
where (c.Count_Date__c > :opps.First_Ship_Date__c) and (c.Count_Date__c < :opps.Expected_Return_Ship_Date__c)
order by Count_Date__c asc limit 1 ];
opps.Min_K_QPID__c = Avail.K_QPID__c ;
opps.Min_K_QPID_Date__c = Avail.Count_Date__c;
update opps;
//select MIN(K_DS_WIFI_1__c) From Datascan_Product_Availability__c where count_date__C > 2019-10-18 and count_date__c < 2019-11-22
system.debug('opps ' + opps.First_Ship_Date__c + opps.Expected_Return_Ship_Date__c );
system.debug('prod ' + prod.quantity + prod.productcode );
system.debug('Avail ' + Avail.K_QPID__c );
}
}
{
for ( OpportunityLineItem o : Trigger.new)
{
Opportunity opps;
// id OppLineItemID = o.Opp_Line_Item_Id__c ;
id OppLineItemID = o.ID ;
system.debug('Opurtunity line item triggered ' + o.Opp_Line_Item_Id__c + 'Opportunity ID ' + o.ID);
opps = [Select o.First_Ship_Date__c ,o.Expected_Return_Ship_Date__c, o.Min_K_QPID__c,
(Select Quantity, productcode From OpportunityLineItems where ProductCode = 'K-QPID') From Opportunity o
Where id IN (SELECT OpportunityId FROM OpportunityLineItem WHERE Quantity > 0 and id =:o.id) ];
OpportunityLineItem prod;
prod = [Select Quantity, productcode, Nav_Inventory__c From OpportunityLineItem where Opp_Line_Item_Id__c =:o.Opp_Line_Item_Id__c];
Datascan_Product_Availability__c Avail = [select K_QPID__c , Count_Date__c From Datascan_Product_Availability__c c
where (c.Count_Date__c > :opps.First_Ship_Date__c) and (c.Count_Date__c < :opps.Expected_Return_Ship_Date__c)
order by Count_Date__c asc limit 1 ];
opps.Min_K_QPID__c = Avail.K_QPID__c ;
opps.Min_K_QPID_Date__c = Avail.Count_Date__c;
update opps;
//select MIN(K_DS_WIFI_1__c) From Datascan_Product_Availability__c where count_date__C > 2019-10-18 and count_date__c < 2019-11-22
system.debug('opps ' + opps.First_Ship_Date__c + opps.Expected_Return_Ship_Date__c );
system.debug('prod ' + prod.quantity + prod.productcode );
system.debug('Avail ' + Avail.K_QPID__c );
}
}
Try this
Here is your test class for trigger-
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com (http://www.ajaydubedi.com )
@isTest
public class Product_Schedule_Helper
{
static testMethod void Scheduletest()
{
Account a= new Account(name='test account');
insert a;
Opportunity opp= new Opportunity(name='test opp',accountId=a.Id, StageName = 'Estimated Details', CloseDate =date.today()+5,SR_AE_Win_Probability__c = '25%',
SR_AE_Forecast_Category__c = 'Pipeline',
First_Ship_Date__c=date.today()-5,Expected_Return_Ship_Date__c=date.today()+5,Min_K_QPID__c=222);
insert opp;
product2 product= new product2(Name = 'K-DART SCANNER',ProductCode = 'K-QPID', isActive = true);
insert product;
Id pricebook_Id=System.Test.getStandardPricebookId();
pricebookEntry price_entry= new pricebookEntry( Pricebook2Id = pricebook_Id, Product2Id = product.Id, UnitPrice = 0.02,IsActive = true);
insert price_entry;
OpportunitylineItem opplineitem = new OpportunitylineItem (OpportunityId = opp.Id,Quantity=110, UnitPrice = 0.02);
insert opplineitem;
Datascan_Product_Availability__c Availtest= new Datascan_Product_Availability__c (K_QPID__c=233 ,Count_Date__c = date.today()+3);
insert Availtest;
}
}
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId, unknown (versions 3.0 and higher must specify pricebook entry id, others must specify product id): [PricebookEntryId, unknown]
Make sure while creating OpportunitLineItem (Opportunity Product), PricebookEntryId is not null.Hope this helps to solve dml exception.
@isTest
public class Product_Schedule_Helper
{
static testMethod void Scheduletest()
{
Account a= new Account(name='test account');
insert a;
Opportunity opp= new Opportunity(name='test opp',accountId=a.Id, StageName = 'Estimated Details', CloseDate =date.today()+5,SR_AE_Win_Probability__c = '25%',
SR_AE_Forecast_Category__c = 'Pipeline',
First_Ship_Date__c=date.today()-5,Expected_Return_Ship_Date__c=date.today()+5,Min_K_QPID__c=222);
insert opp;
product2 product= new product2(Name = 'K-DART SCANNER',ProductCode = 'K-QPID', isActive = true);
insert product;
Id pricebook_Id=System.Test.getStandardPricebookId();
pricebookEntry price_entry= new pricebookEntry( Pricebook2Id = pricebook_Id, Product2Id = product.Id, UnitPrice = 0.02,IsActive = true);
insert price_entry;
Datascan_Product_Availability__c Availtest= new Datascan_Product_Availability__c (K_QPID__c=233 ,Count_Date__c = date.today()+3);
insert Availtest;
OpportunitylineItem opplineitem = new OpportunitylineItem(OpportunityId = opp.Id, Quantity = 10, PricebookEntryId = price_entry.Id,TotalPrice = 1000.00);
insert opplineitem;
}
}