You need to sign in to do that
Don't have an account?
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;
}
All Answers
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);
}
}
System.assertEquals(newProduct.Product_Description_Formatted__c,newQuoteLineItem.Product_Description_Formatted__c);