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
Abhi MalikAbhi Malik 

Hi everyone am trying to write a testclass for my trigger but not getting any coverage please help me

Trigger:
trigger QuantityMOQValidate on OpportunityLineItem (after insert, after update) {
if(checkRecursive.runOnce()){
    Set<Id> pbeIds = new Set<Id>();
    for (OpportunityLineItem oli : Trigger.new){
        pbeIds.add(oli.pricebookentryid);
        system.debug('adding to pbeIds ------>' + oli.pricebookentry.id);
    }
    Map<Id, double> entries = new Map<Id, Decimal>();
    for(PricebookEntry pbe : [select id, MOQ__c from PricebookEntry where id IN : pbeIds]){
        entries.put(pbe.id, pbe.MOQ__c);
        system.debug('putting in a map id------>' + pbe.id);
        system.debug('putting in a map value------>' + pbe.MOQ__c);
    }
    list<OpportunityLineItem> upOLI = new list<OpportunityLineItem>();
    for(OpportunityLineItem oli: Trigger.new){
        if((oli.Quantity < entries.get(oli.pricebookEntryId)) && entries.get(oli.pricebookEntryId) != Null ){
            system.debug('Quantity less than MOQ');            
            oli.addError('Quantity less than MOQ ['+ Integer.valueof(entries.get(oli.pricebookEntryId))+']');
        }        
        system.debug('>>>>>>>>Quantity----->' + oli.Quantity+'>>MOQ val----->'+ entries.get(oli.pricebookEntryId));        
    }   
}
}

Testclass:

@istest
public class TestQuantityMOQValidate {
 static testMethod void myUnitTest() {       

//Data Prep
//Create Account, Opportunity, Product, etc.
        Account acct1 = new Account(name='test Account One1');
        insert acct1;
//Create Opportunity on Account
        Opportunity Oppty1 = new Opportunity(name='test Oppty One1');
        Oppty1.StageName = 'Selected';
        Oppty1.CloseDate = Date.today() + 30;
        insert Oppty1;         

// Create Products 
         Product2 testprod1 = new Product2 (name='test product one1');
         testprod1.productcode = 'test pd code1one';
        
                insert testprod1;
         Product2 testprod2 = new Product2 (name='test product two2');
         testprod2.productcode = 'test pd code2two';
         
         
         insert testprod2;
// Get Pricebook
         Pricebook2 testpb = [select id from Pricebook2 where IsStandard = true];
         testpb.IsActive=true;
         update testpb;   
// Add to pricebook
        PriceBookEntry pbe1 = new PricebookEntry();
        pbe1.UnitPrice = 100;
        pbe1.Product2Id = testprod1.Id;
        pbe1.Pricebook2Id = testpb.id;
        pbe1.IsActive = true;
        pbe1.useStandardPrice = false;
        insert pbe1;
        PriceBookEntry pbe2 = new PricebookEntry();
        pbe2.UnitPrice = 200;
        pbe2.Product2Id = testprod2.Id; 
        pbe2.Pricebook2Id = testpb.id;
        pbe2.IsActive = true;
        pbe2.useStandardPrice = false;
        insert pbe2;

Test.startTest();

Oppty1.StageName='Selected';
update Oppty1;
System.assertNotEquals('Prospecting', Oppty1.StageName); 

Test.stopTest();
}
}
parth jollyparth jolly
Hi Abhi Malik ,
Try this it will cover 90 % code coverage
@isTest(seeAllData=true)
private class TestUpdateClosedListPrice {

static testMethod void TestUpdateClosedListPrice() {

Account acc = new Account(Name = 'Test Account');
insert acc;

//get standard pricebook
Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];

Pricebook2 pbk1 = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1', isActive=true);
insert pbk1;

Product2 prd1 = new Product2 (Name='Test Product Entry 1',Description='Test Product Entry 1',productCode = 'ABC', isActive = true);
insert prd1;


PricebookEntry pbe1 = new PricebookEntry (Product2ID=prd1.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true);
insert pbe1;


Opportunity opp1 = new Opportunity (Name='Opp1',StageName='Stage 0 - Lead Handed Off',CloseDate=Date.today(),Pricebook2Id = pbe1.Pricebook2Id, AccountId = acc.id);
insert opp1;


OpportunityLineItem lineItem1 = new OpportunityLineItem (OpportunityID=opp1.id,PriceBookEntryID=pbe1.id, quantity=4, totalprice=200);
insert lineItem1;

Test.startTest();

opp1.StageName='Stage 8 - Shipped';
update opp1;
System.assertNotEquals(0, lineItem1.totalprice);

Test.stopTest();
}
}

 
Board salesforceBoard salesforce
Hi Abhi Malik ,

Please assign 
oppty1.name='Test';
oppty1.accountid =a.id;
insert required fiedls for  opportunity and for the pricebook and opplineitem insertion follow the below code :
Id pricebookId = Test.getStandardPricebookId();

//Create your product
Product2 prod = new Product2(
     Name = 'Product X',
     ProductCode = 'Pro-X',
     isActive = true
);
insert prod;

//Create your pricebook entry
PricebookEntry pbEntry = new PricebookEntry(
     Pricebook2Id = pricebookId,
     Product2Id = prod.Id,
     UnitPrice = 100.00,
     IsActive = true
);
insert pbEntry;

//create your opportunity line item.  This assumes you already have an opportunity created, called opp
OpportunityLineItem oli = new OpportunityLineItem(
     OpportunityId = opp.Id,
     Quantity = 5,
     PricebookEntryId = pbEntry.Id,
     TotalPrice = quantity * pbEntry.UnitPrice
);
insert oli;

do the update logic below this .

Mark it as solved if it helps you,
Thanks ,
Karthik.
Abhi MalikAbhi Malik
Thanks everyone for reply
am trying both the codes but getting the erroe

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, This price definition already exists in this price book: []
Board salesforceBoard salesforce
Malik,
I think you are using of both our codes ,dont combine it either use any one snippet if you use my snippet no need of making seealldata=true in your test class.

Thanks ,
karthik
Abhi MalikAbhi Malik
am using only yours code but getting the same error
@isTest
public class TestQuantityMOQValidate {
        static testMethod void TestMOQValidate() {
        
        Account acc = new Account(Name = 'Test Account');
        insert acc;
      
        Pricebook2 pbk1 = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1', isActive=true);
        insert pbk1;
            
        Opportunity opp1 = new Opportunity (Name='Opp1',StageName='Stage 0 - Lead Handed Off',CloseDate=Date.today(), AccountId = acc.id);
        insert opp1;
        
        Id pricebookId = Test.getStandardPricebookId();
        
        //Create your product
        Product2 prod = new Product2(
             Name = 'Product X',
             ProductCode = 'Pro-X',
             isActive = true
        );
        insert prod;
        
        //Create your pricebook entry
        PricebookEntry pbEntry = new PricebookEntry(
             Pricebook2Id = pricebookId,
             Product2Id = prod.Id,
             UnitPrice = 100.00,
             IsActive = true
        );
        insert pbEntry;
        
        //create your opportunity line item.  This assumes you already have an opportunity created, called opp
        OpportunityLineItem oli = new OpportunityLineItem(
             OpportunityId = opp1.Id,
             Quantity = 5,
             PricebookEntryId = pbEntry.Id,
             TotalPrice = 234
        );
        insert oli;
               
}
}

Error:
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, This price definition already exists in this price book: []
Board salesforceBoard salesforce
Malik ,
Remove this and try it .
Pricebook2 pbk1 = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1', isActive=true);
        insert pbk1;

 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post
1) http://salesforce.stackexchange.com/questions/8334/help-with-test-coverage
Please check below code
@isTest
public class TestQuantityMOQValidate {
        static testMethod void TestMOQValidate() 
		{
        
			Account acc = new Account(Name = 'Test Account');
			insert acc;
		  
				
			Opportunity opp1 = new Opportunity (Name='Opp1',StageName='Stage 0 - Lead Handed Off',CloseDate=Date.today(), AccountId = acc.id);
			insert opp1;
			
			Id pricebookId = Test.getStandardPricebookId();
			
			//Create your product
			Product2 prod = new Product2(
				 Name = 'Product X',
				 ProductCode = 'Pro-X',
				 isActive = true
			);
			insert prod;
			
			//Create your pricebook entry
			PricebookEntry pbEntry = new PricebookEntry(
				 Pricebook2Id = pricebookId,
				 Product2Id = prod.Id,
				 UnitPrice = 100.00,
				 IsActive = true
			);
			insert pbEntry;
			
			//create your opportunity line item.  This assumes you already have an opportunity created, called opp
			OpportunityLineItem oli = new OpportunityLineItem(
				 OpportunityId = opp1.Id,
				 Quantity = 5,
				 PricebookEntryId = pbEntry.Id,
				 TotalPrice = 234
			);
			insert oli;
               
		}
}

Let us know if this will help you