[SalesForce] Create new record in one controller and pass Id to a second controller

Still pretty 'green' when it comes to Apex and I am having a difficult time with this one issue. So, I have a Visualforce page that replaces Orders to replicate the button 'Save and Add Product' which redirects to another Visualforce page that I created to clean up the Add Product page. Both pages work perfect on their own, but when I attempt to redirect to the other page I get and error.

System.QueryException: invalid ID field: null

I cannot figure out how to get the Id from the controller on the Orders page after it is created to pass to the controller for the Order Products page. Here is a little bit of the code.

Controller for the Orders VF page

public class SaveAndAddProductControllerExtension {

    public SaveAndAddProductControllerExtension(ApexPages.StandardController controller) {
    }

    Order ord;

    public Order getOrder() {
        if(ord == null) ord = new Order();
            return ord;
    }

    public PageReference save() {
        try {
        insert Ord;

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

    return null;
  }

}

Controller for the Order Products VF page

 public with sharing class ordersProductEntryExtension {

    public Order theOrder {get;set;}
    public String searchString {get;set;}
    public orderItem[] shoppingCart {get;set;}
    public priceBookEntry[] AvailableProducts {get;set;}
    public Pricebook2 theBook {get;set;}   
    public String toSelect {get; set;}
    public String toUnselect {get; set;}
    public Decimal Total {get;set;}
    public Boolean overLimit {get;set;}
    public Boolean multipleCurrencies {get; set;}

    private orderItem[] forDeletion = new orderItem[]{};

    public ordersProductEntryExtension(ApexPages.StandardController controller) {

        // Need to know if org has multiple currencies enabled
        multipleCurrencies = UserInfo.isMultiCurrencyOrganization();

        // Get information about the Order being worked on
        if(multipleCurrencies)
            **theOrder = database.query ('SELECT Id, Pricebook2Id, Pricebook2.Name, CurrencyIsoCode from Order where Id = \'' + controller.getRecord().Id + '\' limit 1');**
        else
            theOrder = [SELECT Id, Pricebook2Id, PriceBook2.Name FROM Order WHERE Id = :controller.getRecord().Id limit 1];

The error is on line 23 of the Orderproductentryextension which is the one that contains the database query towards the bottom. Any help would be greatly appreciated.

Best Answer

Liz, it looks like you're implementing extensions to standard controllers. In this case, I think it's important for you to know that the standard controller expects the record ID to be in the id page parameter.

Try making the modification below to SaveAndAddProductControllerExtension.save().

//PageReference neword = new PageReference('/apex/orderproductentry?oId='+order.Id);
//neword.setRedirect(true);
//return neword;

PageReference nextPage = Page.OrderProductEntry;
nextPage.getParameters().put('id', order.Id);
return nextPage;
Related Topic