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
Shri BEShri BE 

first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: Quantity (quantity must be nonzero): [Quantity]

Hi Experts,

I am getting the below error when I insert Opportunity Line Item in Test Class for trigger. Kindly help me how to rectify this error. Because of this my Code coverage is 70%.

first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: Quantity (Quantity mus be nonzero): [Quantity]

Your inputs are needed.

Thanks
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Salesforce does not allow specific fields (i.e. opportunity line items value) to have a value of 0, please change the quantity to a non-zero number.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Shri BEShri BE
Hi Khan,

Below is my Test Class. I am still getting this error.

@isTest(SeeAllData=true)
public class insertOppsLineitemTest {
    static testMethod void testorderItems() {                    
        Opportunity opp = [SELECT Id, Name FROM Opportunity WHERE Name ='Sample'];
        
        Pricebookentry pbe = [SELECT Id, Name FROM Pricebookentry WHERE Name = 'Sample Product'];
        
        orderItems__c oit = [SELECT Id, Name, Name__c FROM OrderItems__c WHERE Name__c = 'Testing'];
        
        Opportunitylineitem oilt = [SELECT Id, Name__c FROM Opportunitylineitem WHERE Name__c = 'Sample Product'];        
        
        OpportunityLineItem item = new OpportunityLineItem(
            pricebookentryid=pbe.Id,
            TotalPrice=2000,
            Quantity = 2,
            OpportunityID = opp.Id
        );
        insert item;      
        
        oilt.UnitPrice = 0;
        update oilt; 
               
    }
}

Thanks.
Meghna Vijay 7Meghna Vijay 7
Hi Shri,

Can you set UnitPrice to a non-zero number?

Thanks
Raj VakatiRaj Vakati
try this
 
@isTest(SeeAllData=false)
public class insertOppsLineitemTest {
    static testMethod void testorderItems() {   


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;

orderItems__c oddd = new orderItems__c();
oddd.Name__c = 'Testing';
insert oddd ; 

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;



	 
        
        lineItem1.UnitPrice = 10;
        update oilt; 
               
    }
}