Hey guys, today in this post we are going to learn about How to Create Apex Test Class for Quote and Quote Line Item in salesforce.
This class is defined using the @IsTest annotation. Classes defined this way should only contain test methods and any methods required to support those test methods. One advantage to creating a separate class for testing is that classes defined with @IsTest donβt count against your orgβs limit of 6 MB of Apex code. You can also add the @IsTest annotation to individual methods. For more information, see @IsTest Annotation and Execution Governors and Limits. To know more details about Test Class, Click Here.
Final Output β
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
- Find the below steps βΎ
Create Apex Class Controller
Step 1:- Create Apex Class : createQuoteAndLineItemLwcCtrl.apxc
From Developer Console >> File >> New >> Apex Class
createQuoteAndLineItemLwcCtrl.apxc [Apex Class Controller]
public WITH sharing class createQuoteAndLineItemLwcCtrl {
public createQuoteAndLineItemLwcCtrl() {
}
@AuraEnabled(cacheable=TRUE)
public static List<Opportunity> getOpportunityList(Id recId){
List<Opportunity> optList = [SELECT Id, Name, Description FROM Opportunity WHERE Id=:recId];
RETURN optList;
}
//START Quote
@AuraEnabled
public static String submitOptRecord(String jsonDataStr) {
Map<String, Object> RESULT = NEW Map<String, Object>();
String DataRespoce ='';
try {
Map<String, Object> formDataMap = (Map<String, Object>)JSON.deserializeUntyped(jsonDataStr);
Map<String, Object> OptDataMap = (Map<String, Object>)formDataMap.get('optDataFyObj');
// Opportunity optObj = NEW Opportunity();
Quote optObj = NEW Quote();
optObj.Name = getStringValueFromMap(OptDataMap, 'Name');
optObj.OpportunityId = getStringValueFromMap(OptDataMap, 'OpportunityId');
optObj.Description = getStringValueFromMap(OptDataMap, 'Description');
optObj.Pricebook2Id = getStringValueFromMap(OptDataMap, 'Pricebook2Id');
List<DATABASE.SaveResult> insertResult = DATABASE.insert(NEW List<Quote>{optObj});
DataRespoce =JSON.serialize(insertResult);
}catch(Exception ex) {
RESULT.put('status', 500);
RESULT.put('message', 'Exception ' + ex.getMessage() + ',line' + ex.getLineNumber());
}
RETURN DataRespoce;
}
public static String getStringValueFromMap(Map<String, Object> dataMap, String fieldName) {
String VALUE;
try {
IF(dataMap.containsKey(fieldName)) {
VALUE = String.valueOf(dataMap.get(fieldName));
}
VALUE = String.isEmpty(VALUE) ? VALUE : String.valueOf(VALUE);
} catch(Exception ex) {
System.debug('Exception getValueFromMap : '+ ex.getMessage() + ' line ' + ex.getLineNumber());
}
RETURN VALUE;
}
public static DATE getDateValueFromMap(Map<String, Object> dataMap, String fieldName) {
DATE VALUE;
try {
String str;
IF(dataMap.containsKey(fieldName)) {
str = String.valueOf(dataMap.get(fieldName));
}
VALUE = String.isEmpty(str) ? VALUE : DATE.valueOf(str);
} catch(Exception ex) {
System.debug('Exception getIntValueFromMap : '+ ex.getMessage() + ' line ' + ex.getLineNumber());
}
RETURN VALUE;
}
//START Quote Line Item
@AuraEnabled
public static void saveQuoteLineItem(List<QuoteLineItem> quoteItemList){
try {
INSERT quoteItemList;
System.debug('quoteItemList## ' + quoteItemList);
}
catch(Exception ex) {
System.debug('Exception getValueFromMap : '+ ex.getMessage() + ' line ' + ex.getLineNumber());
}
}
//END Quote Line Item
}
Create Test Class Controller
Step 2:- Create Apex Class : createQuoteAndLineItemLwcCtrlTest.apxc
As per student's demands, I re-opened my eBook of "Salesforce Tutorial" Limited-time huge discount offer Absolutely 50% Off.
I am thinking to give you discount offer occasion of Gandhi Jayanti for a powerful "Salesforce admin course" where you can Understand from Basic Concepts to advanced label in Salesforce.
π So Don't MISS it... (Access Right Now)
π Get Huge Discount Offer 50%: - Get eBook
From Developer Console >> File >> New >> Apex Class
createQuoteAndLineItemLwcCtrlTest.apxc [Apex Class Controller]
@isTest
public class createQuoteAndLineItemLwcCtrlTest {
static testmethod void getOpportunityListTest(){
opportunity opt = NEW opportunity();
opt.Name='test';
opt.StageName='Prospecting';
opt.CloseDate=system.today();
INSERT opt;
test.startTest();
createQuoteAndLineItemLwcCtrl.getOpportunityList(opt.Id);
test.stopTest();
}
static testmethod void submitOptRecordTest(){
String jsonStr =
'{"invoiceList":[' +
'{"totalPrice":5.5,"statementDate":"2011-10-04T16:58:54.858Z","lineItems":[' +
'{"Country":"India","Type":"test","Frequency":"test"},' +
'{"Country":"India","Type":"test","Frequency":"test"}],' +
'"invoiceNumber":1},' +
'{"totalPrice":11.5,"statementDate":"2011-10-04T16:58:54.858Z","lineItems":[' +
'{"Country":"India","Type":"test","Frequency":"test"},' +
'{"Country":"India","Type":"test","Frequency":"test"},' +
'{"Country":"India","Type":"test","Frequency":"test"}],"invoiceNumber":2}' +
']}';
test.startTest();
createQuoteAndLineItemLwcCtrl.submitOptRecord(jsonStr);
test.stopTest();
}
static testmethod void getStringValueFromMapTest(){
opportunity opt = NEW opportunity();
opt.Name='test';
opt.StageName='Prospecting';
opt.CloseDate=system.today();
INSERT opt;
Map<String, Object> dataMap = NEW Map<String, Object>();
dataMap.put('test1',opt);
test.startTest();
createQuoteAndLineItemLwcCtrl.getStringValueFromMap(dataMap, 'test');
test.stopTest();
}
static testmethod void getDateValueFromMapTest(){
opportunity opt1 = NEW opportunity();
opt1.Name='test';
opt1.StageName='Prospecting';
opt1.CloseDate=system.today();
INSERT opt1;
Map<String, Object> dataMap1 = NEW Map<String, Object>();
dataMap1.put('test1',opt1);
test.startTest();
createQuoteAndLineItemLwcCtrl.getDateValueFromMap(dataMap1, 'test');
test.stopTest();
}
static testmethod void saveQuoteLineItemTest(){
Id priceId = Test.getStandardPricebookId();
opportunity opt = NEW opportunity();
opt.Name='test';
opt.StageName='Prospecting';
opt.CloseDate=system.today();
INSERT opt;
Product2 p2= NEW Product2();
p2.Name= 'test';
p2.IsActive=TRUE;
p2.ProductCode='test';
INSERT p2;
Pricebook2 p1 = NEW Pricebook2();
p1.Name='test';
p1.IsActive=TRUE;
INSERT p1;
PricebookEntry priceBkEntry = NEW PricebookEntry();
priceBkEntry.UseStandardPrice=TRUE;
priceBkEntry.Pricebook2Id= priceId;
//priceBkEntry.Pricebook2Id=p1.Id;
priceBkEntry.Product2Id=p2.id;
priceBkEntry.UnitPrice=40446;
priceBkEntry.UseStandardPrice=FALSE;
priceBkEntry.IsActive=TRUE;
NEW PricebookEntry (Product2Id=p2.id,Pricebook2Id=Test.getStandardPricebookId(),UnitPrice=40446, UseStandardPrice=TRUE, isActive=TRUE);
INSERT priceBkEntry;
Quote q2 = NEW Quote();
q2.Email='test@test.com';
q2.Name='test';
q2.OpportunityId=opt.Id;
//q2.Pricebook2Id=p1.Id;
q2.Pricebook2Id=priceId;
INSERT q2;
List<QuoteLineItem> qutItemList = NEW List<QuoteLineItem>();
QuoteLineItem qitm = NEW QuoteLineItem();
qitm.Description='test';
qitm.QuoteId=q2.Id;
qitm.PricebookEntryId=priceBkEntry.Id;
qitm.Quantity=1.0;
qitm.UnitPrice=1.0;
qitm.Product2Id=p2.Id;
INSERT qitm;
qutItemList.add(qitm);
test.startTest();
createQuoteAndLineItemLwcCtrl.saveQuoteLineItem(qutItemList);
test.stopTest();
}
}
Further post that would you like to learn in Salesforce
What is a quote line in salesforce?
Quote lines store information about products that a sales rep has quoted. With certain page layout and field-level security settings, some fields aren't visible or editable.
Can I create a quote in Salesforce without opportunity?
You can fast-track deals by creating quotes without first creating an opportunity, speeding up delivering quotes to their customers for review. Quotes must be related to an account when they're converted to orders.
How do I create a new quote in Salesforce lightning?
The easiest way to create a new quote in Salesforce Lightning is through the Opportunities tab. Follow these steps to get started: Open your chosen customer opportunity. Navigate to the Related section on the right of the screen, scroll down to Quotes, click on the dropdown and 'New Quote'.
Related Topics | You May Also Like
- My Udemy Course β



