Let's say I have a custom object named Custom_object__c. This custom object is related to Opportunity object, which has the Opportunity Line Items.

How can I display selected opportunity fields and opportunity line item fields to this Custom_object__c dynamically by just searching for the OpportunityID? Meaning upon searching for a certain OpportunityID through lookup field in this custom object, some fields with values of these two objects will show up in edit mode?

Any concept or ideas would be great.

Controller: CustomObjectController

public class CustomObjectController{
    ApexPages.standardController customController;
    public Custom_Object__c cu {get;set;}

    public CustomObjectController(ApexPages.StandardController controller){
        customController = controller;
        cu = new Custom_Object__c();

    public PageReference save(){
        Custom_Object__c cu2 = new Custom_Object__c();
        cu2.Opportunity_Number__c = cu.Opportunity_Number__c;
        cu2.Date__c = cu.Date__c;
        cu2.Item__c = cu.Item__c;
        cu2.Price__c = cu.Price__c;
        insert cu2;

        Pagereference pageref;
        pageref = new PageReference('/' + cu2.Id);
        return pageref;

    public PageReference cancel(){
        return returnController.cancel();

VF Page: CustomObjectPage

<apex:page standardController="Custom_Object__c" extensions="CustomObjectController">
    <apex:sectionHeader title="Custom Object Form Edit" subtitle="New Custom Object Form"/>

    <apex:form >
        <apex:pageBlock title="Custom Object Form Edit" mode="edit">
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>

            <apex:pageBlockSection title="Custom Object Information">
                <apex:inputField value="{!cu.Order_Number__c}"/>
                <apex:inputField value="{!cu.Date}"/>

            <apex:pageBlockSection title="Item Information">
                <apex:inputField value="{!cu.Item__c}"/>
                <apex:inputField value="{!cu.Price__c}"/>



Note: Date__c, Item__c, and Price__c are all formula fields:

Date__c references the Opportunity_Date__c from Opportunity__c object while

Item__c and Price__c reference the Item with corresponding price from Product in Opportunity_LineItem__c (child of Opportunity__c)

I mimicked the standard Opportunity and Opportunity Line Item objects in salesforce. I resorted to formula fields as an alternative. But I want this approach to be just temporary for it only shows the field values upon clicking the save button.

  • You need to create custom VF edit page for this. Because we can't add VF page in standard edit page. – Tushar Sharma Jun 26 '16 at 12:10
  • yes i'm aware of that....what I don't get is the process of getting and displaying the selected field values of Opportunity and Opportunity Line Items related to the searched Opportunity Id in the custom object – asdfg Jun 26 '16 at 12:25
  • If you mind to share your code here. – Santanu Boral Jun 26 '16 at 15:11

Salesforce does not provide any shortcut way of displaying relationship fields without querying those. Only thing you would get is OpportunityId in the controller argument. To display opportunity and line item records you need to fetch those values through the SOQL and assign those values.

Hope it helps!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.