0

I am very new to salesforce, apex, and visualforce. I created a developer account and began creating custom pages and apex classes. I am trying to create a method inside a custom controller class that will create a new task when I press a button.

My apex method:

public pageReference create_task() {
    List<Task__c> tasks = new List<Task__C>();

    Task__c new_task = new Task__c();

    new_task.Name = 'Test task using custom controller';
    new_task.Date_started__c = System.Today();

    tasks.add(new_task);


    try {
        insert tasks;
    } catch(DmlException e) {
        //do some error handling
    }

    return null;

}

If I call this method in the tag like:

<apex:page controller="<my class name>" action="{!create_task}">

It will work perfectly fine and the task is created in my development org.

However, I want to create the task after I press a button. I tried this:

<apex:commandButton action="{!create_task}" value="Create Task!" />

But this does not work when I click the button.

Why is is that if I put the action in the page element that it works, but if I put it in commandButton it does not do anything? I've searched online, but I cannot find anything.

Thanks for your help.

-- Nick

1
  • 1
    and you see nothing in the debug log when the commandButton is clicked?
    – cropredy
    Aug 12, 2014 at 22:26

1 Answer 1

0

This works for me

VF:

<apex:page controller="test">
   <apex:outputPanel>
        <apex:messages />
   </apex:outputPanel>
   <apex:form >
        <apex:commandButton value="Craete Task"  action="{!create_task}" />                  
   </apex:form>
</apex:page>

Apex:

public class test
{
    public pageReference create_task() 
    {
      List<Task__c> tasks = new List<Task__C>();
      Task__c new_task = new Task__c();
      new_task.Name = 'Test task using custom controller';
      new_task.Date_started__c = System.Today();
      tasks.add(new_task);
      try 
      {
        insert tasks;
      } 
      catch(DmlException e) {
        //do some error handling
      }
      return null;
    }
}     
1
  • I included the <apex:outputPanel> code and after that I was able to find out why it wasn't working and how to fix it.
    – Nicholas
    Aug 14, 2014 at 13:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .