+ Start a Discussion
ckellieckellie 

Calling Apex Code in Custom Button with Related Record

I am needing to automatically create a quote record and attach it to a related quotelineitem. The steps the user will do for the automation is the following:

 

1. User will go to the quote record.

2. In the quote line item related list, the user will check the box next to the chosen quote line item.
3. User will click "New Quote" button in the quote line item related list section of the original quote.
4. The new quote will be connected to the chosen quote line item through the lookup field "Base Product."

 

Below is the apex class that creates the quot, but I cannot connect the quote to the chosen quote line item. Below is my apex class:

 

public class displayproductoptionsv6{

    public QuoteLineItem qlit;
    public String getquotelineitem(){return null;}
    public String getproduct2(){return null;}
    public String pname {get;set;}
    public Id o {get;set;}
    public List<Quote> q {get;set;}
    public List<cProduct> pList {get; set;}
    public List<cProduct> p2List {get; set;}
    public List<PriceBookEntry> spList {get; set;}
    public List<cProduct> cpsel{get; set;}
    public List<string> selectedproductid = new list<string>();
    ApexPages.StandardController controller;
    public displayproductoptionsv6(ApexPages.StandardController c){
    
        qlit = [select id, Quote.name, pricebookentryid, pricebookentry.product2id, Quote.opportunityid from QuoteLineItem where id =: ApexPages.currentpage().getParameters().get('qli')];
    system.debug('*****TEST-1**********'+qlit.id);
    
         q = [select id from Quote where id =: ApexPages.currentpage().getParameters().get('quoteid')];
    List<QuoteLineItem> qbase = [select id, quote.name, quantity, quote.Opportunity.Name, Pricebookentry.Product2.name, Pricebookentry.Pricebook2id, 
                                currencyisocode, Product_Base_Option__c from QuoteLineItem where id =: qlit.id];
    
    for(QuoteLineItem ql :qbase){
        pname = qbase[0].Pricebookentry.Product2.name;
    } 

 pList = new List<cProduct>();
    List<Pricebookentry> prbase  = [select id, name, Product2.Product_Base_Option__c,product2.productcode,
                                    unitprice, product2.description, pricebook2id, product2.mfg_cost_us__c from 
                                   Pricebookentry where pricebook2id =: qbase[0].pricebookentry.pricebook2id
                                   and product2.Product_Base_Option__c like: '%Option%'
                                   and product2.Product_Base_Option__c like: '%'+qbase[0].Product_Base_Option__c+'%'order by product2.name asc];

            for(Pricebookentry pbase : prbase){
                
                cProduct c1 = new cProduct(pbase);
                c1.selected = false;
                plist.add(c1);
                }
        system.debug('*****SFDC-TEST-1**********'+pList);
         if(Test.isRunningTest())
         { getquotelineitem();
            getproduct2();

             }
   }             

    public List<cProduct> getresults()
    { 
      System.debug('$$$$$$$$'+pList);
      return pList; 
    }
  
    public pageReference processSelected(){

        List<Pricebookentry> selectedProduct = new List<Pricebookentry>();
        List<Pricebookentry> base = [select id from pricebookentry where id =: qlit.pricebookentryid];
       System.debug('%%%%%%%%%%%%'+getresults());
       System.debug('%%%%%%%%%%%%:::'+ApexPages.currentPage().getParameters().get('id'));

        for(cProduct cCon : getresults()) {

        System.debug('#####'+cCon.selected);
            if(cCon.selected == true) {
                selectedProduct.add(cCon.con);         
                 System.debug('%%%%%%%%%%%%'+selectedProduct);    
                 System.debug('%%%%%%%%%%%%'+selectedProduct.size());    
            }
        }

    cpsel = new List<cProduct>();    
  if(test.isRunningTest()){
   getresults();
   
    }          
        system.debug('*****SFDC-TEST-3**********'+selectedProduct);

       System.debug('These are the selected Products...');
       System.debug('##########SelectedProduct:'+SelectedProduct);

        for(PricebookEntry cCon : SelectedProduct ) {
            system.debug('#####cCon'+cCon);
            selectedproductid.add(cCon.id);
        }
                system.debug('######********########'+selectedproductid);
                system.debug('######********########'+selectedproductid.size());

        for(PricebookEntry cCon : base) {
            system.debug('#####cCon'+cCon);
            selectedproductid.add(cCon.id);
        }

return null;
}

    public List<PricebookEntry> getchosen()
    { 
      System.debug('$$$$$$$$'+splist);


     splist = [select id, PriceBookEntry.Product2.name, product2.mfg_cost_us__c,
                                       product2.quantity__c, product2.item__c, unitprice from Pricebookentry
                                       where id in: selectedproductid]; 
      return splist; 

    }
    
    /* Wrapper class to contain product record and a boolean flag */
public class cProduct{
public Pricebookentry con {get; set;}
public Pricebookentry scon {get; set;}
public Boolean selected {get; set;}

public cProduct (Pricebookentry c)
{
     con = c;
     selected = false;
      }
}

/* end of Wrapper class */
public PageReference Save() {

      List<quotelineitem> qli = [select id, pricebookentry.product2.name, quote.pricebook2id, 
          quote.Opportunity.SFDC_for_Quote__c, quote.Opportunityid from quotelineitem where id =:qlit.id];
      
      id cqrt = [select id from recordtype where name = 'AutoConfig Quote'].id;
      
      Quote qt = new Quote();
          qt.Opportunityid = qli[0].quote.Opportunityid;
          qt.pricebook2id = qli[0].quote.pricebook2id;
          qt.name = qli[0].pricebookentry.product2.name + ' - ' + qli[0].quote.Opportunity.SFDC_for_Quote__c + ' - Configuration';
          qt.Base_Product__c = qli[0].id;
          qt.recordtypeid = cqrt;
      
      database.insert(qt);
      
   List<Pricebookentry> pbe = [select id, PriceBookEntry.Product2.name, product2.mfg_cost_us__c,
                                       product2.quantity__c, product2.item__c, unitprice from Pricebookentry
                                       where id =: qli[0].id]; 
         
    List<QuoteLineItem> ql = new list<QuoteLineItem>();
    
    for(QuoteLineItem qu : ql){
        
        qu.pricebookentryid = pbe[0].product2id;
        qu.quoteid = qt.id;
        qu.cost__c =splist[0].product2.mfg_cost_us__c;
        qu.quantity = splist[0].product2.quantity__c;
        qu.item__c = splist[0].product2.item__c; 
        qu.unitprice = splist[0].unitprice;
        
     insert qu;
    }
    

    PageReference pageRef = new PageReference('/' + qlit.id);
    
     return pageref;
    }
   public PageReference CancelPage() {
           
        return (new ApexPages.StandardController(qlit)).view();
    }


}

 

Best Answer chosen by Admin (Salesforce Developers) 
Michael_TorchedloMichael_Torchedlo

I am unclear on the problem.  Is the issue

 

A: Your code inserts a quote with line items, but the quote is not related to the right line item on the original quote.

 

or

 

B: Your code inserts a quote that doesn't have any line items?

 

It seems to me like it could be B.  The save method that you posted inserts a quote but that quote will not have any line items.

 

    List<QuoteLineItem> ql = new list<QuoteLineItem>();   //ql is an empty list
   
    for(QuoteLineItem qu : ql){

       //you are inserting QuoteLineItems here but since ql is empty, nothing inside the for loop executes

 

   }

 

public PageReference Save() {

      List<quotelineitem> qli = [select id, pricebookentry.product2.name, quote.pricebook2id, 
          quote.Opportunity.SFDC_for_Quote__c, quote.Opportunityid from quotelineitem where id =:qlit.id];
      
      id cqrt = [select id from recordtype where name = 'AutoConfig Quote'].id;
      
      Quote qt = new Quote();
          qt.Opportunityid = qli[0].quote.Opportunityid;
          qt.pricebook2id = qli[0].quote.pricebook2id;
          qt.name = qli[0].pricebookentry.product2.name + ' - ' + qli[0].quote.Opportunity.SFDC_for_Quote__c + ' - Configuration';
          qt.Base_Product__c = qli[0].id;
          qt.recordtypeid = cqrt;
      
      database.insert(qt);
      
   List<Pricebookentry> pbe = [select id, PriceBookEntry.Product2.name, product2.mfg_cost_us__c,
                                       product2.quantity__c, product2.item__c, unitprice from Pricebookentry
                                       where id =: qli[0].id]; 
         
    List<QuoteLineItem> ql = new list<QuoteLineItem>();
    
    for(QuoteLineItem qu : ql){
        
        qu.pricebookentryid = pbe[0].product2id;
        qu.quoteid = qt.id;
        qu.cost__c =splist[0].product2.mfg_cost_us__c;
        qu.quantity = splist[0].product2.quantity__c;
        qu.item__c = splist[0].product2.item__c; 
        qu.unitprice = splist[0].unitprice;
        
     insert qu;
    }

 

All Answers

Michael_TorchedloMichael_Torchedlo

I am unclear on the problem.  Is the issue

 

A: Your code inserts a quote with line items, but the quote is not related to the right line item on the original quote.

 

or

 

B: Your code inserts a quote that doesn't have any line items?

 

It seems to me like it could be B.  The save method that you posted inserts a quote but that quote will not have any line items.

 

    List<QuoteLineItem> ql = new list<QuoteLineItem>();   //ql is an empty list
   
    for(QuoteLineItem qu : ql){

       //you are inserting QuoteLineItems here but since ql is empty, nothing inside the for loop executes

 

   }

 

public PageReference Save() {

      List<quotelineitem> qli = [select id, pricebookentry.product2.name, quote.pricebook2id, 
          quote.Opportunity.SFDC_for_Quote__c, quote.Opportunityid from quotelineitem where id =:qlit.id];
      
      id cqrt = [select id from recordtype where name = 'AutoConfig Quote'].id;
      
      Quote qt = new Quote();
          qt.Opportunityid = qli[0].quote.Opportunityid;
          qt.pricebook2id = qli[0].quote.pricebook2id;
          qt.name = qli[0].pricebookentry.product2.name + ' - ' + qli[0].quote.Opportunity.SFDC_for_Quote__c + ' - Configuration';
          qt.Base_Product__c = qli[0].id;
          qt.recordtypeid = cqrt;
      
      database.insert(qt);
      
   List<Pricebookentry> pbe = [select id, PriceBookEntry.Product2.name, product2.mfg_cost_us__c,
                                       product2.quantity__c, product2.item__c, unitprice from Pricebookentry
                                       where id =: qli[0].id]; 
         
    List<QuoteLineItem> ql = new list<QuoteLineItem>();
    
    for(QuoteLineItem qu : ql){
        
        qu.pricebookentryid = pbe[0].product2id;
        qu.quoteid = qt.id;
        qu.cost__c =splist[0].product2.mfg_cost_us__c;
        qu.quantity = splist[0].product2.quantity__c;
        qu.item__c = splist[0].product2.item__c; 
        qu.unitprice = splist[0].unitprice;
        
     insert qu;
    }

 

This was selected as the best answer
ckellieckellie

 You were correct that explanation B is the problem. I understand the error as you mentioned and should be able to resolve the problem.

 

Thanks