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
Prema -Prema - 

How to delete the Opportunityline items those which are selected from the list of records. On the VF page , all the products should be displayed and when i select the checkbox then all the selected line items should be deleted related to an opportunity.

Shubham NandwanaShubham Nandwana
Hi Prema,
You can get the list of ID of all checked rows then query to get all selected OpportunityLineItem sobjects (using 'List<OpportunityLineItem> list=[select id from OpportunityLineItem where Id IN : listOfSelectedIds'),  run 'delete list' to delete all the objects. 

Shubham Nandwana.
AppPerfect Corp.
salesforce@appperfect.com
408-252-4100
http://www.appperfect.com/services/salesforce/
Salesforce Development & Operations Experts
Rahul.MishraRahul.Mishra
Hi Prema,

Here is the code:
 
VF Page:

<apex:page controller="OpportunityProductController">
 <apex:form id="fm">
  <apex:pageBlock >
  <apex:commandButton value="Delete Selected Records" action="{!RemoveSelected}" reRender="fm"/>
   <apex:pageBlockTable value="{!lstOppProductWrapper}" var="OppProduct" id="pbt">
   <apex:column >
    <apex:facet name="header">
     Action Column
    </apex:facet>
    <apex:inputCheckbox value="{!OppProduct.chkbox}"/>
   </apex:column>
    <apex:column value="{!OppProduct.opc.Name}"/>
    <apex:column value="{!OppProduct.opc.Opportunity.Name}"/>
   </apex:pageBlockTable>
  </apex:pageBlock>
 </apex:form>
</apex:page>


Classes:

public class OpportunityProductController {
    
    public List<OppProductWrapper> lstOppProductWrapper {get;set;}
    public String productRecordId {get;set;}
    public List<OpportunityLineItem> lstOppProductWrapperToDelete;
    
    public OpportunityProductController() {
        
        lstOppProductWrapper = new List<OppProductWrapper>();
        lstOppProductWrapperToDelete =  new List<OpportunityLineItem>();
        
        for(OpportunityLineItem oli : [Select Id, Name, Opportunity.Name, OpportunityId From OpportunityLineItem]) {   
            lstOppProductWrapper.add(new OppProductWrapper(oli));
        }
    } 
    
    public void RemoveSelected() {
        
        for(OppProductWrapper opw : lstOppProductWrapper) {
            if(opw.chkbox == true) {
                lstOppProductWrapperToDelete.add(opw.OPC);
            } 
        }
        if(!lstOppProductWrapperToDelete.isEmpty())
            delete lstOppProductWrapperToDelete;
        lstOppProductWrapperToDelete.clear();
        lstOppProductWrapper.clear();
        for(OpportunityLineItem oli : [Select Id, Name, Opportunity.Name, OpportunityId From OpportunityLineItem]) {   
            lstOppProductWrapper.add(new OppProductWrapper(oli));
        }
    }
    
    public class OppProductWrapper {
        public boolean chkbox {get;set;}
        public OpportunityLineItem OPC {get;set;}
        
        public OppProductWrapper(OpportunityLineItem OPC) {
            this.OPC  = OPC;
            chkbox  = false;
        }
    }   
}
Mark it as solved if it does help you
Prema -Prema -
Hello friends thanks for your reply i was able to figure it out on my own. If possible could you please let me know how can i update all the records of products when i click on Selected products.
Shubham NandwanaShubham Nandwana
Hi Prema,
Once you have the list of records selected, you can directly loop over them, update their desired fields and use update command to update records.
for(OpportunityLineItem opp:listToBeUpdated){
  //update the desired fields
}
update listToOfUpdated;

Shubham Nandwana.
AppPerfect Corp.
salesforce@appperfect.com
408-252-4100
http://www.appperfect.com/services/salesforce/
Salesforce Development & Operations Experts
Prema -Prema -
No, I dont want any specific field to select it and get it in the list and finally update. I want it on the VF page but I am thinking how it would be possible to update the selected record on vf page. with maybe 3-4fields of every products it should be showing over vf page and there would be inline editing so that i can edit it and save it