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
Shruthi NarsiShruthi Narsi 

Test Class Failed!!!!!

I have written the below code and the test class is failed Can someone help me with the code. I have to add products on quoteline items

@isTest
public class testclass {
    static testMethod void testUpdateAccount() {
        Quotes__c a = new Quotes__c(Name='testAccount',OpportunityId__c= 'testAccount');
        insert a; 
        
        Product2__c prod = new Product2__c(Name = 'Laptop X200', 
                                           Family__c = 'None');
        insert prod;
        
        Id PricebookId = Test.getStandardPricebookId();
        
        PricebookEntry__c StandardPrice = new PricebookEntry__c(
            Pricebook2Id__c = PricebookId, Product2Id__c = prod.Id,
            UnitPrice__c = 10000, IsActive__c = true);
        insert StandardPrice;
        
        
        PricebookEntry__c customPB = new PricebookEntry__c(Name='Custom Pricebook', IsActive__c=true);
        insert customPB;
        
        PricebookEntry__c customPrice = new  PricebookEntry__c(
            Pricebook2Id__c = customPB.Id, Product2Id__c = prod.Id,
            UnitPrice__c = 12000, IsActive__c = true);
        insert customPrice;
        
        
        QuoteLineitem__c quote = new QuoteLineitem__c (
                                                          Product2Id__c='G=10000',
                                                          Discount__c= 20,
                                                          Line_Item_Description__c='Test'
                                                          );
        
        insert Quote; 
        
        System.assertEquals('test', a.Name);
        
    }
}

Controller Class

public class ProductSearchPopupController {
   
    public String query {get; set;}
    public List<PricebookEntry__c> products {get; set;}
    public List<wrapProduct> wrapProductList {get; set;}
    public List<PricebookEntry__c> selectedProduct{get;set;}
    public List<QuoteLineitem__c> quoteLineList{get;set;}
    public List<wrapProduct> selectedWrapperList{get;set;}
    public Boolean normalList{get;set;}
    public Boolean selectedList{get;set;}
    public Boolean block{get;set;}
    public Boolean block1{get;set;}
    public Boolean block2{get;set;}
    public String SalesPrice {get; set;}
    public integer Discount {get; set;}
    public String Quantity {get; set;}
    public String ServiceDate {get; set;}
    Id recordId;
    
    public ProductSearchPopupController(ApexPages.StandardController controller){
        recordId = controller.getId();
        system.debug('recordId '+recordId);
        wrapProductList = new List<wrapProduct>();
        selectedWrapperList = new List<wrapProduct>();
        normalList = true;
        selectedList = false;
        block = true;
        block1 = false;
        block2 = false;
    }
    
    public PageReference runQuery(){
        if(query == null || query == ''){
            system.debug('query '+query);
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info,'Please enter Product to search for'));
            
            return null;
        }
        system.debug('query '+query);
        List<List<PricebookEntry__c>> searchResults=[FIND :query IN ALL FIELDS RETURNING PricebookEntry__c (id, Name, ProductCode__c, Product2Id__r.Product_Description__c,UnitPrice__c, UseStandardPrice__c)];
        if(searchResults[0]!=null){
            for(PricebookEntry__c a: searchResults[0]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapProductList.add(new wrapProduct(a));
                block = true;
                block1 = true;
                block2 = false;
            }
        }
        return null;
    }
    public PageReference ProceedWithSelectedToNextPage(){
        selectedWrapperList = new List<wrapProduct>();
        normalList = false;
        selectedList = true;
        for(wrapProduct selectedWrapObj: wrapProductList){
            system.debug('selectedWrapObj.selected  ---------'+selectedWrapObj.selected);
            if(selectedWrapObj.selected == true)
                selectedWrapperList.add(selectedWrapObj);
        }
        system.debug('selectedWrapperList size ---------'+selectedWrapperList.size());
        PageReference pageRef = new PageReference('/apex/AccountOpportunityTwoPage');
        pageRef.setRedirect(false);
        return pageRef;
    }
    public void processSelected() {
        selectedProduct = new List<PricebookEntry__c>();
        for(wrapProduct wrapProductObj : wrapProductList) {
            if(wrapProductObj.selected == true) {
                selectedProduct.add(wrapProductObj.acc);
                block = false;
                block1 = false;
                block2 = true;
                
            }
        }
    }
    
    public class wrapProduct{
        public PricebookEntry__c acc {get;set;}
        public Boolean selected {get;set;}
        public wrapProduct(PricebookEntry__c p) {
            this.acc = p;
            this.selected = false;
        }
        
    }
    
    public pagereference saveproduct(){ 
        List<QuoteLineitem__c> quoteLineList = new  List<QuoteLineitem__c>();
        if(!selectedProduct.isEmpty()){
            for(PricebookEntry__c sp:selectedProduct){
                system.debug('sp '+sp);
                QuoteLineitem__c qli = new QuoteLineitem__c();
                qli.QuotesId__c = recordId;
                qli.ListPrice__c = sp.UnitPrice__c;
                qli.UnitPrice__c = sp.UnitPrice__c;
                qli.Product2Id__c = sp.Product2Id__c;    
                if(Discount!=0 || Discount!=null){
                    qli.Discount__c = Discount;
                }
                quoteLineList.add(qli);
            }
            
            
            if(quoteLineList.size()>0){
                insert quoteLineList;
                PageReference pageRef = new PageReference('https://proseraa.lightning.force.com/lightning/r/Quotes__c/'+recordId+'/view');
                pageRef.setRedirect(true);
                return pageRef;
            }
        }
        return null;
    }
   
}
Anthony McDougaldAnthony McDougald
Good Morning Shruthi,
Hope that your day is off to an amazing start. After analyzing your test class, I see that your System Assert call is incorrect. Please try the below test class and let us know how it turns out. Hope this helps and may God bless you abundantly.
@isTest
public class testclass {
    static testMethod void testUpdateAccount() {
        Quotes__c a = new Quotes__c(Name='testAccount',OpportunityId__c= 'testAccount');
        insert a; 
        
        Product2__c prod = new Product2__c(Name = 'Laptop X200', 
                                           Family__c = 'None');
        insert prod;
        
        Id PricebookId = Test.getStandardPricebookId();
        
        PricebookEntry__c StandardPrice = new PricebookEntry__c(
            Pricebook2Id__c = PricebookId, Product2Id__c = prod.Id,
            UnitPrice__c = 10000, IsActive__c = true);
        insert StandardPrice;
        
        
        PricebookEntry__c customPB = new PricebookEntry__c(Name='Custom Pricebook', IsActive__c=true);
        insert customPB;
        
        PricebookEntry__c customPrice = new  PricebookEntry__c(
            Pricebook2Id__c = customPB.Id, Product2Id__c = prod.Id,
            UnitPrice__c = 12000, IsActive__c = true);
        insert customPrice;
        
        
        QuoteLineitem__c quote = new QuoteLineitem__c (
                                                          Product2Id__c='G=10000',
                                                          Discount__c= 20,
                                                          Line_Item_Description__c='Test'
                                                          );
        
        insert Quote; 
        
        System.assertEquals('testAccount', a.Name);
        
    }
}

Best Regards,
Anthony McDougald​​​​​​​
Shruthi NarsiShruthi Narsi
User-added image

System.StringException: Invalid id: testAccount
Shruthi NarsiShruthi Narsi
I pasted the wrong code. Below is the code

@isTest
public class TestUpdateAccountIfOpp {
    static testMethod void testUpdateAccount() {
        Account a = new Account(Name='testAccount', 
                                Email_Domain__c='test', 
                                Company_Short__c='tst', 
                                Product__c='Semantria', 
                                Industry='Other', 
                                Type='Prospect');
        insert a; 
        
        Product2__c prod = new Product2__c(Name = 'Laptop X200', 
                                     Family__c = 'Hardware');
        insert prod;
        
        Id pricebookId__c = Test.getStandardPricebookId();
        
        PricebookEntry__c standardPrice = new PricebookEntry(
            Pricebook2Id__c = pricebookId__c, Product2Id__c = prod.Id,
            UnitPrice__c = 10000, IsActive__c = true);
        insert standardPrice;
        
        
        Pricebook2__c customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
        insert customPB;
        
        PricebookEntry_-c customPrice = new PricebookEntry(
            Pricebook2Id__c = customPB.Id, Product2Id__c = prod.Id,
            UnitPrice__c = 12000, IsActive__c = true);
        insert customPrice;
        
        
        Opportunity__c opp = new Opportunity(Name='Test', 
                                          Account = a, 
                                          Product__c='Semantria',
                                          NextStep='test',
                                          Marketing_Channel__c='Email',
                                          PriceBook2__c = customPB,
                                          Product2__c = prod,
                                          AccountId=a.Id, 
                                          StageName__c='Closed Won - One Time', 
                                          Probability__c=100);
       
       insert opp; 
        
       System.assertEquals('Customer', a.Type);
    }
}

I am getting many errors

User-added imageUser-added image

User-added image
Anthony McDougaldAnthony McDougald
Good Afternoon Shruthi,
It would seem that a lot of your declared Sobjects and their fields are being declared as custom objects/fields with the __c notation. Can you please confirm that these are actually custom objects or are these the standard objecta and fields on your Salesforce org? We've edited some of your code for you to test and report back so we can do further troubleshooting. Thank you and hope this helps.
@isTest
public class TestUpdateAccountIfOpp {
    static testMethod void testUpdateAccount() {
        Account a = new Account(Name='testAccount', 
                                //Email_Domain__c='test', 
                                //Company_Short__c='tst', 
                                //Product__c='Semantria', 
                                Industry='Other', 
                                Type='Prospect');
        insert a; 
        
        Product2__c prod = new Product2__c(Name = 'Laptop X200', 
                                     Family__c = 'Hardware');
        insert prod;
        
        //Id pricebookId__c = Test.getStandardPricebookId();
        
        PricebookEntry standardPrice = new PricebookEntry(
            //Pricebook2Id__c = pricebookId__c, 
			//Product2Id__c = prod.Id,
            //UnitPrice__c = 10000, 
			//IsActive__c = true);
        insert standardPrice;
        
        
        Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
        insert customPB;
        
        PricebookEntry customPrice = new PricebookEntry(
            Pricebook2Id = customPB.Id, 
			Product2Id = prod.Id,
            UnitPrice = 12000, 
			IsActive = true);
        insert customPrice;
        
        
        Opportunity opp = new Opportunity(Name='Test', 
                                          Account = a, 
                                          Product__c='Semantria',
                                          NextStep='test',
                                          Marketing_Channel__c='Email',
                                          PriceBook2__c = customPB,
                                          Product2__c = prod,
                                          AccountId=a.Id, 
                                          StageName__c='Closed Won - One Time', 
                                          Probability__c=100);
       
       insert opp; 
        
       System.assertEquals('Customer', a.Type);
    }
}


Best Regards,
Anthony McDougald
Shruthi NarsiShruthi Narsi
Hi Anthony,

Thank you so much for your response. :)

I am currently working on force.com project to add custom products on quoteline items. No I need your help to write the test class for the same.

Below is the controller which I have used

public class ProductSearchPopupController {
   
    public String query {get; set;}
    public List<PricebookEntry__c> products {get; set;}
    public List<wrapProduct> wrapProductList {get; set;}
    public List<PricebookEntry__c> selectedProduct{get;set;}
    public List<QuoteLineitem__c> quoteLineList{get;set;}
    public List<wrapProduct> selectedWrapperList{get;set;}
    public Boolean normalList{get;set;}
    public Boolean selectedList{get;set;}
    public Boolean block{get;set;}
    public Boolean block1{get;set;}
    public Boolean block2{get;set;}
    public String SalesPrice {get; set;}
    public integer Discount {get; set;}
    public String Quantity {get; set;}
    public String ServiceDate {get; set;}
    Id recordId;
    
    public ProductSearchPopupController(ApexPages.StandardController controller){
        recordId = controller.getId();
        system.debug('recordId '+recordId);
        wrapProductList = new List<wrapProduct>();
        selectedWrapperList = new List<wrapProduct>();
        normalList = true;
        selectedList = false;
        block = true;
        block1 = false;
        block2 = false;
    }
    
    public PageReference runQuery(){
        if(query == null || query == ''){
            system.debug('query '+query);
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info,'Please enter Product to search for'));
            
            return null;
        }
        system.debug('query '+query);
        List<List<PricebookEntry__c>> searchResults=[FIND :query IN ALL FIELDS RETURNING PricebookEntry__c (id, Name, ProductCode__c, Product2Id__r.Product_Description__c,UnitPrice__c, UseStandardPrice__c)];
        if(searchResults[0]!=null){
            for(PricebookEntry__c a: searchResults[0]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapProductList.add(new wrapProduct(a));
                block = true;
                block1 = true;
                block2 = false;
            }
        }
        return null;
    }
    public PageReference ProceedWithSelectedToNextPage(){
        selectedWrapperList = new List<wrapProduct>();
        normalList = false;
        selectedList = true;
        for(wrapProduct selectedWrapObj: wrapProductList){
            system.debug('selectedWrapObj.selected  ---------'+selectedWrapObj.selected);
            if(selectedWrapObj.selected == true)
                selectedWrapperList.add(selectedWrapObj);
        }
        system.debug('selectedWrapperList size ---------'+selectedWrapperList.size());
        PageReference pageRef = new PageReference('/apex/AccountOpportunityTwoPage');
        pageRef.setRedirect(false);
        return pageRef;
    }
    public void processSelected() {
        selectedProduct = new List<PricebookEntry__c>();
        for(wrapProduct wrapProductObj : wrapProductList) {
            if(wrapProductObj.selected == true) {
                selectedProduct.add(wrapProductObj.acc);
                block = false;
                block1 = false;
                block2 = true;
                
            }
        }
    }
    
    public class wrapProduct{
        public PricebookEntry__c acc {get;set;}
        public Boolean selected {get;set;}
        public wrapProduct(PricebookEntry__c p) {
            this.acc = p;
            this.selected = false;
        }
        
    }
    
    public pagereference saveproduct(){ 
        List<QuoteLineitem__c> quoteLineList = new  List<QuoteLineitem__c>();
        if(!selectedProduct.isEmpty()){
            for(PricebookEntry__c sp:selectedProduct){
                system.debug('sp '+sp);
                QuoteLineitem__c qli = new QuoteLineitem__c();
                qli.QuotesId__c = recordId;
                qli.ListPrice__c = sp.UnitPrice__c;
                qli.UnitPrice__c = sp.UnitPrice__c;
                qli.Product2Id__c = sp.Product2Id__c;    
                if(Discount!=0 || Discount!=null){
                    qli.Discount__c = Discount;
                }
                quoteLineList.add(qli);
            }

            if(quoteLineList.size()>0){
                insert quoteLineList;
                PageReference pageRef = new PageReference('https://proseraa.lightning.force.com/lightning/r/Quotes__c/'+recordId+'/view');
                pageRef.setRedirect(true);
                return pageRef;
            }
        }
        return null;
    }
   
}

VF Code

<apex:page standardController="Quotes__c"  extensions="ProductSearchPopupController" showHeader="false" sidebar="false" lightningStylesheets="true">
    <apex:form id="form">
       
        <apex:pageMessages ></apex:pageMessages>
        <div style="width 100%">
            <apex:pageBlock title="Add Products" id="block" rendered="{!block}"> 
                <centre>
                    <apex:pageBlockSection columns="1" id="section"> 
                        Enter the product to be added and select Go<br/>
                        <apex:inputText value="{!query}" id="query"/> 
                    </apex:pageBlockSection> 
                </centre>
                    <apex:commandButton value="Go" action="{!runQuery}"/>
                    <apex:commandButton value="cancel" action="{!cancel}"/>
            </apex:pageBlock>
            <apex:pageBlock id="block1" rendered="{!block1}" >
                <apex:pageBlockSection columns="1">
                    <apex:pageBlockTable value="{!wrapProductList}" var="accWrap">
                        <apex:column >
                            <apex:facet name="header">
                                <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                            </apex:facet>
                            <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
                            
                        </apex:column>
                        <apex:column headerValue="Name">
                            <apex:outputLink value="#">{!accWrap.acc.Name}</apex:outputLink>       
                        </apex:column>
                        <apex:column headerValue="Product Code">
                            <apex:outputText value="{!accWrap.acc.ProductCode__c}"  />       
                        </apex:column>
                        <apex:column headerValue="Product Description">
                            <apex:outputText value="{!accWrap.acc.Product2Id__r.Product_Description__c}"  />       
                        </apex:column>
                        <apex:column headerValue="Unit Price">
                            <apex:outputText value="{!accWrap.acc.UnitPrice__c}"  />  
                        </apex:column>
                        
                        <apex:column headerValue="Standard Price">
                            <apex:outputText value="{!accWrap.acc.UseStandardPrice__c}"  />  
                        </apex:column>
                    </apex:pageBlockTable>  
                    
                    
                    <apex:commandButton value="Next" action="{!processSelected}"/>  
                </apex:pageBlockSection>
            </apex:pageBlock>
        </div>
        <div>

Test class
   
    

      

    
            <apex:pageBlock id="block2" rendered="{!block2}" >
                <apex:pageblockSection title="All Products" collapsible="false" columns="3">
                    <apex:pageBlockTable value="{!selectedProduct}" var="c" id="table2" title="Selected Products">
                        <apex:column value="{!c.Name}" headerValue="Product Name"/>
                        <apex:column headerValue="Sales Price">
                            <apex:inputText value="{!c.UnitPrice__c}" id="UnitPrice" /> 
                        </apex:column>
                        <apex:column headerValue="Discount">
                            <apex:inputText value="{!discount}" id="discount" /> 
                        </apex:column>
                        <apex:column headerValue="Quantity">
                            <apex:inputText value="{!quantity}" id="quantity" /> 
                        </apex:column>
                    </apex:pageBlockTable>  
                </apex:pageblockSection>
                <apex:commandButton action="{!saveproduct}" value="Save"/>
                 
                <apex:commandButton value="Cancel" oncomplete="doRedirect();"/>                
        
          
                
           
            </apex:pageBlock>
            
        </div>
    </apex:form>
</apex:page>