function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ERP TeamERP Team 

Pre-populate WhatId in New Task button

Hi All,

I have a Visualforce page on the Account Object
I want to add a button to create a new task for the current account I want to pre-populate the what id field but it's not working

My code : 
VF : 
<apex:page standardController="Account" extensions="ContactRelatedListController" sidebar="true" showHeader="false" >     
    <apex:pageblock > 
           <apex:outputLabel value="{!Account.Name}" styleClass="Account"> 
           </apex:outputLabel> 	
<apex:commandLink value="New Task" action="{!newTask}" target="_parent" styleClass="btn" style="text-decoration:none;padding:4px;"/></div> 

    </apex:pageblock> 

</apex:page>
Controller : 
public class ContactRelatedListController {   
public Account accounts {get;set;} 
    public Account acc {get;set;} 

    public ContactRelatedListController(ApexPages.StandardController controller) { 
        acc = (account)controller.getRecord();      
	    accounts = [SELECT id FROM account WHERE id=: acc.id LIMIT 1];
}

public Pagereference newTask(){
 
    PageReference pageRef = new PageReference('/00T/e?&WhatId=' + accounts.Id+'&retURL='+accounts.Id);

    pageRef.setRedirect(True);

    return PageRef;

    }
}
Thanks for your help
Hemdene.

 
Best Answer chosen by ERP Team
ShirishaShirisha (Salesforce Developers) 
Hi,

Then,you will have to query the Task record and update the record with the Parent Id before update/after Update instead of using before insert/after insert.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi,

Greetings!

Please find the sample code in the below blog which will help you in achieving your requirement.

https://techman97.wordpress.com/2011/10/22/creating-a-new-event-or-task-with-prepopulated-information/

Here,you can replace the custom Object with the Account as you are trying to create the new Task on the Account Object.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
ERP TeamERP Team
Hi Shirisha,

Thank you for you rreply,
This is helpful but what I want is to display the new Task window with the whatId prepopulated with the current account I don't want to actually insert the task I want to be able to add more infos into it

Thanks,
Hemdène.
ShirishaShirisha (Salesforce Developers) 
Hi,

Then,you will have to query the Task record and update the record with the Parent Id before update/after Update instead of using before insert/after insert.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
This was selected as the best answer
ERP TeamERP Team
Hi Shirisha,

Thank you for your help
It worked for me

Best Regards,
Hemdène.