How to Create Apex Test Class for Quote and Quote Line Item in salesforce | Create Quote and QuoteLineItem From Opportunity in Salesforce | Test class code coverage for Quote and QuoteLineItem Salesforce

1,284 views

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 →

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!

 

test class for quote line item in salesforce -- w3web.net

 

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]

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  1.  
  2.  public WITH sharing class createQuoteAndLineItemLwcCtrl {
  3.     public createQuoteAndLineItemLwcCtrl() {
  4.  
  5.     }
  6.  
  7.     @AuraEnabled(cacheable=TRUE)
  8.     public static List<Opportunity> getOpportunityList(Id recId){
  9.         List<Opportunity> optList = [SELECT Id, Name, Description FROM Opportunity WHERE Id=:recId];
  10.         RETURN optList;
  11.     }
  12.  
  13.      //START Quote
  14.  
  15.   @AuraEnabled
  16.   public static String  submitOptRecord(String jsonDataStr) {
  17.  
  18.       Map<String, Object> RESULT = NEW Map<String, Object>();
  19.       String DataRespoce ='';
  20.       try {
  21.           Map<String, Object> formDataMap = (Map<String, Object>)JSON.deserializeUntyped(jsonDataStr);
  22.  
  23.           Map<String, Object> OptDataMap = (Map<String, Object>)formDataMap.get('optDataFyObj');
  24.  
  25.          // Opportunity optObj = NEW Opportunity();
  26.           Quote optObj = NEW 	Quote();
  27.           optObj.Name = getStringValueFromMap(OptDataMap, 'Name');
  28.           optObj.OpportunityId = getStringValueFromMap(OptDataMap, 'OpportunityId');
  29.           optObj.Description = getStringValueFromMap(OptDataMap, 'Description');
  30.           optObj.Pricebook2Id = getStringValueFromMap(OptDataMap, 'Pricebook2Id');
  31.           List<DATABASE.SaveResult> insertResult = DATABASE.insert(NEW List<Quote>{optObj});
  32.  
  33.           DataRespoce =JSON.serialize(insertResult); 
  34.  
  35.  
  36.       }catch(Exception ex) {
  37.  
  38.           RESULT.put('status', 500);
  39.           RESULT.put('message', 'Exception ' + ex.getMessage() + ',line' + ex.getLineNumber());
  40.       }
  41.       RETURN DataRespoce;
  42.   }
  43.  
  44.  
  45.  
  46.  
  47.   public static String getStringValueFromMap(Map<String, Object> dataMap, String fieldName) {
  48.       String VALUE;
  49.       try {
  50.           IF(dataMap.containsKey(fieldName)) {
  51.               VALUE = String.valueOf(dataMap.get(fieldName));
  52.           }
  53.  
  54.           VALUE = String.isEmpty(VALUE) ? VALUE :  String.valueOf(VALUE);
  55.       } catch(Exception ex) {
  56.           System.debug('Exception getValueFromMap : '+ ex.getMessage() + ' line ' + ex.getLineNumber());
  57.       }
  58.  
  59.       RETURN VALUE;
  60.   }
  61.  
  62.  
  63.  
  64.  
  65.  
  66.   public static DATE getDateValueFromMap(Map<String, Object> dataMap, String fieldName) {
  67.       DATE VALUE;
  68.       try {
  69.           String str;
  70.           IF(dataMap.containsKey(fieldName)) {
  71.               str = String.valueOf(dataMap.get(fieldName));
  72.           }
  73.  
  74.           VALUE = String.isEmpty(str) ? VALUE :  DATE.valueOf(str);
  75.       } catch(Exception ex) {
  76.           System.debug('Exception getIntValueFromMap : '+ ex.getMessage() + ' line ' + ex.getLineNumber());
  77.       }
  78.  
  79.       RETURN VALUE;
  80.   }
  81.  
  82.   //START Quote Line Item
  83.  
  84.  
  85.  
  86.   @AuraEnabled
  87.   public static void saveQuoteLineItem(List<QuoteLineItem> quoteItemList){
  88.       try {           
  89.          INSERT quoteItemList;
  90.          System.debug('quoteItemList## ' + quoteItemList);
  91.       }
  92.       catch(Exception ex) {
  93.           System.debug('Exception getValueFromMap : '+ ex.getMessage() + ' line ' + ex.getLineNumber());
  94.       }
  95.  
  96.  
  97.   } 
  98.  
  99.  
  100. //END Quote Line Item
  101.  
  102.  
  103. }

 

Create Test Class Controller

Step 2:- Create Apex Class : createQuoteAndLineItemLwcCtrlTest.apxc

From Developer Console >> File >> New >> Apex Class

createQuoteAndLineItemLwcCtrlTest.apxc [Apex Class Controller]

  1.   @isTest
  2. public class createQuoteAndLineItemLwcCtrlTest {
  3.  
  4.     static testmethod void getOpportunityListTest(){
  5.         opportunity opt = NEW opportunity();
  6.         opt.Name='test';
  7.         opt.StageName='Prospecting';
  8.         opt.CloseDate=system.today();
  9.         INSERT opt;
  10.         test.startTest();
  11.         createQuoteAndLineItemLwcCtrl.getOpportunityList(opt.Id);
  12.         test.stopTest();    
  13.     }
  14.  
  15.     static testmethod void submitOptRecordTest(){
  16.        String jsonStr = 
  17.         '{"invoiceList":[' +
  18.         '{"totalPrice":5.5,"statementDate":"2011-10-04T16:58:54.858Z","lineItems":[' +
  19.             '{"Country":"India","Type":"test","Frequency":"test"},' +
  20.             '{"Country":"India","Type":"test","Frequency":"test"}],' +
  21.                 '"invoiceNumber":1},' +
  22.         '{"totalPrice":11.5,"statementDate":"2011-10-04T16:58:54.858Z","lineItems":[' +
  23.             '{"Country":"India","Type":"test","Frequency":"test"},' +
  24.             '{"Country":"India","Type":"test","Frequency":"test"},' +
  25.             '{"Country":"India","Type":"test","Frequency":"test"}],"invoiceNumber":2}' +
  26.         ']}';
  27.  
  28.         test.startTest();
  29.         createQuoteAndLineItemLwcCtrl.submitOptRecord(jsonStr);
  30.         test.stopTest();
  31.     }
  32.  
  33.  
  34.     static testmethod void getStringValueFromMapTest(){
  35.         opportunity opt = NEW opportunity();
  36.         opt.Name='test';
  37.         opt.StageName='Prospecting';
  38.         opt.CloseDate=system.today();
  39.         INSERT opt;
  40.  
  41.         Map<String, Object> dataMap = NEW Map<String, Object>();
  42.         dataMap.put('test1',opt);
  43.  
  44.         test.startTest();
  45.         createQuoteAndLineItemLwcCtrl.getStringValueFromMap(dataMap, 'test');
  46.         test.stopTest();
  47.     }
  48.  
  49.  
  50.     static testmethod void getDateValueFromMapTest(){
  51.         opportunity opt1 = NEW opportunity();
  52.         opt1.Name='test';
  53.         opt1.StageName='Prospecting';
  54.         opt1.CloseDate=system.today();
  55.         INSERT opt1;
  56.  
  57.         Map<String, Object> dataMap1 = NEW Map<String, Object>();
  58.         dataMap1.put('test1',opt1);
  59.        test.startTest();
  60.          createQuoteAndLineItemLwcCtrl.getDateValueFromMap(dataMap1, 'test');
  61.        test.stopTest(); 
  62.     }
  63.  
  64.  
  65.     static testmethod void saveQuoteLineItemTest(){
  66.        Id priceId = Test.getStandardPricebookId();
  67.  
  68.         opportunity opt = NEW opportunity();
  69.         opt.Name='test';
  70.         opt.StageName='Prospecting';
  71.         opt.CloseDate=system.today();
  72.         INSERT opt;
  73.  
  74.         Product2 p2= NEW Product2();
  75.         p2.Name= 'test';
  76.         p2.IsActive=TRUE;
  77.         p2.ProductCode='test';       
  78.         INSERT p2;
  79.  
  80.         Pricebook2 p1 = NEW Pricebook2();
  81.         p1.Name='test';
  82.         p1.IsActive=TRUE;         
  83.         INSERT p1; 
  84.  
  85.         PricebookEntry priceBkEntry = NEW PricebookEntry();
  86.         priceBkEntry.UseStandardPrice=TRUE;
  87.         priceBkEntry.Pricebook2Id= priceId;
  88.         //priceBkEntry.Pricebook2Id=p1.Id;        
  89.         priceBkEntry.Product2Id=p2.id;
  90.         priceBkEntry.UnitPrice=40446;
  91.         priceBkEntry.UseStandardPrice=FALSE;
  92.         priceBkEntry.IsActive=TRUE;
  93.         NEW PricebookEntry (Product2Id=p2.id,Pricebook2Id=Test.getStandardPricebookId(),UnitPrice=40446, UseStandardPrice=TRUE, isActive=TRUE);
  94.         INSERT priceBkEntry;
  95.  
  96.  
  97.  
  98.         Quote q2 = NEW Quote();
  99.         q2.Email='test@test.com'; 
  100.         q2.Name='test'; 
  101.         q2.OpportunityId=opt.Id;     
  102.         //q2.Pricebook2Id=p1.Id;
  103.         q2.Pricebook2Id=priceId; 
  104.         INSERT q2;
  105.  
  106.  
  107.  
  108.         List<QuoteLineItem> qutItemList = NEW List<QuoteLineItem>();
  109.         QuoteLineItem qitm = NEW QuoteLineItem();        
  110.         qitm.Description='test';
  111.         qitm.QuoteId=q2.Id;
  112.         qitm.PricebookEntryId=priceBkEntry.Id;
  113.         qitm.Quantity=1.0;
  114.         qitm.UnitPrice=1.0;
  115.         qitm.Product2Id=p2.Id;
  116.         INSERT qitm;
  117.         qutItemList.add(qitm);
  118.  
  119.         test.startTest();
  120.         createQuoteAndLineItemLwcCtrl.saveQuoteLineItem(qutItemList);
  121.         test.stopTest();
  122.     }
  123.  
  124. }

 

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

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

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

Our Free Courses →

👉 Get Free Course →

📌 Salesforce Administrators

📌 Salesforce Lightning Flow Builder

📌 Salesforce Record Trigger Flow Builder

👉 Get Free Course →

📌 Aura Lightning Framework

📌 Lightning Web Component (LWC)

📌 Rest APIs Integration



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment