3

I am having trouble passing values via Javascript.

APEX PAGE:

//STUFF HERE

<b>First Goal:</b><br /><br />
Goal:<apex:inputField id="Goal" value="{!Account_Plan__c.Goal1__c}" /><br />

<b>Second Goal: </b><br /><br />
Goal:<input id="Goal2" /><br />

<b>Third Goal: </b><br /><br />
Goal:<input id="Goal3" /><br />

Strategy to Achieve Goal:<input id="strat" type="text" size="90"/>  


<script type="text/javascript">
      function newtask() {
      AddTask(
          document.getElementById("strat").value,
          document.getElementById("Goal").value,
          document.getElementById("Goal2").value,
          document.getElementById("Goal3").value
           );

      }
      </script> 

  <apex:actionFunction name="AddTask" action="{!AddTask}">
          <apex:param name="strat" value="" />
          <apex:param name="Goal" value="" />
          <apex:param name="Goal2" value="" />
          <apex:param name="Goal3" value="" />
  </apex:actionFunction>

Controller

...OTHER CODE AND CONSTRUCTOR..
Public Pagereference AddTask(){
//    String taskdate = Apexpages.CurrentPage().GetParameters().get('tdate');
//    Date d = date.valueof(taskdate);
  String goal = Apexpages.currentPage().getParameters().get('goal');
    String goal2 = Apexpages.currentPage().getParameters().get('Goal2');
    String goal3 = Apexpages.currentPage().getParameters().get('Goal3');
    String strat = Apexpages.currentPage().getParameters().get('strat');

List<Account_plan__c> a = new List<Account_plan__c>();
a = [Select account__c from Account_plan__c where id = :CurrentAcct];

    Ntask = new Task();
    Ntask.ActivityDate = date.parse('5/25/13');
    Ntask.Subject = strat;
    Ntask.OwnerId = CurrentUser;
    Ntask.WhatId = a[0].account__c;
    Ntask.Status = 'In Progress';
    Ntask.Priority = 'Normal';
    Ntask.Type = 'Call';
    Ntask.Goal_Name__c = goal;



    try{
    insert Ntask;}
    catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'There was an Error! Cannot Create Task'));
      Ntask.addError(e.getMessage());
    } finally {
    System.debug('Insert Task Error');
    }
    SearchTact();
    return null;
    }

This code works without passing parameters, if I were to preset the variables in my controller. I have already tested it. I tried passing them with Javascript and it's not working at all.

Any help would be great.

1
  • What does "not working at all" mean? Are you seeing errors, or javascript console exceptions, etc? What have you tried?
    – jkraybill
    Commented May 4, 2013 at 2:47

2 Answers 2

1

You're pretty close, but unless you want to go all javascript for a reason (there are plenty), using the native getters and setters with a command button is a lot easier.

VisualForce Snippet

<apex:inputText value="{!goal}"/>
<apex:inputText value="{!goal2}"/>
<apex:inputText value="{!goal3}"/>
<apex:inputText value="{!strat}"/>
<apex:commandButton value="New Task" action="{!newTask}"/>

Controller Snippet

public String goal { get; set; }
public String goal2 { get; set; }
public String goal3 { get; set; }
public String strat { get; set; }

public void newTask() {
  // access params directly via controller properties
  Task t = new Task(
      goal__c = goal
    , goal2__c = goal2
    , goal3__c = goal3
    , strat__c = strat
  );
  insert t;
}
3

Instead of using:

  String goal = Apexpages.currentPage().getParameters().get('goal');
String goal2 = Apexpages.currentPage().getParameters().get('Goal2');
String goal3 = Apexpages.currentPage().getParameters().get('Goal3');
String strat = Apexpages.currentPage().getParameters().get('strat');

Follow the pattern here, assigning the variables to the controller via getters/setters

For example:

string goal;

public setGoal(string n){
   this.goal = n;
}

public getGoal(){
   return this.goal;
}

public AddTask(){
   // perform actions for method, goal will be assigned when actionfunction is run 
}
1
  • Jordan. Can you give me an example? I tried to use Public String Goal {get; set;}, do i also need to add public void setGoal(String Goal){this.Goal = Goal;} ?
    – AlphaBravo
    Commented May 3, 2013 at 19:05

You must log in to answer this question.

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