Skip to main content
3 of 3
Added Test Class Code
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 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);
        neword.setRedirect(true);
            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));

    }
}
Liz
  • 149
  • 3
  • 13