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
Silpi roy 16Silpi roy 16 

Test class deployment error

I have written the following code in sandbox which is showing 100% test coverage .
But during deployment it is showing the following error:
System.AssertException: Assertion Failed: Expected: false, Actual: true 
Stack Trace: Class.DeleteBillingItemsTest.DeleteBillingItemsTest: line 37, column 1.
Code is as follows:
public class DeleteBillingItems
{
    @InvocableMethod
    public static void BIDelete(List<Id> BIIds)
    {
        List<BillingItem__c> BI =[select id,BillingDocumentId__c,UnitPrice__c,Sub_Segment__c,Gross__c,NettoAmount2__c from BillingItem__c
                          where BillingItem__c.id in :BIIds and BillingDocumentId__c!=Null
                       and UnitPrice__c=0 and Sub_Segment__c='Undefined' and Gross__c=0 and NettoAmount2__c =0 ];
        delete BI;
        }
}

@isTest
Public class DeleteBillingItemsTest{
Static testMethod void DeleteBillingItemsTest(){
Account acc=new Account();
acc.Name='Test';
insert acc;

 Product2 Pd1=new Product2();
   Pd1.Name='Pd01';
   Pd1.Production_Location__c='SMT-TH';
   Pd1.SAP_Product_Nr__c='123';
  Pd1.Module_Source__c='bought';
   insert pd1;
        
 BillingDoc__c BD1=new BillingDoc__c();
            BD1.DocumentType__c='Test01';
            BD1.Name='001';
            BD1.Production_Location__c='SMT-TH';
            BD1.DocumentTypeCode__c='ZOR';
            BD1.DocumentDate__c=System.Today();
            BD1.FacturaDate__c=System.Today();
        insert BD1;

BillingItem__c BI= new BillingItem__c();
BI.Name='Test';
BI.Quantity__c=100;
BI.Units__c='PCE';
BI.UnitPrice__c= 500;
BI.Product2__c=PD1.id;
BI.BillingDocumentId__c=BD1.ID;


insert BI;
BI.UnitPrice__c= 0;
Update BI; 
BillingItem__c deletedBI = [SELECT Id, IsDeleted FROM BillingItem__c WHERE Id = :BI.Id ALL ROWS];
System.assertEquals(deletedBI.IsDeleted, true);



}

}
Steven NsubugaSteven Nsubuga
You did not call the class to delete the BIs.
@isTest
Public class DeleteBillingItemsTest{
Static testMethod void DeleteBillingItemsTest(){
Account acc=new Account();
acc.Name='Test';
insert acc;

 Product2 Pd1=new Product2();
   Pd1.Name='Pd01';
   Pd1.Production_Location__c='SMT-TH';
   Pd1.SAP_Product_Nr__c='123';
  Pd1.Module_Source__c='bought';
   insert pd1;
        
 BillingDoc__c BD1=new BillingDoc__c();
            BD1.DocumentType__c='Test01';
            BD1.Name='001';
            BD1.Production_Location__c='SMT-TH';
            BD1.DocumentTypeCode__c='ZOR';
            BD1.DocumentDate__c=System.Today();
            BD1.FacturaDate__c=System.Today();
        insert BD1;

BillingItem__c BI= new BillingItem__c();
BI.Name='Test';
BI.Quantity__c=100;
BI.Units__c='PCE';
BI.UnitPrice__c= 500;
BI.Product2__c=PD1.id;
BI.BillingDocumentId__c=BD1.ID;


insert BI;
BI.UnitPrice__c= 0;
Update BI; 
List<Id> BIIds = new List<Id>();
BIIds.add(BI.Id);
DeleteBillingItems.BIDelete(BIIds);

BillingItem__c deletedBI = [SELECT Id, IsDeleted FROM BillingItem__c WHERE Id = :BI.Id ALL ROWS];
System.assertEquals(deletedBI.IsDeleted, true);



}

}

 
Paras_BhattParas_Bhatt

Hi,

 

If you have automated trigger/automation that deleted the record as soon as you set UnitPrice__c == 0, Make sure you deploy this trigger/code along with your test class deployment.

Regards,

Paras Bhatt

Silpi roy 16Silpi roy 16
Thanks a lot Steven and Paras for the Reply.
Steven yes thet worked.One more query:
public class DeleteBillingItems
{
    @InvocableMethod
    public static void BIDelete(List<Id> BIIds)
    {
        List<BillingItem__c> BI =[select id,BillingDocumentId__c,UnitPrice__c,Sub_Segment__c,Gross__c,NettoAmount2__c from BillingItem__c
                          where BillingItem__c.id in :BIIds and BillingDocumentId__c!=Null
                       and UnitPrice__c=0 and Sub_Segment__c='Undefined' and Gross__c=0 and NettoAmount2__c =0 ];
        delete BI;
        }
}

This code is deleting the Billing item once created with menntioned criteria.But if we are undeleting then in that case also I want to delete the Item.Or we can say I want to permaently delete it
 
Steven NsubugaSteven Nsubuga
To do a permanent or hard delete, below delete BI, add this line
Database.emptyRecycleBin(BI);
Silpi roy 16Silpi roy 16
In that case assert statement will hit error.
Steven NsubugaSteven Nsubuga
Yes, the assert as it is would fail. Change it 
@isTest
Public class DeleteBillingItemsTest{
Static testMethod void DeleteBillingItemsTest(){
Account acc=new Account();
acc.Name='Test';
insert acc;

 Product2 Pd1=new Product2();
   Pd1.Name='Pd01';
   Pd1.Production_Location__c='SMT-TH';
   Pd1.SAP_Product_Nr__c='123';
  Pd1.Module_Source__c='bought';
   insert pd1;
        
 BillingDoc__c BD1=new BillingDoc__c();
            BD1.DocumentType__c='Test01';
            BD1.Name='001';
            BD1.Production_Location__c='SMT-TH';
            BD1.DocumentTypeCode__c='ZOR';
            BD1.DocumentDate__c=System.Today();
            BD1.FacturaDate__c=System.Today();
        insert BD1;

BillingItem__c BI= new BillingItem__c();
BI.Name='Test';
BI.Quantity__c=100;
BI.Units__c='PCE';
BI.UnitPrice__c= 500;
BI.Product2__c=PD1.id;
BI.BillingDocumentId__c=BD1.ID;


insert BI;
BI.UnitPrice__c= 0;
Update BI; 
List<Id> BIIds = new List<Id>();
BIIds.add(BI.Id);
DeleteBillingItems.BIDelete(BIIds);

deletedBI = [SELECT Id, IsDeleted FROM BillingItem__c WHERE Id = :BI.Id ALL ROWS];
System.assertEquals(deletedBI.IsDeleted, false);



}

}

 
Silpi roy 16Silpi roy 16
IsDeleted is it a correct field
Silpi roy 16Silpi roy 16
Getting the following error:Error MessageSystem.UnexpectedException: MISSING_ARGUMENT: emptyRecycleBin called with a batch of 0 ids to delete; must specify at least 1 id
Stack TraceClass.DeleteBillingItems.BIDelete: line 10, column 1
Class.DeleteBillingItemsTest.DeleteBillingItemsTest: line 38, column 1
Steven NsubugaSteven Nsubuga
Yes it is, but try doing the check in another way.
@isTest
Public class DeleteBillingItemsTest{
Static testMethod void DeleteBillingItemsTest(){
Account acc=new Account();
acc.Name='Test';
insert acc;

 Product2 Pd1=new Product2();
   Pd1.Name='Pd01';
   Pd1.Production_Location__c='SMT-TH';
   Pd1.SAP_Product_Nr__c='123';
  Pd1.Module_Source__c='bought';
   insert pd1;
        
 BillingDoc__c BD1=new BillingDoc__c();
            BD1.DocumentType__c='Test01';
            BD1.Name='001';
            BD1.Production_Location__c='SMT-TH';
            BD1.DocumentTypeCode__c='ZOR';
            BD1.DocumentDate__c=System.Today();
            BD1.FacturaDate__c=System.Today();
        insert BD1;

BillingItem__c BI= new BillingItem__c();
BI.Name='Test';
BI.Quantity__c=100;
BI.Units__c='PCE';
BI.UnitPrice__c= 500;
BI.Product2__c=PD1.id;
BI.BillingDocumentId__c=BD1.ID;


insert BI;
BI.UnitPrice__c= 0;
Update BI; 
List<Id> BIIds = new List<Id>();
BIIds.add(BI.Id);
DeleteBillingItems.BIDelete(BIIds);

List<BillingItem__c> deletedBIs = [SELECT Id  FROM BillingItem__c WHERE Id = :BI.Id ALL ROWS];
System.assertEqual(deletedBIs.size(), 0);



}

}

 
Steven NsubugaSteven Nsubuga
My mistake, replace delete BI with Database.emptyRecycleBin(BI)
 
Silpi roy 16Silpi roy 16
Nope in that case the recod is not getting deleted on creation.Previously whenever I am trying to create a record with the mentioned criteria it was getting deleted immediately but not it is not.