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
Dee Dee AaronDee Dee Aaron 

Code Coverage Failure: Your code coverage is 0%.

Hi. I found this apex class online (it's super simple). It triggered successfully in my sandbox. When deploying to my production org, I tried validating and received an error: 

Code Coverage Failure: Your code coverage is 0%. 

This is the Apex Class:
public class DeleteUnacceptedQuotes 
{
@InvocableMethod    
public static void QuoteDelete(List<Id> OpportunityIds)    
{        
List<Quote> Quotes =[select id from quote                          
where Opportunity.id in :OpportunityIds                       
and Status != 'Accepted'];        
delete Quotes;   
}
}


Thank you for your help!

Best Answer chosen by Dee Dee Aaron
Maharajan CMaharajan C
Hi Aaraon,

We need the test class also for prod deployment.

Please use the below test class:

Add if there is any required fields are missing while inserting the test records below.
@isTest
public class DeleteUnacceptedQuotesTest {
    @isTest static void QuoteDeleteTest(){
        
        Account objAccount = new Account(Name = 'Test Acc1', BillingCity = 'Test City', BillingState = 'Test State', 
                                         BillingStreet = 'Test Street', BillingPostalCode = '12345', 
                                         BillingCountry = 'Test Country', Phone = '123456');
        insert objAccount;
        
        Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true );
        insert pb;
        
        Product2 objProduct = new Product2(Name = 'Test product1',  IsActive = true);
        insert objProduct;
        
        PriceBookEntry objPBE = new PriceBookEntry(UnitPrice = 300, PriceBook2Id = Test.getStandardPricebookId(),
                                                   Product2Id = objProduct.Id, IsActive = true);        
        insert objPBE;
        
        PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb.Id, Product2Id = objProduct.Id, UnitPrice = 1000, IsActive = true);
        insert pbe;
        
        Opportunity objOpp = new Opportunity(Name = 'Test Opp', AccountId = objAccount.Id, StageName = 'Qualification', CloseDate = Date.today()+1);
        insert objOpp;
        
        Quote q= new Quote(OpportunityId = objOpp.Id, Name = 'Test Quote', Pricebook2Id = pb.Id, Status = 'Review Pending');
        insert q;
        
        Test.startTest();
        DeleteUnacceptedQuotes.QuoteDelete(new List<Id>{objOpp.Id});
        Test.stopTest();
        List<Quote> lstQuote = [Select Id from Quote];
        system.assertEquals(0, lstQuote.size());
    }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Aaraon,

We need the test class also for prod deployment.

Please use the below test class:

Add if there is any required fields are missing while inserting the test records below.
@isTest
public class DeleteUnacceptedQuotesTest {
    @isTest static void QuoteDeleteTest(){
        
        Account objAccount = new Account(Name = 'Test Acc1', BillingCity = 'Test City', BillingState = 'Test State', 
                                         BillingStreet = 'Test Street', BillingPostalCode = '12345', 
                                         BillingCountry = 'Test Country', Phone = '123456');
        insert objAccount;
        
        Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true );
        insert pb;
        
        Product2 objProduct = new Product2(Name = 'Test product1',  IsActive = true);
        insert objProduct;
        
        PriceBookEntry objPBE = new PriceBookEntry(UnitPrice = 300, PriceBook2Id = Test.getStandardPricebookId(),
                                                   Product2Id = objProduct.Id, IsActive = true);        
        insert objPBE;
        
        PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb.Id, Product2Id = objProduct.Id, UnitPrice = 1000, IsActive = true);
        insert pbe;
        
        Opportunity objOpp = new Opportunity(Name = 'Test Opp', AccountId = objAccount.Id, StageName = 'Qualification', CloseDate = Date.today()+1);
        insert objOpp;
        
        Quote q= new Quote(OpportunityId = objOpp.Id, Name = 'Test Quote', Pricebook2Id = pb.Id, Status = 'Review Pending');
        insert q;
        
        Test.startTest();
        DeleteUnacceptedQuotes.QuoteDelete(new List<Id>{objOpp.Id});
        Test.stopTest();
        List<Quote> lstQuote = [Select Id from Quote];
        system.assertEquals(0, lstQuote.size());
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
Dee Dee AaronDee Dee Aaron
Hi Maharajan. Thank you for answering my question. I am marking as best answer-- even though I ended up using a process builder and a flow to accomplish this. I'm sharing here for other users that might be looking to do the same thing: https://trailblazers.salesforce.com/answers?id=9064S000000DKhwQAG