0

I have a problem with one code's part.

Controller (a part):

public void click(){
    for(WrapperClass wrp : wrapperRecordList){          
          if(wrp.isSelected){
            OpportunityLineItem opp = new OpportunityLineItem();
            opp.Product2Id = wrp.productRecord.Id;
            opp.Quantity = wrp.quantity;
            opp.OpportunityId = prodId;
            try{
                opp.PricebookEntryId = [SELECT Id FROM PricebookEntry WHERE Pricebook2Id IN (SELECT Pricebook2Id FROM Opportunity WHERE Id =: prodId) AND Product2Id =: wrp.productRecord.Id LIMIT 1].Id;
                opp.TotalPrice = wrp.quantity * [SELECT UnitPrice FROM PricebookEntry WHERE Pricebook2Id IN (SELECT Pricebook2Id FROM Opportunity WHERE Id =: prodId) AND Product2Id =: wrp.productRecord.Id LIMIT 1].UnitPrice;                    
                insert opp;                
            }catch(Exception e) {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR, e.getMessage()));
            }     
        } 
     }
 }

Test (a part):

Account acc = new Account(Name ='Test acc');
    insert acc;
    Product2 prod = new Product2(Name = 'AutoBot', IsActive = true);
    insert prod;   
    PriceBook2 pb2 = new PriceBook2(Name = 'Test pb', IsActive = true);
    insert pb2;
    Opportunity opport = new Opportunity(Name = 'Test opp', CloseDate = date.today(), AccountId= acc.Id, Pricebook2Id = pb2.Id, StageName = 'Prospecting');
    insert opport;
    PricebookEntry standardpbe = new PricebookEntry(Pricebook2Id = test.getstandardpricebookid(), Product2Id = prod.Id, UnitPrice = 0.0, IsActive = true);
    insert standardpbe;
    PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb2.Id, Product2Id = prod.Id, UnitPrice = 10.0, IsActive = true);
    insert pbe;               

    OpportunityLineItem item = new OpportunityLineItem();
    item.OpportunityId = opport.Id;
    item.Product2Id = prod.Id;
    item.PricebookEntryId = pbe.Id;
    item.Quantity = quantity;
    item.TotalPrice = item.Quantity * pbe.UnitPrice;
    insert item;

    prController.click();

wrp.isSelected is from Wrapper Class:

public class WrapperClass {

    public Boolean isSelected{get;set;}
    public Product2 productRecord{get;set;}
    public Decimal quantity{get;set;}

    public WrapperClass(Product2 productRecord, Boolean isSelected, Decimal quantity) {    
        this.productRecord = productRecord;
        this.isSelected = isSelected;
        this.quantity = quantity;
    }
}

And Wrapper Class tests OK. But with this fragment is something wrong.enter image description here

Can u help me please?

1 Answer 1

0

In the test class,

you need to create an Object of WrapperClass and then pass the ass required parameters in the constructor.

use below code

WrapperClass wrap = new WrapperClass(prod, true, 10);

Now, assign this wrapper class object to the main class variable from the test class like below

prController.wrapperList = new List<WrapperClass>{wrap};

In the final step, call the controller method.

prController.click();
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .