You need to sign in to do that
Don't have an account?
Mike Otter
Field is not writeable - error following code in book
Background:
Problem:
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:
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;
}
}
{
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;
}
}
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 !!
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:
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 :)
Am I supposed to merge the code somehow (if they can't co-exist) or call one snippet from the other section?
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 !!
2) I edited the M-D relationship, checked the box for "reparenting", tried the sequence again, and got a different error message:
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.
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;
}
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 !