Skip to main content
1 of 3
Liz
  • 149
  • 3
  • 13

Test Code assistance on Orders - System.SObjectException: Field is not writeable: Order.AccountId

I am not sure how to resolve this. With the new Orders Object SF 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. Any help/ideas is greatly appreciated.

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);
    neword.setRedirect(true);
        return neword;
    }
    catch (DMLException e) {
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error creating new order.'));
    return null;
}

}

}

Liz
  • 149
  • 3
  • 13