+ Start a Discussion

S-control to populate custom picklist field on task

I have a s-control button that creates a new task when clicked. I also want it to auto populate a custom picklist field when the task is created.


The code I have written is below, does anyone know what code I need to include to populate a custom field via this s-control? If this is not possible do you have any other suggestions?








Also, is there any specific reason I would want to do this with apex code rather than an s-control, if so any tips on how to get started. I have yet to implement any serious Apex code.





Use describeSObject for retriving all the pictlist value in your scontrol.


var objects = new Array();


Hello john022,

If you want to populate a custom field through the URL then you will need to find the ID of that field. If you look at the source code you will see the field name will be the 15 digit ID that Salesforce uses. Some times they include a CF at the beginning but I have not figured out the logic for this, some have it and some do not.

Apex will allow you to create and save the record without having to have the browser do the work. This means that it will not be controlled by permissions or be delayed by any transmission time. The s-control will rely on the browser to do what you want so it will take time translating between the SF system and the user's computer.

I hope this helps explain the reasons to use Apex, if not then please let us know specific questions and we should be able to answer.
CAUTION: the technique being described here is commonly known as "screen-scraping" which is an at-your-own-risk approach to customizing salesforce.com.  There is no guarantee that solutions such as these will continue to function as designed with any given major or minor release of salesforce.com. 

One reason you would want to drop to an Apex and Visualforce solution to this requirement is to avoid having to re-test and validate this solution with each release (including patch releases).  Another reason is that an apex/visualforce solution will be easier to maintain and debug.  Additionally you can add more logic to the process like conditionally setting the due date based on the user's task load (or vacation schedule). You could also add translation support to this example more easily with apex & Visualforce than with an scontrol or pure URL based solution.

For your reference I've converted your parameterized URL which counts on salesforce.com not changing things like "tsk5" as the form parameter for task subject to one which relies on parameter names of your choosing and an "API" that you maintain.

The custom button/link URL would look like this:


Which would call a Visualforce page named taskcreate with code that looks like this:

Visualforce Page:
<apex:page standardController="Task" extensions="taskCreateExt" action="{!doCreate}"/>

And an Apex class which extends the standard controller for task that does the work to create the two tasks - one which is complete and another for the follow up and redirects the user to the desired location:

Apex Code:
public class taskCreateExt {

    /* The constructor for standard controller extensions. */
    public taskCreateExt(ApexPages.StandardController controller) {
        taskCon = controller;
    /* This is the default action for the associated page which 
       creates the two tasks and directs the user back to the contact
       detail page from where this page/action was invoked. */
    public PageReference doCreate() {
        /* Get the request parameter map. */
        Map<String, String> paramMap = ApexPages.currentPage().getParameters();
        /* Get the task from the controller */
        Task call = (Task) taskCon.getRecord();
        /* Assign values from the request params */
        call.subject      = 'Left Voice Mail';
        call.description  = 'Left a voicemail for ' + paramMap.get('contact.name');
        call.whoid        = paramMap.get('task.whoid');
        call.whatid       = paramMap.get('task.whatid');
        call.activitydate = System.today();
        /* get the completed task status value. */
        TaskStatus ts     = [select masterlabel from taskstatus where isclosed = true limit 1];
        /* Set the status based on the taskstatus object. */
        call.status       = ts.masterlabel;
        /* Create the follow-up task */
        Task followup = new Task();
        /* Assign values from the request params */
        followup.subject      = 'Give ' + paramMap.get('contact.name') + ' at ' + paramMap.get('account.name') + ' a follow-up call';
        followup.activitydate = System.today().addDays(2);
        followup.whoid        = call.whoid;
        followup.whatid       = call.whatid;
        /* Create the task collection. */
        Task[] tasks = new Task[]{call, followup};
        /* Insert the tasks. */
        /* Get the pagereference for the contact detail */
        PageReference p = new ApexPages.standardController(new Contact(Id = call.whoid)).view();
        return p;
    /* The standard controller for task which this class extends. */
    private ApexPages.StandardController taskCon;    


Adding custom fields to this approach is simply a matter of adding relevant assignment to either or both of the task objects in the doCreate action method (call, followup) in the same manner as the standard field values are set, i.e. call.subject, followup.subject, etc.

If you want to try this out in your developer account, you'll need to create the apex class first.

I have a custom object that I store Phone # and Email addresses on for clients. When my users click on New Task or New Event, they want the Email address and Phone Number to pull onto the Tasks or Event the same way it does on the Contact object when creating a Task and Event.

Any suggestions?
Thank for your replies guys, you were so much help. I was able to figure out the name of the picklist field with the add-on Chris recommended which is awesome by the way. I made use of that in the meanwhile.
Andrew, I was able to implement your code in my developer account and am able to add custom fields.

Hi Crystal,


could you please provide your mobile number or mail as i also stuck with the same problem.

its very urgent requirement.

your help will sincerely appreciated.

Thanks in Advance


my email id is rpothadi@take-es.com


Hi John,



I need to populate the Email and phone number from leads object in to the new task page.

upon clicking the new task button under the open activities in the leads form,needs to display those email and phone number in the task page.


pls tell me how can this be possible.

your quick response will be highly appreciated.

Thanks in Advance.


Here is my email id rpothadi@take-es.com