function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mike OtterMike Otter 

Field is not writeable - error following code in book

Background:
Although I have coded, it's been in the mainframe world and, thus, I have no experience in Apex.  I went through the tutorial (Visualforce Workbook) and now am in the associated thicker book (Visualforce in Practice).  In Chapter 5 (Building Wizards), the exercise is to code three pages which call each other.  In a nutshell, you enter data about the parent and, when you click Continue, it saves the record and takes you to page 2 where you enter information about the child.  (Page 3 is about grandchildren--same idea--but I'm not there yet).  The three tables are related with a Master-Detail field.

Problem:
Page 1 information is saved and takes me to page 2.  When I click on Save, it's supposed to get the ID of the parent (it does--it's in the log) and then somehow save the child's record.  I don't think it's getting to that line (failure is indicated on the line after the get and before the insert) but have no clue why.  The errant line is in bold (about ten lines from the bottom).

I'd like to solve this myself but, since I don't know enough to know where to look, I figured I'd try here while I look elsewhere.  I've checked the online version of the book (http://www.developerforce.com/guides/Visualforce_in_Practice.pdf) but it has the same code as the book.  I've searched this forum and Google but no one has posted these exercises and I can't see how to relate posted solutions to this one.

Thanks in advance.

Mke
 

The code:
public class ProjectCreateExtension
{
private ApexPages.StandardController sc;
    public Sprint__c sprint {get; set;}
    public List<Sprint__c> sprints {get; set;}
    public String selectedSprint {get; set;}
   
    public ProjectCreateExtension(ApexPages.StandardController standardController)
    {
        // store a reference to the standard controller
        sc = standardController;
        sprint = new Sprint__c();
        // create a new list to store the sprints added by the user
     sprints = new List<Sprint__c>();
    }
   
    public PageReference ToPage1()
    {
        return Page.ProjectCreate1;
    }

    public PageReference ToPage2()
    {
        if (ApexPages.CurrentPage().GetURL().StartsWithIgnoreCase('/apex/projectcreate1'))
        {
            sc.Save();
            Project__c project = (Project__c)sc.GetRecord();
            sprint.Project__c = project.Id;
            insert sprint;
        }
            return Page.ProjectCreate2;       
    }

    public PageReference ToPage3()
    {
        return Page.ProjectCreate3;
    }
   
    public PageReference SaveSprint()
    {
        Project__c project = (Project__c)sc.GetRecord();
        sprint.Project__c = project.Id;
        insert sprint;
       
        sprints.Add(sprint);
        sprint = new Sprint__c();
       
        return null;
    }
       
}
souvik9086souvik9086
Check whether the field Project__c in Sprint is writeable or not.
 
Mike OtterMike Otter
As far as I can tell by looking at the object, it is.  I just created a tab for the child and was able to add a record easily.  Beyond that, I'm not sure where else to look.
Vinit_KumarVinit_Kumar
How you are calling this controller,through VF page ,if so can you check whether this field is Editable for the User or not.

You can check the FLS of 
Go to Setup - > Profiles -> select the profile -> look for custom object field level security and search for Sprin object -> Click on view and look for field Project.

Hope this helps !!


Mike OtterMike Otter
I forgot to mention that I'm using the Developer Edition and I'm doing most of my editing in the Developer Console.

As for FLS, the only read-only fields on Sprint are CreatedBy and LastModifiedBy.

The code/controller is being called from a VF page using the line:

<apex:commandButton action="{!SaveSprint}" value="Save"/>
Vinit_KumarVinit_Kumar
Ok here is the issue I saw just now,I am assuming Project__c is master-detail field and you can't reparent the child unless reparenting is allowed at field level.

As you hyave already associated your Sprint record in method ToPage2(),you can't reparent it in SaveSprint() method as in master-detail reparenting is not allowed unless it is declared at field level.

If this helps,please mark it as best answer to help others :)
Mike OtterMike Otter
@Vinit_Kumar, I sort of understand what you're saying (children can't change parents) but I don't think the code is doing that.  Then again, I'm just typing what the book tells me to write and the light bulb hasn't lit up yet.

Am I supposed to merge the code somehow (if they can't co-exist) or call one snippet from the other section?
Vinit_KumarVinit_Kumar
I have not gone through the book.There might be couple of things I am ssuming :-

1.) The book is using Lookup relationship as this would work for Lookup.
2.) The master-detail field in book allows reparenting at the field level.That's a checkbox you need to check while creating the field.

Hope this helps !!
Mike OtterMike Otter
1) The book directs us to use Master-Detail relationship for this exercise.
2) I edited the M-D relationship, checked the box for "reparenting", tried the sequence again, and got a different error message:
Insert failed. First exception on row 0 with id a07i000000Esw1xAAB; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Error is in expression '{!SaveSprint}' in component <apex:commandButton> in page projectcreate2

I'll also see if I can contact the book's author(s).  If the code works, they must have a copy of the working code somewhere.
Kaipu ReddyKaipu Reddy
remove insert sprint in topage2 method, below is the corrected one

public PageReference ToPage2()
    {
        if (ApexPages.CurrentPage().GetURL().StartsWithIgnoreCase('/apex/projectcreate1'))
        {
            sc.Save();
            Project__c project = (Project__c)sc.GetRecord();
            sprint.Project__c = project.Id;
           
        }
            return Page.ProjectCreate2;      
    }
Greg MontgomeryGreg Montgomery
Thank you, that fix was helpful. I had hit the exact same issue as Mike Otter.

Does anyone know where to find the source code for the book that the example is taken from: http://www.developerforce.com/guides/Visualforce_in_Practice.pdf  ?

It's a pretty good guide but the PDF is rife with error, thought the source code companion for the book might be cleaner.

Thanks !