2

This is probably a simple fix, but I'm struggling. I have a VF page that pre-populates Task fields via a controller extension. I want to then give the user the option to create a follow-up task. I want this task to duplicate some of the fields entered by the user in the initial task (e.g., subject, due date + 7 days, etc.). Can anyone help. Thanks

Controller:

public with sharing class RDATasks{

    public list<Task> listTask{get;set;}
    public list<Task> listTaskFollow{get;set;}

    private final Contact con;
    public RDATasks(ApexPages.StandardController myController){
        listTask = new List<Task>();
        listTaskFollow = new List<Task>();
        con=(Contact)myController.getRecord();
    }

//  Adds Task to list
    public void RDA()
    {
        listTask.add(new Task(
           WhoId = con.Id,
           Status = 'Completed',
           ActivityDate = system.TODAY()+7,
           OwnerId = UserInfo.getUserId(),
           WhatId = con.AccountId,
           RDA__c = TRUE)
           );
    }

//  Adds Follow-Up Task to list
    public void RDAfollow()
    {
        listTaskFollow.add(new Task(
           WhoId = con.Id,
           Status = 'In Progress',
           ActivityDate = system.TODAY()+7,
           Subject = 'Follow-Up: ',
           OwnerId = UserInfo.getUserId(),
           WhatId = con.AccountId,
           RDA__c = TRUE)
           );
    }

//  Saves list of Tasks added
    public PageReference saveTask(){
        insert listTask;
        insert listTaskFollow;
            {
            PageReference contactPage = new PageReference('/' + con.Id);
            contactPage.setRedirect(true);
            return contactPage;
            }
    }
}

VF Page:

<apex:page standardController="Contact" tabStyle="Contact" sidebar="false" extensions="RDATasks" action="{!RDA}">

<apex:form >

<apex:sectionHeader title="Activities"/>
<apex:repeat value="{!listTask}" var="tsk">
    <apex:pageBlock >
        <apex:pageBlockSection >
            <apex:outputField value="{!Contact.Account.Name}" label="Related To"/>
            <apex:outputField value="{!Contact.Name}" label="Name"/>
            <apex:inputField value="{!tsk.Type_of_RDA__c}" style="width:35%"/>
            <apex:inputField value="{!tsk.Subject}" style="width:45%"/>
            <apex:inputField value="{!tsk.ActivityDate}" style="width:35%"/>
            <apex:inputField value="{!tsk.Status}" />
            <apex:inputField value="{!tsk.Description}" style="width:100%"/>
            <apex:commandButton value="Add Follow Up RDA" action="{!RDAfollow}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:repeat>

<apex:repeat value="{!listTaskFollow}" var="tskF">
    <apex:pageBlock >
        <apex:pageBlockSection >
            <apex:outputField value="{!Contact.Account.Name}" label="Related To"/>
            <apex:outputField value="{!Contact.Name}" label="Name"/>
            <apex:inputField value="{!tskF.Type_of_RDA__c}" style="width:35%"/>
            <apex:inputField value="{!tskF.Subject}" style="width:45%"/>
            <apex:inputField value="{!tskF.ActivityDate}" style="width:35%"/>
            <apex:inputField value="{!tskF.Status}" />
            <apex:inputField value="{!tskF.Description}" style="width:100%"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:repeat>

    <apex:commandButton value="Add Another RDA" action="{!RDA}"/>
    <apex:commandButton value="Save RDAs" action="{!saveTask}"/>
</apex:form>

</apex:page>
  • If I understand well, when the user click on "Add Another RDA", you want to add a new pageblocksection to the page with pre-filled data depending on the previous edited task by the user? – Martin Lezer Jan 23 '17 at 16:11
  • 1
    It's unclear to me where you're "stuck". What's working for you and what isn't? Please be more specific rather than just doing a code dump on us. What behavior do you want that you're not presently seeing? – crmprogdev Jan 23 '17 at 16:24
  • @MartinLezer -- It's actually when they click the "Add Follow-Up RDA" in the 1st pageBlockSection that I want another section to appear with data pre-filled from the data entered previously. – John Neilan Jan 23 '17 at 16:46
  • @crmprogdev -- Sorry for the vagueness. I am stuck at the line 16 command button of my VF page. When I open the page from a Contact initially, the data populates as expected from the RDA method in my controller. When the user clicks the command button on line 16, I want some of the same data they already entered to pre-populate the new page block section. I am not sure how to accomplish this. Does that make sense? – John Neilan Jan 23 '17 at 16:49
  • @MartinLezer -- Thanks. I thought I needed to use lists because the VF page is meant to allow the user to create multiple tasks (and follow-up tasks) and then save all at once. – John Neilan Jan 23 '17 at 16:59
1

You can add a show/hide attribute to the <pageBlock id="addedTasks" render="{!myRenderVar}"> in this section of code that's changes value when you initially click the "Save" button to set it to true as shown below. When true, the pageBlock renders, when false, it doesn't. You can set it in your controller or use an action attribute in your Button for "onClick".

<apex:repeat value="{!listTaskFollow}" var="tskF">
    <apex:pageBlock id="addedTasks" render="{!myRenderVar}">
        <apex:pageBlockSection >
            <apex:outputField value="{!Contact.Account.Name}" label="Related To"/>
            <apex:outputField value="{!Contact.Name}" label="Name"/>
            <apex:inputField value="{!tskF.Type_of_RDA__c}" style="width:35%"/>
            <apex:inputField value="{!tskF.Subject}" style="width:45%"/>
            <apex:inputField value="{!tskF.ActivityDate}" style="width:35%"/>
            <apex:inputField value="{!tskF.Status}" />
            <apex:inputField value="{!tskF.Description}" style="width:100%"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:repeat>

The question then becomes, do you want to keep the follow-up task separate? If not, in your controller, I recommend you consider changing

public list<Task> listTaskFollow{get;set;}

to

public Task taskFollow{get;set;}

since you presumably will only need to create one of these at a time. Once created, you should call a method that adds this follow-up task to your main list of Tasks. If not, you'll need to keep track of the length of listTaskFollow as you add new tasks to it, which brings to me where I suspect you may be having issues with populating follow-up tasks.

You've not said exactly what fields you want to add, but what you'll want to do is create a new Task that uses the fields from listTask[n] where integer n = listTask.size()-1.

Assuming you continue to use listfollowTask:

Set<Task>Tasks = new Set<Task>();
Set<Task>FollowTasks = new Set<Task>();

public PageReference saveTask(){
    insert listTask;
    insert TaskFollow;
    if(!TaskFollow.isEmpty()){
        Tasks.addAll(listTask);
        FollowTasks.addAll(TaskFollow);
        if(!Tasks.containsAll(FollowTasks)){
            integer last = TaskFollow.size();               
            listTask.add(TaskFollow[last];
            // assumes you only allow user to add one follow-up Task at a time
         }

    RDAfollow();
        {
        PageReference contactPage = new PageReference('/' + con.Id);
        contactPage.setRedirect(true);
        return contactPage;
        }

public void RDAfollow()
{
    n=listTask.size() - 1; // assumes you've already declared n as integer
    listTaskFollow.add(new Task(
       WhoId = con.Id,
       Status = 'In Progress',
       ActivityDate = system.TODAY()+7,
       Subject = 'Follow-Up: ',
       OwnerId = UserInfo.getUserId(),
       WhatId = con.AccountId,
       RDA__c = TRUE
       Subject = listTask[n].Subject;
       Comments = listTask[n].Comments; 
       )
       );
}
  • Thanks so much! That worked! I am running into other issues, but if I can't figure them out I'll post under a different question. Thanks! – John Neilan Jan 23 '17 at 19:28

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.