1

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 very much for your help!

1

You can pass the item.Id to the controller through <apex:param/> like this:

<apex:commandButton  value="New Task" id="newTaskButton" action="{!generateTask}" reRender="form">
    <apex:param name="selectedItemId" value="{!item.Id}" assignTo="{!selectedItemId}"/>
</apex:commandButton>

In the controller, do this changes:

public Id selectedItemId { get; set; }

public PageReference generateTask(){
        //you can use the selectedItemId here
        insertnewTask = new Task();
        insertnewTask.whatId = selectedItemId;
        generateTask = true;
        return null;
    }

Also a small note, change rendered="{!generateTask==true}" to rendered={!generateTask}" as it always evaluates if condition is true

| improve this answer | |
  • Thank you very much, that helped a lot and functions well! Highly appreciated! – user40501 Jan 24 '17 at 5:28
  • what about your last question, I have answered? – Santanu Boral Jan 24 '17 at 5:34
  • I am working on it now. I did not have time yesterday to look into it what was the issue. If you have some time to support further that would be nice though. In about half an hour I can be more specific what might be the issue. Thank you so much for your support! – user40501 Jan 24 '17 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.