+ Start a Discussion
Paul.FoxPaul.Fox 

Insert fails if commandLink has rerender attribute

I have created a simple page in Apex that shows my tasks for the day and gives me a place to add a new task.
However, I can't seem to get the commandLink or the form to create a new task if I use the rerender parameter.
This means that when I add a new Task the whole page refreshes, which takes a lot longer than just refreshing the task list.

Here's the snippet from the Apex Page:
Code:
  <apex:form id="AddTask">
      <apex:inputText size="40" id="NewTask"/>
      <apex:commandLink value="Add Task" action="{!AddTask}" rerender="openTasks"/>
  </apex:form>
Somewhere below this I have a <ul id="openTasks">. If I take off that rerender

 Here's the AddTask method in the Controller:
Code:
    public PageReference AddTask() {
        Task newTask = new Task(Subject='xxx',Priority='Normal',Status='Not Started');
        string NewTaskSubject = 'Hello'; //Need to change this to get value from field.
        newTask.subject = NewTaskSubject;
        insert(newTask);
        return null;
    }

I also need to figure out how to grab the new task subject if anyone wants to help me with that. I can't use the standard edit method because I don't want the standard subject field with the little pop-up box.

jwetzlerjwetzler
The rerender attribute must refer to the id of a Visualforce component.  The id is not a DOM id but rather an identifier for a Visualforce component (and the identifier is used to create the DOM id).


Message Edited by jwetzler on 10-30-2008 11:12 AM
Paul.FoxPaul.Fox
I switched out the list with an apex:pageBlockTable with id="openTasks" and it still doesn't work.

Here's the pageBlock Table tag
Code:
<apex:pageBlockTable value="{!OpenTasks}" var="task" id="openTaskList">

 

jwetzlerjwetzler
Well we're probably going to need to see a little more of your code in order to help you.  There's not much to go on here.

Does your getOpenTasks method actually re-run the query?  In other words maybe you want to set your openTasks list to null when your action method is called so the next time getOpenTasks is called it runs your query again.

When you say it doesn't create the task are you saying the create doesn't work, or it's just not showing up in the list?

I'm just speculating here because there isn't enough info for me to really know what's going on.
Paul.FoxPaul.Fox
It wasn't creating the task at all, I couldn't find it in salesforce.

I cleaned up some of the other code and the commandButton now works even with the rerender.
However, how do I get it to work if someone just presses enter also?

Page:
Code:
<apex:page controller="ToDoListController">
  <apex:pageBlock title="{!$User.FirstName}'s To-Do List" id="todo">

 <apex:form id="AddTask"> <apex:inputText size="40" id="NewTask" value="{!NewTaskSubject}"/> <apex:commandButton action="{!AddTask}" value="Add Task" rerender="todo"/> </apex:form>
<apex:form > <apex:pageBlockTable value="{!OpenTasks}" var="task" id="openTaskList"> // Open task info removed for easier reading </apex:pageBlockTable>
<apex:pageBlockTable value="{!CompletedTasks}" var="task" id="completedTaskList" > <apex:column headerValue="Completed Tasks" style="color:#8B9B93;"> //Completed task info removed for easier reading. </apex:column> </apex:pageBlockTable> </apex:form>
</apex:pageBlock> </apex:page>

Relevant Controller Methods:
Code:
public PageReference AddTask() {
        Task newTask = new Task(Subject='xxx',Priority='Normal',Status='Not Started');
        newTask.subject = NewTaskSubject;
        insert(newTask);
        NewTaskSubject=null;
        return null;
    }

public List<Task> getOpenTasks() {
    return [select Id, Subject, Who.Id, Who.Name, IsClosed, ActivityDate from Task 
        where IsClosed = False AND (ActivityDate <= TODAY OR ActivityDate = null)
        ORDER BY LastModifiedDate ASC
        LIMIT 50];
}

 


jwetzlerjwetzler
Are you working in IE?  There's a bug in IE where hitting the enter key does not submit the form if there is only one input in it.  Workaround is to add an empty input or hidden input in your form.

By the way it's not necessary or recommended for you to have two forms on your page.  You should encapsulate everything in one form tag.  If you need to control which areas get submitted on a postback you want to look into using actionRegion.
Paul.FoxPaul.Fox
I'm working in firefox 2. I had it all in one form but I split it into two since I thought that might be why it wasn't working. Also I was trying to use the actionsupport command on the first form to handle the case when someone presses enter, but I couldn't get it to work.

My understanding of forms is that there is no problem with having multiple forms on the page. In fact if they have different functions they should be different forms. Is there something with visualforce that makes unnecessary or even bad?