0

here is my vf page where i am trying to connect two object field's i.e. Pricebookentry & opportunityLineItem and insert into opportunityLineItem. for this i am also getting id of the opportunity but i don't know how to pass that id in these records so that i'll be able to insert these records in opportunityLineItem Related list that will be visible in opportunity. Actually here i am trying to make Add product stanadard functionality of opportunity using VF pages. here is the image enter image description here

here is my controller-------------

public class GEN_ProductSelectionController {



 ID cid = ApexPages.currentPage().getParameters().get('id'); //opportunity Id

 public PageReference Reset() {
        return null;
 }

 Public List<OpportunityLineItem> opportunityLineItemList{get;set;}
 public List<wrapOpportunityLineItem> wrapper{get;set;}
 public List<wrapProduct> wrapProductList {get; set;}
 public List<PricebookEntry> selectedProducts{get;set;}
 public List<OpportunityLineItem> closeProduct{get;set;}

 public String searchstring {get;set;}  

 public void search(){  
         wrapProductList = new List<wrapProduct>();
         string searchquery='select name,id,ProductCode,UnitPrice,product2.Type__c from PricebookEntry where name like \'%'+searchstring+'%\' Limit 20';  

         for(PricebookEntry a: Database.query(searchquery)) {
              wrapProductList.add(new wrapProduct(a));
         }
 }  

public GEN_ProductSelectionController(){
         wrapProductList = new List<wrapProduct>();

         for(PricebookEntry a: [select Id, Name,ProductCode,UnitPrice,product2.Type__c from PricebookEntry]) {
         wrapProductList.add(new wrapProduct(a));
         }
}

public class wrapProduct {
        public PricebookEntry pro {get; set;}
        public Boolean selected {get; set;}

        public wrapProduct(PricebookEntry p) {
            pro = p;
            selected = false;
        }
}

public class wrapOpportunityLineItem{

        public OpportunityLineItem oppLineItem{get;set;}
          public PricebookEntry selProd{get;set;}

        public wrapOpportunityLineItem(OpportunityLineItem oli, PricebookEntry sp)
        {  

           oppLineItem = oli;
           selProd = sp;
        }

}


public PageReference closeProduct() {
        selectedProducts = new List<PricebookEntry>();

        for(wrapProduct wrapProductObj : wrapProductList) {

             if(wrapProductObj.selected == true) {
              selectedProducts.add(wrapProductObj.pro);
             }
        }
        system.debug('selectedWrapperList size ---------'+selectedProducts.size());
        PageReference pagePrice = new PageReference('/apex/GEN_MultiEdit?id='+cid);
        opportunityLineItemList = [Select Quantity,UnitPrice,Monthly_Revenue__c,Contract_Start_Date__c,Full_Revenue_Date__c,Service_Start_Date__c,OpportunityId from OpportunityLineItem ];

           wrapper = new List<wrapOpportunityLineItem>();
        for(Integer i=0; i<selectedProducts.size();i++)
        {
           wrapper.add(new wrapOpportunityLineItem(opportunityLineItemList[i],selectedProducts[i]) );
        }


        pageprice.setRedirect(false);
        return pageprice; 

}

public void closeAccou(){
        selectedProducts = new List<PricebookEntry>();

        for(wrapProduct wrapProductObj : wrapProductList) {

           if(wrapProductObj.selected == true) {
               selectedProducts.add(wrapProductObj.pro);
           }
        }
          insert selectedProducts;


}



}
3
  • 1
    a couple of comment and hints: (1) One often needs to have in addition to wrapped lists, maps to the wrapped lists to allow you to xref between an id of some Sobject and the relevant wrapper object; (2) Draw out a schema of how the wrapper classes need to relate to each other (3) You're creating self-inflicted wounds by your variable naming conventions - I see a, p, and pro being used for a PriceBookEntry - Most of us consistently use pbe ; you have closeProduct - does that mean close as an action? or close as in near. Your lists are not all named as plurals
    – cropredy
    Feb 13, 2016 at 23:02
  • @crop1645 Excellent naming advice!
    – Adrian Larson
    Feb 14, 2016 at 0:03
  • Rat__ you can pass the id in wrapper list but. I can see you are passing queried OLI(In your prev question I already told you how to create new OLI). why? It is already OLI associated to Opportunity...
    – Ratan Paul
    Feb 14, 2016 at 2:50

1 Answer 1

1

So I believe you want to create new OLI then create instance of OLI with opportunity id

Like below..

It will create new OLI related to that opportunity

Id objCurrentOppId = ApexPages.currentPage().getParameters().get('id');
for(Integer i=0; i<selectedProducts.size(); i++)
{
    // Pass new OLI with opportunity id

wrapper.add(new wrapOpportunityLineItem(new OpportunityLineItem(OpportunityId = objCurrentOppId),
                                       selectedProducts[i]) );
}

Updates

First collect all OLI from wrapper list and then add into OLI list and then insert that list

List<OpportunityLineItem> lstOLI = new List<OpportunityLineItem>();
for(wrapOpportunityLineItem obj :wrapper)
{
  lstOLI.add(obj.oppLineItem);
}

insert lstOLI;
6
  • thanks but when i am trying to save the wrapper list i am getting error-- Error: GEN_ProductSelectionController Compile Error: DML requires SObject or SObject list type: List<GEN_ProductSelectionController.wrapOpportunityLineItem> at line 69 column 11 in this line public void save() { for(Integer i=0; i<wrapper.size(); i++) { upsert wrapper; }
    – Sharma_raj
    Feb 14, 2016 at 5:59
  • 1
    @RAT__ you can't perform DMl on wrapper list. You want to insert OLI correct?
    – Ratan Paul
    Feb 14, 2016 at 6:01
  • yes, i have merged pricebookEntry's product,Price with OpportunityLineItem's Quantity and few custom date fields now i want to add them in OLI which will be visible on opportunity inside oli related list. and sorry for naming conventions, i'll try to make habit to use proper naming conventions. thanks for ur valuable support
    – Sharma_raj
    Feb 14, 2016 at 6:12
  • @RAT__ YOu need to store wrapper OLI in list and insert that list check my updated ans.
    – Ratan Paul
    Feb 14, 2016 at 6:21
  • @RAT__ Pls don't say thanks. If you really think we helped you then pls upvote our ans or accept our ans as best that way you can thank us.
    – Ratan Paul
    Feb 14, 2016 at 6:44

You must log in to answer this question.

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