I am not sure how to resolve this. With the new Orders Object Salesforce does not give you the option to create a new Order from the Opp. This is a requirement for our org. I have created a custom VF page and am using the Orders standard controller along with an extension to add some other functionality. Everything is working, but I cannot get my test class to pass because the AccountId field is not writeable.

public  class SaveAndAddProductControllerExtension {

private ApexPages.StandardController stdCtrl;
public Contact OrdContact {get;set;}
public Contact OrdContact2 {get;set;}
public Opportunity Name {get;set;}
public Opportunity ActName {get;set;}
public string id;
public string Acid;
Order ord;

    private ApexPages.StandardController controller;

        public SaveAndAddProductControllerExtension(ApexPages.StandardController controller) {  

            stdCtrl = controller;
            ord = (order)controller.getrecord();
            id = ApexPages.currentPage().getParameters().get('oppid'); 
            Acid = ApexPages.currentPage().getParameters().get('Accid');
            if (id != null)
            name = [select id, name from opportunity where id= :id];
            ord.Opportunity_Name__c = id;
            ord.AccountId = acid;
            this.controller = controller;

    public void ContactPopulated(){
        Order cont=(Order)stdCtrl.getRecord();
        OrdContact =[select id, MailingStreet, MailingCity, MailingState, MailingPostalCode from contact where id= :cont.BillToContactid ]; 

    public void ContactPopulated2(){
        Order cont=(Order)stdCtrl.getRecord();
        OrdContact2 =[select id, MailingStreet, MailingCity, MailingState, MailingPostalCode from contact where id= :cont.ShipToContactId]; 

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

    public PageReference save() {
        try {
        system.debug('*************' +Ord);
        ord.BillingCity = OrdContact.MailingCity;
        ord.BillingPostalCode = OrdContact.MailingPostalCode;
        ord.BillingState = OrdContact.MailingState;
        ord.BillingStreet = OrdContact.MailingStreet;

        ord.ShippingCity = OrdContact2.MailingCity;
        ord.ShippingPostalCode = OrdContact2.MailingPostalCode;
        ord.ShippingState = OrdContact2.MailingState;
        ord.ShippingStreet = OrdContact2.MailingStreet;
        insert Ord;

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



Test Class

@istest(seeAllData = True)
private class SaveAndAddProductControllerTest {

    static testMethod void testing(){

        Profile p = [SELECT Id FROM Profile WHERE Name = 'System Administrator' limit 1];
        List<user> UserList = [SELECT Id FROM user WHERE isActive = True AND profileid =: p.id limit 2];

        Account a2 = new Account();
        a2.Name = 'The Amazing';
        insert a2;

        Contact c = new Contact();
        c.firstname = 'Summer';
        c.LastName = 'Testing';
        c.AccountId = a2.Id;
        c.MailingCity = 'HappyTown';
        c.MailingPostalCode = '01010';
        c.MailingState = 'MA';
        c.MailingStreet = '123 Awesome St';

        insert c;

        Opportunity opp = new Opportunity();        
        opp.RecordTypeId = '012500000009kqw';
        opp.OwnerId = UserList[0].Id;
        opp.Name = 'Test Opp';
        opp.AccountId = a2.Id;
        opp.Line_of_Business__c = 'Banking';
        opp.Segment__c = 'Advisory Services';
        opp.Product__c = null;
        opp.CloseDate = system.today();
        opp.Deal_Status__c = 'Upside';
        opp.StageName = 'Closed Won';
        insert opp;

        Order order = new Order();
        order.RecordTypeId = '012W0000000CvTF';
        order.Opportunity_Name__c = opp.Id;
        order.AccountId = a2.Id;
        order.EffectiveDate = system.today();
        order.Status = 'Open';
        order.Price_List__c = '0-500M';
        order.BillToContactId = c.Id;
        order.ShipToContactId = c.Id;
        insert order;

        Order ord = [SELECT Id, RecordTypeId,Opportunity_Name__c, AccountId, EffectiveDate, Status, Price_List__c, BillToContactId, ShipToContactId FROM Order limit 1];

        SaveAndAddProductControllerExtension sAAP = new SaveAndAddProductControllerExtension(new ApexPages.StandardController (Order));

  • Hi Liz. I went ahead and edited your question for you to use the proper code formatting and make it readable. Please take some time learning how to format the code properly in this site to get code answers in the future - salesforce.stackexchange.com/editing-help#code. Also, in order to get any help with this question, the community is going to need to see your unit test. Show us what you have so far. Go ahead and edit your question and give us those details - salesforce.stackexchange.com/help/privileges/edit – Jesse Altman Jun 19 '14 at 11:16
  • I do know how to format the code...my apologies. I have been looking at this computer for hours now and had a little oversight. As far as the test class, I can post what I have so far, but I have not completed it yet because I wanted to figure out this error before moving on. Thanks – Liz Jun 19 '14 at 11:33
  • what is the exact error you are getting? I.e. including line numbers etc. – Guy Clairbois Jun 19 '14 at 13:10

First guess, without replicating on my org: try assigning the accountId right away when defining the Order:

    Order order = new Order(AccountId = a2.Id);
  • Removed ord.AccountId = acid; from extension (as suggested by guy)
  • Added the AccountId to the Order (also as suggested by by) and,
  • Added the PageRef just before defining the Order.

    PageReference pageRef = Page.orders;
    Order ord2 = new Order(AccountId = a2.Id);
    ord2.Opportunity_Name__c = opp.Id;
    ord2.RecordTypeId = '012W0000000CvTF';
    ord2.EffectiveDate = system.today();
    ord2.Status = 'Open';
    ord2.Price_List__c = '0-500M';
    ord2.BillToContactId = c.Id;
    ord2.ShipToContactId = c.Id;
    insert ord2;
  • Yea, I tried that...no dice! – Liz Jun 19 '14 at 12:58
  • in the SaveAndAddProductControllerExtension you are doing the same. What happens if you leave out that line? (ord.AccountId = acid;) – Guy Clairbois Jun 19 '14 at 13:44
  • Boom! Got it. I had tried removing the line (ord.AccountID = acid) and it didn't work, but I made some other changes(including assinging the account off the bat when defining the order) that I will post in my original question and then removed it again, walla! Thanks for your help. – Liz Jun 19 '14 at 15:37

Once you set the account id, you cannot change it. The following code gives the same error. If you remove the last 2 lines, it works fine.

Order sOrder = new Order();
sOrder.effectiveDate = Date.today();
sOrder.status = 'Draft';
insert sOrder;
update sOrder;

In the controller, you can check if the account id is null and then set it.

if (ord.AccountId == null) {
    ord.AccountId = acid;

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.