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
A9865A9865 

"New Task" button in apex:repeat table should repopulate "Related to" field

I have a "Add new Task" Button that should be displayed in every row of my visual force table via apex:repeat. The button leads to a rendered output panel, where a new task can be generated. When hitting the button in the according line, the field "Related To" of the new task should already be prefilled with the WhoId of the custom object that is queried within the Apex:repeat of the table.

Thus, how would I pass on the item.Id within the Apex repeat to the controller?
 
<apex:form >
    <apex:pageblock >
    <apex:pageMessages />

        <apex:outputpanel rendered="{!generateTask==false}">
            <table class="list" border="0" cellpadding="0" cellspacing="0" id="maintable">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Start Date</th>
                        <th>New Task</th>
                    </tr>
                </thead>

                <apex:outputPanel id="tableData">
                    <apex:repeat value="{!lstDevRequests_edit}"  var="item">
                        <tbody>
                            <tr>
                                <td>{!item.Name}</td>
                                <td><apex:outputText value="{0,date,MM/dd/yyyy}"> <apex:param value="{!item.Start_Date__c}" /> </apex:outputText></td>
                                <td><apex:commandButton action="{!generateTask}" value="New Task" id="newTaskButton"/></td>
                            </tr>
                        </tbody>
                    </apex:repeat>
                </apex:outputPanel>
            </table>
        </apex:outputpanel> 

        <apex:pageBlockSection title="Create New Task" columns="2" collapsible="false" rendered="{!generateTask==true}" >
            <apex:pageBlockSectionitem >
                    <apex:outputLabel value="Assigned To"/>
                    <apex:inputField value="{!insertnewTask.OwnerId}"/>
            </apex:pageBlockSectionitem>
            <apex:pageBlockSectionitem >
                    <apex:outputLabel value="Subject"/>
                    <apex:inputField value="{!insertnewTask.Subject}"/>
            </apex:pageBlockSectionitem>
            <apex:pageBlockSectionitem >
                    <apex:outputLabel value="Related To"/>
                    <apex:inputField value="{!insertnewTask.WhatId}"/>
            </apex:pageBlockSectionitem>         
        </apex:pageBlockSection>

    </apex:pageblock>
</apex:form>
 
public with sharing class tableRequests {
public List<Dev_Request__c> lstDevRequests_edit {get; set;}
public Task insertnewTask {get;set;}
public boolean generateTask {get;set;}



public tableRequests() {
    generateTask = false;
    lstDevRequests_edit = lstDevRequests_edit();
    insertnewTask = getnewTask();
}

public List<Dev_Request__c> lstDevRequests_edit() {
    if(lstDevRequests_edit == null)
        lstDevRequests_edit = [Select Id, Name, Assignee__c, Assignee__r.Name, Start_Date__c, Due_Date_QA__c, Estimated_Hours__c, Estimated_Completion_Date__c, Status__c, Overview__c, Parent_Dev_Request__c, (SELECT Id, WhatId, ActivityDAte, Owner.Name, Description, Status, Subject from Tasks) from Dev_Request__c];
    return lstDevRequests_edit;
}

public Task getnewTask() {
    Task newTask = new Task();
        newTask.WhatId = 'a0626000000EGH1AAO'; // This needs to be profiled with the corresponding item.Id
    return newTask;
}

public PageReference generateTask(){
    generateTask = true;
    return null;
}
}

Thank you for your help!
Best Answer chosen by A9865
SonamSonam (Salesforce Developers) 
Hi,

Could you try the following:

<td><apex:commandButton action="{!generateTask}" value="New Task" id="newTaskButton"/></td>

Change this boolean value to an action function in the controller which will generate the new task simialr to the function getnewtask:

<td><apex:commandButton action="{!createtask(item.id)}" value="New Task" id="newTaskButton"/></td>

In this function: createtask(item.id) pass the id of the row element as a parameter, this way you will be able to transfer the value in item.id to the controller to be used to add it to the new task as who ID




 

All Answers

SonamSonam (Salesforce Developers) 
Hi,

Could you try the following:

<td><apex:commandButton action="{!generateTask}" value="New Task" id="newTaskButton"/></td>

Change this boolean value to an action function in the controller which will generate the new task simialr to the function getnewtask:

<td><apex:commandButton action="{!createtask(item.id)}" value="New Task" id="newTaskButton"/></td>

In this function: createtask(item.id) pass the id of the row element as a parameter, this way you will be able to transfer the value in item.id to the controller to be used to add it to the new task as who ID




 
This was selected as the best answer
A9865A9865
Thank you, I solved the issue with an action support, but your method probably works as well. I have not tested it though.