1

I have created a custom lookup field on the Order object to the Opportunity because we want to create Orders from Opps and not Accounts.

I have also created a Visualforce page for Orders and am attempting to get both the Opportunity Name and the Account name to populate when creating a new record.

I have tried everything that I can find but cannot figure this out and I know it is something simple that I am missing.

Here is some of the VF page:

<apex:page showHeader="true" Standardcontroller="Order" extensions="SaveAndAddProductControllerExtension">
<apex:sectionHeader title="New Order" subtitle=""/> 
    <apex:form > 
        <apex:pageBlock title="New Order" mode="edit">
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Save & Add Product" action="{!save}"/><apex:commandButton value="Cancel" action="{!cancel}"/> 
            </apex:pageBlockButtons>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Save & Add Product" action="{!save}"/><apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
                <apex:pageBlockSection title="Order Information" columns="2">
                    <apex:inputField value="{!order.Opportunity_Name__r.Name}"/>
                    <apex:inputField value="{!order.AccountId}"/>
                    <apex:inputField value="{!order.EffectiveDate}" required="true"/>
                    <apex:inputField value="{!order.Database__c}" required="false"/>
                    <apex:inputfield value="{!order.EndDate}" required="true"/>
                    <apex:inputField value="{!order.Status}" required="true"/>
                    <apex:pageBlockSectionItem ></apex:pageBlockSectionItem>
                    <apex:inputField value="{!order.Price_List__c}" required="false"/>
                </apex:pageBlockSection>

And my class:

public class SaveAndAddProductControllerExtension {

public Order order;
public String oppName;

    public SaveAndAddProductControllerExtension(ApexPages.StandardController controller) {  
        this.order = (Order)controller.getRecord();
        this.oppName = order.Opportunity_Name__r.Name;
    }

    public String getoppName() {
    {
        oppName = [SELECT Name FROM Opportunity WHERE Id = :order.Opportunity_Name__c].Name;
    }

    return oppName;
}

Order ord;

public Order getOrder() {
    if(ord == null) ord = new Order();
        ord.Opportunity_Name__c = ord.Opportunity_Name__r.Id;
        ord.Account = ord.Opportunity_Name__r.Account;
        ord.EffectiveDate = System.Today();
        ord.EndDate = System.Today() + 1095;
        ord.Status = 'Open';    
        return ord;
}

public PageReference save() {
    try {
    system.debug('*************' +Ord);
    insert Ord;

    PageReference neword = new PageReference('/apex/orderProductEntry?Id='+Ord.Id);
    system.debug('test::::::::::::' + neword);
    neword.setRedirect(true);
        return neword;
    }
    catch (DMLException e) {
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error creating new order.'));
    return null;
}

    return null;
  }

}

I really wanted to figure this out on my own, but I am at my breaking point. Any help would be immensely appreciated.

4
  • What are you expecting to happen that is not? Or what is happening that shouldn't? Commented May 18, 2014 at 6:41
  • My apologies if my question was unclear. I would like the Opportunity Name field (which is a lookup from Orders to Opps) to show the Opp name when creating a new Order record. The same holds true for the Account Name field.
    – Liz
    Commented May 18, 2014 at 6:47
  • How are you invoking your VF page? From one of the parent records? Commented May 18, 2014 at 6:51
  • From the Opportunity record with a custom button on the Orders Related List
    – Liz
    Commented May 18, 2014 at 7:02

1 Answer 1

2

You need to pass the ID of the parent object (Opportunity) to the new child object page. So the Order "List Button" that you add to the related list on Opportunity should be of type URL and look like this (assuming "Order" is the name of the page):

/apex/Order?oppId={!Opportunity.Id}&accId={!Opportunity.AccountId}

(Both the Opportunity and Account IDs are being passed to save the controller the trouble of having to query for the Account ID.)

The controller can then pick up these values and set them on the object instance provided by the standard controller:

public with sharing class OrderExtension {
    private ApexPages.StandardController sc;
    public OrderExtension(ApexPages.StandardController sc) {
        this.sc = sc;
        // Other methods in the controller can get the Order like this too
        Order o = (Order) sc.getRecord();
        Map<String, String> m = ApexPages.currentPage().getParameters();
        o.Opportunity__c = (Id) m.get('oppId');
        o.AccountId = (Id) m.get('accId');
    }
    ....
}

When the page is rendered, because the ID values for Opportunity and Account are set, the name values are automatically queried and presented in the fields:

<apex:page showHeader="true" Standardcontroller="Order" extensions="OrderExtension">
    <apex:sectionHeader title="New Order" subtitle=""/> 
    <apex:form>
        <apex:pageBlock >
            <apex:pageBlockSection title="Order Information" columns="2">
                <apex:inputField value="{!Order.Opportunity__c}"/>
                <apex:inputField value="{!Order.AccountId}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Note that the Order being referenced here in the Visualforce is the standard controller instance.

Other fields on that standard controller instance can also be set as needed and then the default save used or a custom save method written that does a bit more work.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .