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
VPrakashVPrakash 

Adding products as opp line items to the opportunities-customize

Hi all,

 

Can we customize the process of adding products as opp line items to the opportunities using Apex and Visualforce? If so can you please provide me with some sample code or references.

 

 

Thanks in Advance 

sfdcfoxsfdcfox

You can go to Setup > App Setup > Customize > Opportunities > Opportunity Products > Buttons and Links to override the default pages. Making your own code is half the fun. You'll need the Visualforce Developer's Guide and Apex Code Developer's Guide. There's not any specific code I can point you to for the exact functionality you're looking for, but all the pieces you need are in the documentation.

nagalakshminagalakshmi

hi,

try this one. i think it is useful for u..

 

visual force page:

 

 

<apex:page controller="opp" showHeader="false" sidebar="false">

<apex:form >

<apex:outputPanel rendered="{!oppview}">

<apex:pageBlock >

<apex:pageBlockTable value="{!opprecs}" var="o">

<apex:column headerValue="Name">

<apex:commandLink value="{!o.name}" action="{!viewdetail}">

<apex:param name="vid" value="{!o.id}"/>

</apex:commandLink>

</apex:column>

</apex:pageBlockTable>

</apex:pageBlock>

</apex:outputPanel>

 

<apex:outputPanel rendered="{!oppeditview}">

<apex:pageBlock title="Oppertunity">

<center>

<apex:commandButton value="Save" action="{!save}"/>

<apex:commandButton value="Cancel" action="{!cancel}"/>

</center>

 

<apex:pageblockSection title="opportunity information">

<apex:inputField value="{!editopp.name}"/>

<apex:inputField value="{!editopp.accountid}"/>

<apex:inputField value="{!editopp.type}"/>

<apex:inputField value="{!editopp.leadsource}"/>

</apex:pageblockSection>

 

 

<apex:pageBlockSection title="Stage Information">

<apex:inputField value="{!editopp.stagename}"/>

<apex:inputField value="{!editopp.probability}"/>

<apex:inputField value="{!editopp.amount}"/>

<apex:inputField value="{!editopp.closedate}"/>

</apex:pageBlockSection>

<apex:pageblockSection title="Additional Information" >

 <apex:inputField value="{!editopp.nextstep}"/>

</apex:pageblockSection>

 

<apex:pageBlockSection title="Description">

 <apex:inputField value="{!editopp.description }"/>

</apex:pageBlockSection>

 

</apex:pageBlock>

 

<apex:pageBlock >

<apex:pageBlockTable value="{!prod}" var="p">

<apex:column headerValue="check">

<apex:inputCheckbox value="{!p.check1}" />

</apex:column>

<apex:column headerValue="Product Name" value="{!p.name}"/>

<apex:column headerValue="Product Code" value="{!p.prodcode}"/>

 

<apex:column headerValue="Standard Price" value="${!p.standardprice}"/>

<apex:column headerValue="Product Description" value="{!p.description}"/>

<apex:column headerValue="Product family" value="{!p.prodfamily}"/>

</apex:pageBlockTable>

</apex:pageBlock>

</apex:outputPanel>

</apex:form>

</apex:page>

 

 

 

controller:

 

public class opp
{
public id oppid{set;get;}
id s;
set<id> opset=new set<id>();
public boolean check{set;get;}
public boolean oppview{set;get;}
public boolean oppeditview{set;get;}
opportunity oo=new opportunity();
list<Opportunity > oplist=new list<Opportunity  >();
list<OpportunityLineItem > olist=new list<OpportunityLineItem >();
list<product2> plist=new list<product2>();
public class wrapperopp
{
public boolean check1{set;get;}
public boolean oppview{set;get;}
public string name{set;get;}
public string description{set;get;}
public string prodfamily{set;get;}
public string prodcode{set;get;}
public decimal standardprice{set;get;}
public id pid{set;get;}
public boolean oppeditview{set;get;}
}
list<wrapperopp> wlist=new list<wrapperopp>();
/*public void prodetails()
{

for(product2 p:[select id,name from product2 ])
{
wrapperopp op=new wrapperopp();
op.name=p.name;
if(opset.contains(p.id))
op.check1=true;
else
op.check1=false;
wlist.add(op);
}
}*/

set<id> oset=new set<id>();
list<wrapperopp> wrlist=new list<wrapperopp>();
opportunitylineitem ol=new opportunitylineitem ();
public opp()
{
oppeditview=false;
oppview=true;
details();
}
public void details()
{
for(Opportunity o:[select id,name from Opportunity])
{
oplist.add(o);
}
}

public list<Opportunity> getopprecs()
{
return oplist;
}
set<id> oset1=new set<id>();
list<opportunity> olist1=new list<opportunity>();
public void viewdetail()
{

oppeditview=true;
oppview=false;
s=apexpages.currentpage().getparameters().get('vid');
oo=[select name,accountid,type,leadsource,stagename,probability,amount,closedate,nextstep,description from opportunity where id =:s];
oppid=oo.id;
 for(OpportunityLineItem o:[select PricebookEntryId from OpportunityLineItem where opportunityid=:s])
 {
 oset1.add(o.pricebookEntryId);
 }
 for(pricebook2 o:[select id from pricebook2 where isactive=:true  ])
      {
      oset.add(o.id);
      }
      for(pricebookEntry p:[select id,name,product2.productcode,productcode,product2.Family,product2.description,UnitPrice  from pricebookentry where pricebook2id in :oset and isactive=:true ])
      {
      wrapperopp w=new wrapperopp();
      w.pid=p.id;
      w.name=p.name;
      w.check1=false;
      w.description=p.product2.description;
      w.prodfamily=p.product2.Family;
      w.prodcode=p.productcode;
      w.standardprice=p.UnitPrice;
      wlist.add(w);
      if(oset1.contains(w.pid))
      {
      w.check1=true;
      }
      }
      }
     
public opportunity geteditopp()
{
return oo;
}
public list<wrapperopp> getprod()
{
return wlist;
}

list<opportunitylineitem> optlist=new list<opportunitylineitem>();
public void save()
{
wrapperopp wo=new wrapperopp();
update oo;
for(opportunity o:[select id from opportunity where id=:oppid])
{
for(wrapperopp w2:wlist)
{
if(!oset1.contains(w2.pid))
if(w2.check1==true)
{
opportunitylineitem oi=new opportunitylineitem();
oi.PricebookEntryId=w2.pid;
oi.quantity=2;
oi.opportunityid=o.id;
oi.TotalPrice=120;
optlist.add(oi);
}
}
}

upsert optlist;
}

public void cancel()
{
oppeditview=false;
oppview=true;
}

}

 

VPrakashVPrakash

Thank you Lakshmi for the reply. I think this might help.