+ Start a Discussion
Rakesh SRakesh S 

How to add a user as default to "Assign To" field while creating a task


Hi All,
I have created a page for Physician__c and it has Communication History as a related list.
And it has "Log a Call" button(Custom).

when i click on "Log a Call" button then it goes to new Task page(Custom).
And this page has the following fields like Assign To, Date, Type,Reminder Date etc...
I entered all fields and save it then successfully saved and added it in communication History.

But problem is that In New Task page, i need current user as default in "Assign To" field
and it should be lookup only(it may change).

When i am using Standard button then it works fine but its not my requirement.
Please give appropriate solution for this. Let me know, If any body not understanding.
User-added image


Thank you
Rakesh
Best Answer chosen by Rakesh S
William TranWilliam Tran
Rakesh,
 
There are so many things wrong with your code :-)  but that's okay.

Here's a working version, 

Just create a new apex page, cut & paste the VF page and controller, enter in some value and save.

It should work right away, 

Now, you need to add back the Date__c since I don't have that so I removed it.
 And the uncomment tsk.Whatid=WhatId; 
Thx


Controller
public with sharing class PhysicianController {

    public Task tsk{get;set;}
    public String WhatId{get;set;}  
    Private ApexPages.StandardController con;
    
    public PhysicianController (ApexPages.StandardController controller) {
        
        this.con = controller;
        WhatId  = ApexPages.currentPage().getParameters().get('what_id');
        tsk = new task();
        tsk.OwnerId = UserInfo.getUserId();
    }
    
    public PageReference save(){

          tsk.Status = 'Completed';
          //t.Whatid=WhatId;
          insert tsk;

        PageReference page = new PageReference('/'+WhatId);
        page.setRedirect(true);
        return page;
    }
}




Visualforce Page
<apex:page standardController="Task" extensions="PhysicianController">

    <apex:sectionHeader title="Log a Call"/>
    <apex:form >
        <apex:pageBlock title="Task Edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!Save}" value="Save"/>
                <apex:commandButton action="{!Cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
             <br/>
             <apex:pageblockSection title="Task Information"  id="pbs1"  collapsible="false">
            
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Assigned To" for="usr"/>
                    <apex:inputField value="{!Tsk.OwnerId}" id="usr" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Status" for="st" />
                    <apex:outputtext value="Completed" id="st" />
                </apex:pageBlockSectionItem>
                

                
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Type" for="type"/>
                    <apex:inputField value="{!Tsk.Type}" id="type"/>
                </apex:pageBlockSectionItem>
                                                     
            </apex:pageblockSection>
            <apex:pageBlockSection title="Items Discussed"  id="pbs2"  collapsible="false">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Comments" for="cms"/>
                    <apex:inputField value="{!Tsk.Description}" id="cms"/>
                </apex:pageBlockSectionItem>
                 
            </apex:pageBlockSection>
            <apex:pageblockSection title="Followup" columns="2"  id="pbs3"  collapsible="false">
            
             <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Follow up" for="flp"/>
                    <apex:inputcheckbox value="{!Tsk.isReminderset}" id="flp"  label="Follow up"/>
                </apex:pageBlockSectionItem>
                 <apex:pageBlockSectionItem >
                    
                </apex:pageBlockSectionItem>
                 <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Follow up Date" for="flpDate"/>
                   <apex:inputfield value="{!Tsk.ReminderDateTime }"  id="flpDate"/>
                </apex:pageBlockSectionItem>
                
         <br/>
             
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

All Answers

William TranWilliam Tran
Can you post the code for the custom button?

The custom page is a Visualforce page?  Standard controller? any extensions?

Thx
Rakesh SRakesh S
public with sharing class PhysicianController {

    public Task tsk{get;set;}
    public String WhatId{get;set;}  
    Private ApexPages.StandardController con;
    
    public LogACallControllerPhysician(ApexPages.StandardController controller) {
        
        this.con = controller;
        WhatId  = ApexPages.currentPage().getParameters().get('what_id');
        //system.debug('student id :'+Id);
    }
    
    public PageReference save(){
        con.save();
        
        tsk = (Task)con.getRecord();
        
        system.debug('!!!!!'+tsk.ReminderDateTime);
        system.debug('chk box: '+tsk.isReminderSet);
        Task t = new Task();
          t.Type = tsk.Type;
         // t.Subject = tsk.Subject;
          t.Date__c = tsk.Date__c;
          t.Status = 'Completed';
          t.OwnerId = tsk.OwnerId;
          t.Description = tsk.Description;
          t.Whatid=WhatId;
          t.isReminderSet = tsk.isReminderSet;
          t.ReminderDateTime = tsk.ReminderDateTime;
          insert t;
        
        PageReference page = new PageReference('/'+WhatId);
        page.setRedirect(true);
        return page;
    }
}
 
<apex:page standardController="Task" extensions="PhysicianController">

    <apex:sectionHeader title="Log a Call"/>
    <apex:form >
        <apex:pageBlock title="Task Edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!Save}" value="Save"/>
                <apex:commandButton action="{!Cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
             <br/>
             <apex:pageblockSection title="Task Information"  id="pbs1"  collapsible="false">
            
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Assigned To" for="usr"/>
                    <apex:inputField value="{!Task.OwnerId}" id="usr"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Status" for="st" />
                    <apex:outputtext value="Completed" id="st" />
                </apex:pageBlockSectionItem>
                
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Date" for="dt"/>
                    <apex:inputField value="{!Task.Date__c}" id="dt"/>
                </apex:pageBlockSectionItem>
                
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Type" for="type"/>
                    <apex:inputField value="{!Task.Type}" id="type"/>
                </apex:pageBlockSectionItem>
                                                     
            </apex:pageblockSection>
            <apex:pageBlockSection title="Items Discussed"  id="pbs2"  collapsible="false">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Comments" for="cms"/>
                    <apex:inputField value="{!Task.Description}" id="cms"/>
                </apex:pageBlockSectionItem>
                 
            </apex:pageBlockSection>
            <apex:pageblockSection title="Followup" columns="2"  id="pbs3"  collapsible="false">
            
             <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Follow up" for="flp"/>
                    <apex:inputcheckbox value="{!Task.isReminderset}" id="flp"  label="Follow up"/>
                </apex:pageBlockSectionItem>
                 <apex:pageBlockSectionItem >
                    
                </apex:pageBlockSectionItem>
                 <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Follow up Date" for="flpDate"/>
                   <apex:inputfield value="{!Task.ReminderDateTime }"  id="flpDate"/>
                </apex:pageBlockSectionItem>
                
         <br/>
             
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Please give me solution with this.


​Thank you..
William TranWilliam Tran
Rakesh,
 
There are so many things wrong with your code :-)  but that's okay.

Here's a working version, 

Just create a new apex page, cut & paste the VF page and controller, enter in some value and save.

It should work right away, 

Now, you need to add back the Date__c since I don't have that so I removed it.
 And the uncomment tsk.Whatid=WhatId; 
Thx


Controller
public with sharing class PhysicianController {

    public Task tsk{get;set;}
    public String WhatId{get;set;}  
    Private ApexPages.StandardController con;
    
    public PhysicianController (ApexPages.StandardController controller) {
        
        this.con = controller;
        WhatId  = ApexPages.currentPage().getParameters().get('what_id');
        tsk = new task();
        tsk.OwnerId = UserInfo.getUserId();
    }
    
    public PageReference save(){

          tsk.Status = 'Completed';
          //t.Whatid=WhatId;
          insert tsk;

        PageReference page = new PageReference('/'+WhatId);
        page.setRedirect(true);
        return page;
    }
}




Visualforce Page
<apex:page standardController="Task" extensions="PhysicianController">

    <apex:sectionHeader title="Log a Call"/>
    <apex:form >
        <apex:pageBlock title="Task Edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!Save}" value="Save"/>
                <apex:commandButton action="{!Cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
             <br/>
             <apex:pageblockSection title="Task Information"  id="pbs1"  collapsible="false">
            
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Assigned To" for="usr"/>
                    <apex:inputField value="{!Tsk.OwnerId}" id="usr" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Status" for="st" />
                    <apex:outputtext value="Completed" id="st" />
                </apex:pageBlockSectionItem>
                

                
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Type" for="type"/>
                    <apex:inputField value="{!Tsk.Type}" id="type"/>
                </apex:pageBlockSectionItem>
                                                     
            </apex:pageblockSection>
            <apex:pageBlockSection title="Items Discussed"  id="pbs2"  collapsible="false">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Comments" for="cms"/>
                    <apex:inputField value="{!Tsk.Description}" id="cms"/>
                </apex:pageBlockSectionItem>
                 
            </apex:pageBlockSection>
            <apex:pageblockSection title="Followup" columns="2"  id="pbs3"  collapsible="false">
            
             <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Follow up" for="flp"/>
                    <apex:inputcheckbox value="{!Tsk.isReminderset}" id="flp"  label="Follow up"/>
                </apex:pageBlockSectionItem>
                 <apex:pageBlockSectionItem >
                    
                </apex:pageBlockSectionItem>
                 <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Follow up Date" for="flpDate"/>
                   <apex:inputfield value="{!Tsk.ReminderDateTime }"  id="flpDate"/>
                </apex:pageBlockSectionItem>
                
         <br/>
             
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
This was selected as the best answer