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
Alex ReadingAlex Reading 

Help with APEX Test Class

Hey Guys,

Let me apologize in advance because I am a complete Newby when it comes to Test Classes in Apex - Unfortunately I have been tasked with moving an Apex Trigger that I created from Sandbox to Production. I figured out how to do this using changesets but the trigger won't deploy due to it failing some tests. I realise that I need to write a test class before deploying the trigger and that is where I am stuck. Below is my APEX Trigger - Pretty standard for creating a quote when an opportunity is closed-won and has already had a quote synced with it. The HasQuote__C field is a formula checkbox that is true when a quote has been synced with the opportunity. You can see the opportunity line items are being picked up and used as QuoteLineItems in the created quote.

trigger InvoicefromQuote on Opportunity (after insert, after update) {
     for(Opportunity o: trigger.new){
      if(o.isWon == true && o.HasOpportunityLineItem == true && o.HasQuote__C == true ){
         String opptyId = o.Id;
         String opname = o.name;
         OpportunityLineItem[] OLI = [Select UnitPrice, Quantity, PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Description, Converted_to_Asset__c 
                                      From OpportunityLineItem
                                      where OpportunityId = :opptyId];
         quote[] quote = new quote[]{};
         quotelineitem[] qli = new quotelineitem[]{};
         
         quote a = new Quote();
         a.name = 'Invoice for ' + o.name;
         a.recordtypeid = '012D0000000jWpqIAE';
         a.opportunityId = o.id;
         a.Pricebook2Id = o.Pricebook2Id;
         a.description = 'This is an automatically generated invoice - because the opportunity: (' + o.name +') was closed won with an (Create Invoice Enabled) Quote' ;
         insert a;

        
         for(OpportunityLineItem ol: OLI){
            quotelineitem b = new QuoteLineItem();
            b.PricebookEntryId = ol.PricebookEntryId;
            b.UnitPrice = ol.UnitPrice;
            b.Product2Id = ol.PriceBookEntry.Product2Id;
            b.Quantity = ol.Quantity;
            b.QuoteId = a.id;
            insert b;
           
}
}
}
}

Any help or examples that anyone can provide on how I can smoothly move this over to our production environment would be greatly appreciated, any questions or requests for info welcome.

Thanks in advance.
Subramani_SFDCSubramani_SFDC
insert opportunity,pricebook,opplineitem,quote object record in test class will give 100% coverage
Alex ReadingAlex Reading
@Subramani_SFDC

How would I go about writing this in APEX? Also would I have to deploy my TestClass first and then my trigger?

Thanks for your help,

Again apologies - This is all new to me :)
Subramani_SFDCSubramani_SFDC
try this

@isTest
Private class InvoicefromQuote_test
{
static testMethod void testInvoicefromQuote()
{
RecordType rt = [select id,Name from RecordType where SobjectType='opportunity' and Name='A' Limit 1];
Opportunity o = new Opportunity(Name = 'Test' , StageName ='Closed Won' , CloseDate = Date.today());
insert o;

Product2 p = new Product2(Name = 'productname');
insert p;

Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true];

Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true);
insert pb;
Product2 prod = new Product2(Name = 'productname', Family = 'Best Practices' , IsActive = true);
insert prod;

PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = standardPB.Id, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
insert standardPrice;

quote a = new Quote();
a.name = 'Invoice for ' + o.name;
a.recordtypeid = rt.id';
a.opportunityId = o.id;
a.Pricebook2Id = o.Pricebook2Id;
a.description = 'This is an automatically generated invoice  Quote' ;
insert a;


OpportunityLineItem ol = new OpportunityLineItem(OpportunityId = o.id , QuoteId=a.id,PricebookEntryId = standardPrice.id);
insert ol;
}
Alex ReadingAlex Reading
Hey Subramani,

Thanks for your help, really appreciate that, I ran the above test in Sandbox and unfortunately ran into the following error:
Error Message   System.QueryException: List has no rows for assignment to SObject
Stack Trace	Class.InvoicefromQuote_test.testInvoicefromQuote: line 6, column 1

This is an error I have been constantly running into, any more help?

Thanks again :)