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
cruttley2cruttley2 

I have my first simple trigger that is working, but I dont know how to write a test class.

I am not an Apex developer, but I have been able to get my first simple trigger working in my sandbox. However, I am wondering if someone could guide me throught the writing of a Test Class, so that I can get this promoted to production. All help greatly appreciated. Here is my trigger code:

 

// This trigger assigns a value to the QuoteLineItem Product Description Formatted field from a PriceBookEntry field
// before Quote Line Items are saved to the database.
 
trigger QuoteLineItemProductDescriptionFormatted on QuoteLineItem (before insert) {
   
        // Determine the distinct pricebook entries
    Set<ID> pbeIds = new Set<ID>();
    for (QuoteLineItem oli : Trigger.new)
        pbeIds.add(oli.pricebookentryid);
 
    // Query the pricebook entries
    Map<Id, PricebookEntry> entries = new Map<Id, PricebookEntry>(
        [select product2.Product_Description_Formatted__c from pricebookentry
         where id in :pbeIds]);
      
     // Now set the Product Description Formatted on the Quote line items
    for (QuoteLineItem oli : Trigger.new)      
        oli.Product_Description_Formatted__c = entries.get(oli.pricebookEntryId).product2.Product_Description_Formatted__c;
 
}

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
Basically, your test code just needs to create a product, add it to a price book (create a price book entry record), create an opportunity, then a quote, then a quote line item. Finally, you should re-query the record value and make sure that it matches the expected value.

All Answers

sfdcfoxsfdcfox
Basically, your test code just needs to create a product, add it to a price book (create a price book entry record), create an opportunity, then a quote, then a quote line item. Finally, you should re-query the record value and make sure that it matches the expected value.
This was selected as the best answer
cruttley2cruttley2

Thanks for your help sfdcfox. I have coded my test class and done all the inserts see below. I have 100% code coverage.....the only thing I dont know how to do is retrieve the quotelinelitem I just inserted and verify that its rich text field (product_description_formatted__c) equals the value 'THIS IS A RICH TEXT DESCRIPTION'. 

Are you able to help me with that coding?

 

 

 

public class TestQuoteLineItemProdDescUpdate{

static testmethod void QuoteLineItemProductDescriptionFormatted (){
Product2 newproduct = new Product2(Name = 'Chris Test Product', IsActive = True,Product_Description_Formatted__C = 'THIS IS A RICH TEXT DESCRIPTION');
insert newProduct;
system.assertNotEquals(newProduct, null);

PriceBookEntry newpricebookentry = new PriceBookEntry(PriceBook2ID = '01sU0000000JVEnIAO', Product2ID = newProduct.id,IsActive = True,UnitPrice=10.10,UseStandardPrice=False);
insert newPriceBookEntry;
system.assertNotEquals(newPriceBookEntry, null);

Opportunity newopportunity = new Opportunity(AccountId = '001Z000000NDpCH',PriceBook2ID = '01sU0000000JVEnIAO', Name = 'OPPORTUNITY',StageName='Opportunity Identification',CloseDate=System.Today());
insert newOpportunity;
system.assertNotEquals(newOpportunity, null);

Quote newquote = new Quote(OpportunityId = newopportunity.id,ContactID = '003Z000000KwBjs', Name = 'Quote',PriceBook2ID = '01sU0000000JVEnIAO');
insert newQuote;
system.assertNotEquals(newQuote, null);

QuoteLineItem newquotelineitem = new QuoteLineItem(QuoteId = newquote.id,PriceBookEntryId = newpricebookentry.id,quantity=6,unitprice=8.88);
insert newQuoteLineItem;
system.assertNotEquals(newQuoteLineItem, null);

WHAT GOES HERE? I WANT TO VERIFY THAT newquotelineitem.product_description_formatted__c = 'THIS IS A RICH TEXT DESCRIPTION'

system.debug(newProduct);
}
}

sfdcfoxsfdcfox
newQuoteLineItem = [SELECT Id,Product_Description_Formatted__c FROM QuoteLineItem WHERE Id = :newQuoteLineItem.Id];
System.assertEquals(newProduct.Product_Description_Formatted__c,newQuoteLineItem.Product_Description_Formatted__c);
cruttley2cruttley2
Thanks sfdcfox. This is awesome. I now have a working trigger that is deployable to production....and I hopefully can create other simple triggers and test classes like this going forward (you taught me how to fish!).