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
Tommy GeorgiouTommy Georgiou 

help with test class

Hi I am really new in apex and programming. 

I received help for the trigger 
trigger UpdateQuoteCheckBox on QuoteLineItem (after insert, after update)
{
    List<Quote> quoteList = new List<Quote>();
    
    for(QuoteLineItem currQli : Trigger.New)
    {
        if(currQli.Product2.Family == 'Your Product Family Name')
        {
            quoteList.add(new Quote(Id = currQli.QuoteId, Family__c = true));
        }
    }
    
    if(!quoteList.isEmpty())
    {
      update quoteList;
    }
}

but do not know how to write the test class for it. I have searched a bit on 
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
 But still don't get. Any help would be appreciated. Thank you in advance
Best Answer chosen by Tommy Georgiou
sandeep sankhlasandeep sankhla
Hi Tommy,

Please refer the below class and insert the quoteLineObject in test class and cover the trigger code..

@isTest
private class SL_TestCloneQuote {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        
        
        Opportunity objOpportunity = new Opportunity(Name='Test-Opportunty', AccountId=objAccount.Id, CloseDate=Date.Today(),
                                                     StageName='Specified', Project__c=objProject.Id);
        insert objOpportunity;
        system.assert(objOpportunity.Id != NULL);
        
        
        Quote objQuote = new Quote(Name='Test-Quote', OpportunityId=objOpportunity.Id, Contractor_Account__c = objAccount.Id, Lead_Time__c = 'test lead time', Pricebook2Id = pricebookId);
        insert objQuote;
        system.assert(objQuote.Id != NULL);
        
        Product2 objProduct1 = new Product2(Name = 'test01', isActive=true, Manufacturer__c = objAccount.Id, Supplier_Price__c = 12.0);
        insert objProduct1;
        system.assert(objProduct1.Id != NULL);
        
        PricebookEntry objPriceBookEntry = new PricebookEntry(Pricebook2Id=pricebookId, Product2Id=objProduct1.Id, UnitPrice=99, isActive=true);
        insert objPriceBookEntry;
        system.assert(objPriceBookEntry.Id != NULL);
        
        List<QuoteLineItem> lstQLI = new List<QuoteLineItem>{
                                                            new QuoteLineItem(QuoteId=objQuote.Id, PricebookEntryId=objPriceBookEntry.Id, Quantity=0.1, UnitPrice=1.1,
                                                                              Override_Auto_Tiered_Pricing__c = true)
                                                            };      
        insert lstQLI;
        
        
        List<QuoteLineItem> lstClonedQLI = new List<QuoteLineItem>([select Id from QuoteLineItem where QuoteId =: str]);
        
                
    }
}

Please mark this as best answer if it work to make this community clean and re-usable..Try and let me know if it work for you..

All Answers

sandeep sankhlasandeep sankhla
Hi Tommy,

Please refer the below class and insert the quoteLineObject in test class and cover the trigger code..

@isTest
private class SL_TestCloneQuote {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        
        
        Opportunity objOpportunity = new Opportunity(Name='Test-Opportunty', AccountId=objAccount.Id, CloseDate=Date.Today(),
                                                     StageName='Specified', Project__c=objProject.Id);
        insert objOpportunity;
        system.assert(objOpportunity.Id != NULL);
        
        
        Quote objQuote = new Quote(Name='Test-Quote', OpportunityId=objOpportunity.Id, Contractor_Account__c = objAccount.Id, Lead_Time__c = 'test lead time', Pricebook2Id = pricebookId);
        insert objQuote;
        system.assert(objQuote.Id != NULL);
        
        Product2 objProduct1 = new Product2(Name = 'test01', isActive=true, Manufacturer__c = objAccount.Id, Supplier_Price__c = 12.0);
        insert objProduct1;
        system.assert(objProduct1.Id != NULL);
        
        PricebookEntry objPriceBookEntry = new PricebookEntry(Pricebook2Id=pricebookId, Product2Id=objProduct1.Id, UnitPrice=99, isActive=true);
        insert objPriceBookEntry;
        system.assert(objPriceBookEntry.Id != NULL);
        
        List<QuoteLineItem> lstQLI = new List<QuoteLineItem>{
                                                            new QuoteLineItem(QuoteId=objQuote.Id, PricebookEntryId=objPriceBookEntry.Id, Quantity=0.1, UnitPrice=1.1,
                                                                              Override_Auto_Tiered_Pricing__c = true)
                                                            };      
        insert lstQLI;
        
        
        List<QuoteLineItem> lstClonedQLI = new List<QuoteLineItem>([select Id from QuoteLineItem where QuoteId =: str]);
        
                
    }
}

Please mark this as best answer if it work to make this community clean and re-usable..Try and let me know if it work for you..
This was selected as the best answer
Tommy GeorgiouTommy Georgiou
Thank you very much Sandeep