+ Start a Discussion
Adam MarksAdam Marks 

Pass WhoId to Visualforce Task

So I figured this would be pretty simple but so far the answer has eluded me. Basically I created a VF page for a very specific Task type that is a Question/Answer format. This page will only ever be launched from a Lead through a custom button but I cannot for the life of me determine how to populate the WhoId automatically. I found this solution (https://developer.salesforce.com/forums?id=906F0000000960aIAA) but I think it's missing something because it did not work for me. Hopefully it's a relatively easy solution, seems like it would be.
This is my VF code (don't laugh)
<apex:page StandardController="Task" extensions="LeadQuestionnaireController">
    <apex:form >
        <apex:pageBlock title="Lead Questionnaire" mode="edit">
            <apex:pageblockbuttons >
                <apex:commandButton action="{!save}" value="Save"/>
                <apex:commandButton action="{!cancel}" value="Cancel"/>
                </apex:pageblockbuttons>
                <apex:pageblocksection >
                <apex:inputfield value="{!Task.WhoId}"/>
                </apex:pageblocksection>
                
                <apex:pageBlockSection title="Please answer questions below" Columns="2">
                    
                    <apex:outputText value="1. Do you make the final decision when it comes to selecting a carrier for your upcoming shipment to XYZ event?">
                    </apex:outputText>
                    <apex:inputField label=" " value="{!Task.A1__c}"/>
                    <apex:outputText value="2. Do you typically ship more than 300 lbs to this event?  Or will be planning to ship this amount in the event this is their first time to the show.">
                    </apex:outputText>
                    <apex:inputField label=" " value="{!Task.A2__c}"/>
                    <apex:outputText value="3. How do you decide or what criteria do you use to choose a shipping service to handle your show shipments?">
                    </apex:outputText>
                    <apex:inputField label=" " value="{!Task.A3__c}" style="width: 300px; height: 40px;"/>
                    <apex:outputText value="4. Would you have any interest in considering our service for this upcoming event?">
                    </apex:outputText>
                    <apex:inputField label=" " value="{!Task.A4__c}"/>
                    <apex:outputText value="5. How many shows over a year do you typically ship to?">
                    </apex:outputText>
                    <apex:inputField label=" " value="{!Task.A5__c}"/>
            <apex:outputText value="6. If not interested at this time, when do you typically evaluate your carriers?">
            </apex:outputText>
            <apex:inputField label=" " value="{!Task.A6__c}" style="width: 300px; height: 40px;"/>
            </apex:pageblocksection>
            
            <apex:pageBlockSection title="Comments" Columns="1">
                <apex:inputtextarea label=" " value="{!Task.Description}" style="width: 500px; height: 150px;"/>
                
            </apex:pageblocksection>
          </apex:pageBlock>
        </apex:form>
</apex:page>

This is the Extension, which I know is wrong.
public with sharing class LeadQuestionnaireController{
Task task = new Task();
    
    public LeadQuestionnaireController(ApexPages.StandardController controller) {
      this.task = (Task)controller.getRecord();
      string strWhatId = ApexPages.currentPage().getParameters().get('oppIdd');
      string strOwnerId = ApexPages.currentPage().getParameters().get('ownerId');
      Task.WhatId = strWhatId ;
      Task.OwnerId = strOwnerId ;
        
    }

}
Thanks in advance.
KevinPKevinP
Looking here: https://www.salesforce.com/us/developer/docs/api/Content/sforce_api_erd_activities.htm it seems like a lead is connected to task via whoID, so it looks like when your creating your task in your extension, you want to set WhoId = Lead.Id. 

I believe (given what you've posted here) that you want something like this for your extesion:
Public with sharing Class leadQuestionaireControllerExtension{
  
  Public Task task{get; private set;}

  Public LeadQuestionaireControllerExtension(ApexPages.StandardController controller){
    task = new Task();
    // since this is an extension on Lead (right?) this will get hte lead id.
    task.WhoId = controller.getId();
    task.WhatId = ApexPages.currentPage().getParameters().get('oppId');
    task.OwnerId = ApexPages.currentPage().getParameters().get('ownerId');
  }
}

Key here is line 3, which creates a public property on the controller with a get method and a private set method. (you may find you want it as a public set method). This will give you a merge obj on your VF page for {!task.whoId}.

Additionally, as i understand it, it since this is an extension on lead, the who ID will be the ID of the lead you're working with. Hence the call to getId on the main controller variable.

Adam MarksAdam Marks
Kevin, thank you for the reply. The standard controller is actually for a task since the page is basically a custom log a call script. I have a button on my Lead record that simply opens the URL to the page. What changes would I need to make to the controller to get this to work? 
Andy BoettcherAndy Boettcher
Adam - actually I think you're almost there man.  You're already doing the OwnerId and WhatId - just do the same thing for the WhoId - or is there something else not working?