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
RSButtarRSButtar 

Add Product Records from a Custom Object to Product Related list on Opportunity Detail Page

I have to Add Product Records from a Custom Package_Product__c Object that has lookup to Product2, into the Product Related list on Opportunity Detail Page 

I have Custom Link on the Opportunity Detail Page. This link Opens a VF Page with Drop downlist that contains records from a CUstom Object Package_Header__c (). Each Package have more than one records from the custom object Package_Products__c. When a user select the package from the Dropdown, all the products in the package are added to the Product Related List on the Opprotunity detail page.

I have completed the first part here, where I have inserted the Records Package_Header__c Records into the dropdown on the basis of Legal_entity__c field (it represent countries). Also I have queried the records from the custom Object Package_Products__c. But My main Problem is how to insert the records into Product Related List (This related list is Standard one and comes by default with every opportunity record and lists the products associated with the opportunity)

The coded for it is written below: I am using Extension = packageSelectionController  and Opportunity as Standard Controller

 

public class packageSelectionController {

public string legalEntity;

public Opportunity opp = new Opportunity();

public Package_Header__c pobj = new Package_Header__c();

public List<Package_Header__c> plist {get;set;}

 

public packageSelectionController(ApexPages.StandardController controller) {

String id = ApexPages.currentPage().getParameters().get('id'); opp = [

select Legal_Entity__c from Opportunity where id = :id];

legalEntity = opp.Legal_Entity__c;

plist = [select id, Name from Package_Header__c where Legal_Entity__c =: legalEntity];

}

 

//Create the picklist on the VF Page and populate it withy the records from Package_Header__c

String piValues;

public List<SelectOption> getItems(){

List<SelectOption> options = new List<SelectOption>();options.add(

new SelectOption(' ',' ')); for(Package_Header__c pobj1: plist)

{

options.add(new SelectOption(pobj1.id,pobj1.Name));

}

return options;

}

 

public String getPiValues(){ return piValues;

}

public void setPiValues(String selectedValue){ this.piValues = selectedValue;

}

 

public String getLegalEntity(){ return legalEntity;

}

//create a list of package products that holds the products for a particular package,(used in the SelectPackage method below)

public List<Package_Product__c> packageProduct = new List<Package_Product__c>();

public List<OpportunityLineItem> oplItem = new List<OpportunityLineItem>();

public PageReference SelectPackage(){

//Select the Required fields from the Package_Product__c Onthe basis of selected value of the picklist for Packages

String packageId = ApexPages.currentPage().getParameters().get(piValues);

packageProduct = [Select Id, Name, Quantity__c, Product2__c, Discount__c, Fixed_Price_Discount__c,

Sort_Order__c, Opt__c, Smpl__c, Product2__r.Id, Product2__r.ProductCode From Package_Product__c

Where Package_Header__c = :packageId ];

//variables for holding then field values from the

String productName;

String productCode;

String quantity;

String discount;

String fixedPrice;

String Opt;

 

//For loop for assigning the retieved values for each record in the above query to the variables declared above.

 for(Package_Product__c pkProd: packageProduct){oplItem.productName = packageProduct.Name;

oplItem.discount = packageProduct.Discount__c;

}

 

PageReference opportunityPage = new ApexPages.StandardController(opp).view();

opportunityPage.setRedirect(true);

return opportunityPage;

 

}

 

public PageReference cancel() {PageReference opportunityPage = new ApexPages.StandardController(opp).view();

opportunityPage.setRedirect(true);

return opportunityPage;

}

}

 

<!--  Code for Page -->

<apex:page standardController="Opportunity" extensions="packageSelectionController" tabStyle="Opportunity" sidebar="false" showHeader="true"  >
  <!-- Begin Default Content REMOVE THIS -->

  <apex:form >
 
  <div style="border: 1px solid red; padding-top:10px; padding-bottom:10px;">
  <apex:pageBlock rendered="true" title="You are Logged in as: {!$User.FirstName} {!$User.LastName}" >
 
  <apex:outputLabel value="Select Product Package from List: " for="packageList" style="font-family:Arial,Sans-Serif; line-height:10pt; font-size:11pt; margin-right:10px; "/><br/>
      <!-- This value parameter takes the name of the list -- getRequestTypeItems (without get) of type Select Options -->
    <br/>
  <apex:selectList value="{!piValues}" size="1">
      <apex:selectOptions value="{!items}"></apex:selectOptions>
  </apex:selectList>
 
 <br/>
 
 <apex:commandButton title="Select" value="Select" action="{!SelectPackage}"/>
 <apex:commandButton title="Cancle" value="Cancel" action="{!cancel}"/>
  </apex:pageBlock>
  </div>
  </apex:form>
  <!-- End Default Content REMOVE THIS -->
</apex:page>

Anand@SAASAnand@SAAS
You can insert using Apex into OpportunityLineItem which corresponds to the PRoducts related list that displays on the opportunity. For more information check look at this link
xzuvelxzuvel

Did you find a way to insert the package in the related product list ?