+ Start a Discussion
Daniel B ProbertDaniel B Probert 

System.SObjectException: Field is not writeable

Hi,

I'm having a really annoying issue with the test class that I'm putting together that I can't figure out what I'm getting an error on so hoping someone in the community can point me in the right direction.

I have an Object Leave__c that I use to log holidays(nothing special just a link to employee records/start/return dates)
I have created a simple VF Page for adding leave:

<apex:page standardcontroller="Leave__c" extensions="employee_leave_controller" showheader="false" sidebar="false">
<apex:stylesheet value="{!URLFOR($Resource.myStyles, 'employee.css')}"/>
    <div style="width:100%; height:100%">
        <a href="/apex/my_leave?"><button type="button">Back to My Leave List</button></a>
    </div>
    <apex:form >
        <apex:pageBlock title="Submit Leave for {!$User.FirstName}">
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Leave Information">
                <apex:inputField value="{!Leave__c.Leave_Type__c}" required="true"/>
                    <span>Select your leave type</span>
                <apex:inputField value="{!Leave__c.Employee__c}" required="true"/>
                    <span>Is this you? If not contact IT</span>
                <apex:inputField value="{!Leave__c.Start_Date__c}" required="true"/>
                    <span>Enter the first day of your holiday</span>
                <apex:inputField value="{!Leave__c.Return_Date__c}" required="true"/>
                    <span>Enter the day before your return to work</span>
                <apex:inputField value="{!Leave__c.No_Of_Days__c}" onfocus="true" required="true"/>
                    <span>{!$ObjectType.Leave__c.fields.No_Of_Days__c.InlineHelpText}</span>               
                <apex:inputField value="{!Leave__c.Notes__c}"/>
                    <span>Any additional information about your abscense that will help your line manager quickly approve.</span>
            </apex:pageBlockSection>
       </apex:pageBlock>
    </apex:form>
</apex:page>

This is my apex extension:

public class employee_leave_controller { 
    private Leave__c leave;
    private ApexPages.StandardController stdController;
    public employee_leave_controller (ApexPages.StandardController controller) {
       
        //pre-populate Lost notes on load
        this.leave = (Leave__c)controller.getRecord();
        leave.Employee__c = [select id from employee__c where Salesforce_Account__r.id =: UserInfo.getUserID()].id;
        stdcontroller=controller;
       
    }
   
   
    public PageReference save()
    {
        //Custom Save Code
        upsert leave;
        PageReference pageRef=Page.my_leave;
        return pageRef;
     }
}

and this is my test class(note the system administrator i've only added now to check the error wasn't permissions related):

@isTest
public class Test_Employee_Portal{
public static testMethod void TestMyLeaveRequest(){
        // set up user and manager
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        User u1 = new User(Alias = 'standt1', Email='demo1@randomdemodomain.com',EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',LocaleSidKey='en_US',ProfileId = p.Id,TimeZoneSidKey='America/Los_Angeles', UserName='dprobertdemo1@camfed.org');
        insert u1;
        User u2 = new User(Alias = 'standt2', Email='demo2@randomdemodomain.com',EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',LocaleSidKey='en_US', ProfileId = p.Id,TimeZoneSidKey='America/Los_Angeles', UserName='dprobertdemo2@camfed.org', ManagerID=u1.Id);
        insert u2;
        //add employee
        Employee__c emp=new Employee__C(Name='Demo Demo',First_Name__c='Demo',Last_Name__c='Demo',Salesforce_Account__c=u2.id);
        insert emp;
//add employee holiday allowance        
Employee_Holiday_Entitlement__c empent=new Employee_Holiday_Entitlement__c(Allowance__c=10,Employee__c=emp.id,Year__c='2014');
        insert empent;
       //test creating a new page
        System.runAs(u2){
            PageReference pageRef = Page.my_leave_request;
            Test.setCurrentPage(pageRef); 
             // leave details 
            Leave__c leav = new Leave__c(Employee__c=emp.id,Start_Date__c=system.today(),Return_Date__c=system.today(),No_of_days__c=5,Leave_Type__c='Holiday');
            insert leav;
       
            ApexPages.standardController controller = new ApexPages.standardController(leav);
           
            employee_leave_controller pag = new employee_leave_controller(controller);         
            pag.save();
        }
    }

the error that i'm getting is:

System.SObjectException: Field is not writeable: Leave__c.Employee__c
Class.employee_leave_controller.<init>: line 8, column 1
Class.Test_Employee_Portal.TestMyLeaveRequest: line 23, column 1

Anyone able to give me a steer on what i've got wrong.

many thanks
dan
Best Answer chosen by Daniel B Probert
Pat PattersonPat Patterson
What is the relationship between Leave__c and Employee__c? If it's master-detail then, by default, you can't 'reparent' the record, that is, write to the Employee__c field after the detail record has been created. You can, however, selecting 'Allow Reparenting' in the master-detail relationship definition to make it work.

All Answers

Pat PattersonPat Patterson
What is the relationship between Leave__c and Employee__c? If it's master-detail then, by default, you can't 'reparent' the record, that is, write to the Employee__c field after the detail record has been created. You can, however, selecting 'Allow Reparenting' in the master-detail relationship definition to make it work.
This was selected as the best answer
Daniel B ProbertDaniel B Probert
grrr i thanks sorted now...