+ Start a Discussion
Eric FortenberryEric Fortenberry 

Task.Who.Phone and .Email

Does anyone know if there's any sort of reason why I wouldn't be able to render Task.Who.Phone and Task.Who.Email on a visualforce page? I tried to use an outputfield, but nothing shows up. Any ideas or guidance would be appreciated.
Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

In your controller define properties and set them:

public with sharing class TaskExtension {
    
    //Instance varaibles
    
    private ApexPages.StandardController controller {get; set;}
    //Adding Properties for collecting Phone and Email
    public String strPhone {get; set;}
    public String strEmail {get; set;}    
	public List<AggregateResult> lstAR = new List<AggregateResult>();
    Task t = new Task();
    
    // Inner class variables
    
    class TaskClass
    {        
        public Integer Total
        {get; set;}
        
        public string ActivityDate
        {get; set;}
              
        public TaskClass(DateTime d, Integer i){
            Total = i;
            ActivityDate = d.formatGMT('E').left(1) + '\n' + d.formatGMT('d');
        }
    }
    
    // Standard controller initiator 
    
    public TaskExtension(ApexPages.StandardController controller)
    {    
	strPhone = '';
	strEmail = '';
        this.t = (Task)controller.getRecord();
        
        // Set the Assigned To field to the current UserId if OwnerId is blank
        
        if(t.OwnerId == null) {
            string strOwnerId = UserInfo.getUserId();
            t.OwnerId = strOwnerId;
        } 

        // Set WhoId to null if the WhoId url parameter starts with '001'
                
        if(t.WhoId != null) {
            if(string.valueof(t.WhoId).startsWith('001')) {
                t.WhoId = null;
            }
        }
        
        // Set WhoId to the primary contact for opportunities
        
        if(t.WhoId == null && t.WhatId != null) {
        	if( string.valueof(t.WhatId).startsWith('006')) {
                String oppId = string.valueof(t.WhatId);
                       
                List<OpportunityContactRole> oppContRole = [SELECT ContactId FROM OpportunityContactRole 
                                                            WHERE OpportunityId = :oppId AND IsPrimary = true];
                if(oppContRole.size() > 0) {
                	Id primaryContactId = oppContRole[0].contactid;
                    t.WhoId = string.valueof(primaryContactId);
                }
        	}
        }
        
        if(t.Subject == null) {
           t.Subject = ApexPages.currentPage().getParameters().get('tsk5');
        }
        
        if(t.Who.Phone == null) {        	
            if(string.valueof(t.WhoId).startsWith('003')) {
        	Contact obj = [SELECT Phone FROM Contact WHERE Id=:t.WhoId];
        	//picking phone from Contact
		strPhone = obj.phone;
            } else if(string.valueof(t.WhoId).startsWith('00Q')) {
        	Lead obj = [SELECT Phone FROM Lead WHERE Id=:t.WhoId];
        	//picking phone from Lead
        	strPhone = obj.phone;
            } else {
        	Account obj = [SELECT Phone FROM Account WHERE Id=:t.WhoId];
        	//picking phone from Account
        	strPhone = obj.phone;
            }
	}
        
...

 and in your controller refer to the two properties:

 

<apex:pageBlockSectionItem>
	<apex:outputText value="Phone"/>
	<apex:outputfield value="{!strPhone}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem>
	<apex:outputText value="Email"/>
	<apex:outputfield value="{!strEmail}"/>
</apex:pageBlockSectionItem>

 

All Answers

Rahul SharmaRahul Sharma

Hey Eric,

It might be the case because whoid is refering to more than one objects (Contact or Lead).

Due to which page is unable to fetch value from exact object. Seems you need to write custom code for the functionality.

Eric FortenberryEric Fortenberry

Here is the custom code I have, but I'm not sure I'm defining the phone field correctly and I'm not sure how to access it from the visualforce page. 

 

Custom controller code (see last block of code):

public with sharing class TaskExtension {
    
    //Instance varaibles
    
    private ApexPages.StandardController controller {get; set;}
    public List<AggregateResult> lstAR = new List<AggregateResult>();
    Task t = new Task();
    
    // Inner class variables
    
    class TaskClass
    {        
        public Integer Total
        {get; set;}
        
        public string ActivityDate
        {get; set;}
              
        public TaskClass(DateTime d, Integer i){
            Total = i;
            ActivityDate = d.formatGMT('E').left(1) + '\n' + d.formatGMT('d');
        }
    }
    
    // Standard controller initiator 
    
    public TaskExtension(ApexPages.StandardController controller)
    {       
        this.t = (Task)controller.getRecord();
        
        // Set the Assigned To field to the current UserId if OwnerId is blank
        
        if(t.OwnerId == null) {
            string strOwnerId = UserInfo.getUserId();
            t.OwnerId = strOwnerId;
        } 

        // Set WhoId to null if the WhoId url parameter starts with '001'
                
        if(t.WhoId != null) {
            if(string.valueof(t.WhoId).startsWith('001')) {
                t.WhoId = null;
            }
        }
        
        // Set WhoId to the primary contact for opportunities
        
        if(t.WhoId == null && t.WhatId != null) {
        	if( string.valueof(t.WhatId).startsWith('006')) {
                String oppId = string.valueof(t.WhatId);
                       
                List<OpportunityContactRole> oppContRole = [SELECT ContactId FROM OpportunityContactRole 
                                                            WHERE OpportunityId = :oppId AND IsPrimary = true];
                if(oppContRole.size() > 0) {
                	Id primaryContactId = oppContRole[0].contactid;
                    t.WhoId = string.valueof(primaryContactId);
                }
        	}
        }
        
        if(t.Subject == null) {
           t.Subject = ApexPages.currentPage().getParameters().get('tsk5');
        }
        
        if(t.Who.Phone == null) {        	
            string <phone>phone;
            if(string.valueof(t.WhoId).startsWith('003')) {
        	Contact obj = [SELECT Phone FROM Contact WHERE Id=:t.WhoId];
        	phone = obj.phone;
            } else if(string.valueof(t.WhoId).startsWith('00Q')) {
        	Lead obj = [SELECT Phone FROM Lead WHERE Id=:t.WhoId];
        	phone = obj.phone;
            } else {
        	Account obj = [SELECT Phone FROM Account WHERE Id=:t.WhoId];
        	phone = obj.phone;
            }
	}
        
...

 

Visualforce code trying to access the phone field:

<apex:outputfield value="{!Task.Who.Phone}"/>

 

Entire Visualforce Page:

<apex:page standardcontroller="Task" extensions="TaskExtension" sidebar="true" showHeader="true" title="{!IF(ISNULL(Task.subject),'New Task',Task.subject)}"> 
    <style type="text/css">
        .multiLineText { width: 400px; height: 90px; }
    </style>
    <apex:sectionheader title="{!$ObjectType.Task.label} Edit" subtitle="{!IF(ISNULL(Task.subject),'New Task',Task.subject)}"/>
    <apex:form >
        <apex:pageblock mode="edit" title="{!$ObjectType.Task.label} Edit">
            <apex:pageblockbuttons >
                <apex:commandbutton value="Save" action="{!save}"/>
                <apex:commandbutton value="Save & New Task" action="{!saveNewTask}"/>
                <apex:commandbutton value="Cancel" action="{!Cancel}"/>
            </apex:pageblockbuttons>

            <!-- ********** Output Panel for Record Type : Task **********  -->
            <apex:outputpanel rendered="{!OR(ISNULL(Task.RecordTypeId),Task.RecordTypeId='01270000000HjWXAA0')}">
                <apex:pageblocksection title="Task Information" columns="2">
                    <apex:inputfield value="{!Task.WhoId}" required="true"/>
                    <apex:inputfield value="{!Task.WhatId}" required="false"/>
                    <apex:inputfield value="{!Task.Type}" required="true"/>
                    <apex:inputfield value="{!Task.OwnerId}" required="true"/>
                    <apex:inputfield value="{!Task.ActivityDate}" required="true"/>
                    <apex:inputfield value="{!Task.Priority}" required="true"/>
                    <apex:inputfield value="{!Task.Status}" required="true"/>
                    <apex:outputfield value="{!Task.Who.Phone}"/>
                    <apex:inputfield value="{!Task.Held_Conversation__c}"/>
                    <apex:outputfield value="{!Task.Who.Email}"/>
                    <apex:inputfield value="{!Task.Subject}" required="true"/>
                </apex:pageblocksection>
                <apex:pageblockSection >
                    <apex:inputfield value="{!Task.Call_Notes__c}" styleClass="multiLineText" required="false"/>
                    <apex:inputfield value="{!Task.Description}" styleClass="multiLineText" required="false"/>
                </apex:pageblocksection>
                <apex:pageBlockSection title="Future Tasks">
                    <apex:chart height="250" width="1200" data="{!results}">
                        <apex:axis type="Category" position="bottom" fields="ActivityDate" title="Day of Month">
                            <apex:chartLabel orientation="vertical"/>
                        </apex:axis>
                        <apex:axis type="Numeric" position="left" fields="Total" title="Open Tasks" maximum="50"/>
                        <apex:barSeries axis="bottom" orientation="vertical" xField="ActivityDate" yField="Total">
                            <apex:chartLabel display="outside" />
                        </apex:barSeries>    
                    </apex:chart>
                </apex:pageBlockSection>
            </apex:outputpanel>
        </apex:pageblock>
    </apex:form>
    <apex:pageBlock >
        <apex:relatedList list="Attachments"/>
    </apex:pageBlock>
    <script>
     var url = location.href;
     var match = url.match(/inline=1/);
        if (match != null) { 
        var newUrl = url.replace(/inline=1/, '');
        window.top.location=newUrl;
    }    
    </script>
</apex:page>

 

 

Rahul SharmaRahul Sharma

Eric,

 

Your appoach is correct . But you need to define two properties in your extension for holding email and phone of reference field, use them in visualforce page to display fields values.

Eric FortenberryEric Fortenberry

Rahul,

 

Would you mind elaborating a little more?  I was attempting to have phone be the property (variable) used to store the phone, but then I wasn't sure how to access that in the visualforce page.  I understand I can't use Task.Who.Phone, but anything else I tried to plug in gave me an error saying it wasn't a Task SObject.

 

Thanks for your help.

Rahul SharmaRahul Sharma

In your controller define properties and set them:

public with sharing class TaskExtension {
    
    //Instance varaibles
    
    private ApexPages.StandardController controller {get; set;}
    //Adding Properties for collecting Phone and Email
    public String strPhone {get; set;}
    public String strEmail {get; set;}    
	public List<AggregateResult> lstAR = new List<AggregateResult>();
    Task t = new Task();
    
    // Inner class variables
    
    class TaskClass
    {        
        public Integer Total
        {get; set;}
        
        public string ActivityDate
        {get; set;}
              
        public TaskClass(DateTime d, Integer i){
            Total = i;
            ActivityDate = d.formatGMT('E').left(1) + '\n' + d.formatGMT('d');
        }
    }
    
    // Standard controller initiator 
    
    public TaskExtension(ApexPages.StandardController controller)
    {    
	strPhone = '';
	strEmail = '';
        this.t = (Task)controller.getRecord();
        
        // Set the Assigned To field to the current UserId if OwnerId is blank
        
        if(t.OwnerId == null) {
            string strOwnerId = UserInfo.getUserId();
            t.OwnerId = strOwnerId;
        } 

        // Set WhoId to null if the WhoId url parameter starts with '001'
                
        if(t.WhoId != null) {
            if(string.valueof(t.WhoId).startsWith('001')) {
                t.WhoId = null;
            }
        }
        
        // Set WhoId to the primary contact for opportunities
        
        if(t.WhoId == null && t.WhatId != null) {
        	if( string.valueof(t.WhatId).startsWith('006')) {
                String oppId = string.valueof(t.WhatId);
                       
                List<OpportunityContactRole> oppContRole = [SELECT ContactId FROM OpportunityContactRole 
                                                            WHERE OpportunityId = :oppId AND IsPrimary = true];
                if(oppContRole.size() > 0) {
                	Id primaryContactId = oppContRole[0].contactid;
                    t.WhoId = string.valueof(primaryContactId);
                }
        	}
        }
        
        if(t.Subject == null) {
           t.Subject = ApexPages.currentPage().getParameters().get('tsk5');
        }
        
        if(t.Who.Phone == null) {        	
            if(string.valueof(t.WhoId).startsWith('003')) {
        	Contact obj = [SELECT Phone FROM Contact WHERE Id=:t.WhoId];
        	//picking phone from Contact
		strPhone = obj.phone;
            } else if(string.valueof(t.WhoId).startsWith('00Q')) {
        	Lead obj = [SELECT Phone FROM Lead WHERE Id=:t.WhoId];
        	//picking phone from Lead
        	strPhone = obj.phone;
            } else {
        	Account obj = [SELECT Phone FROM Account WHERE Id=:t.WhoId];
        	//picking phone from Account
        	strPhone = obj.phone;
            }
	}
        
...

 and in your controller refer to the two properties:

 

<apex:pageBlockSectionItem>
	<apex:outputText value="Phone"/>
	<apex:outputfield value="{!strPhone}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem>
	<apex:outputText value="Email"/>
	<apex:outputfield value="{!strEmail}"/>
</apex:pageBlockSectionItem>

 

This was selected as the best answer