1

I'm stuck with a really easy task.

My VF page have StandardController and Extension. I'm using the standard fields for Task object :

<apex:InputField value="{!Task.Client_Name__c}"/>

So I want to create my own custom button that will create Task record with all values that the user filled in.

This is my controller extension:

public with sharing class MultiselectExampleController {

public SelectOption[] selectedUsers { get; set; }
public SelectOption[] allUsers { get; set; }
public SelectOption[] selectedContacts { get; set; }
public SelectOption[] allContacts { get; set; }    
public SelectOption[] leftOptions { get; set; }
public SelectOption[] rightOptions { get; set; }
public Task task { get; set; }   
public String UserId { get; set; }
public String UserName { get; set; }
public String UserName2 { get; set; }
public String ContactName { get; set; }
public String ContactId { get; set; }
public String strPhone {get; set;}
public String strEmail {get; set;}
public String Status {get; set;} 

Task tas;
final ApexPages.StandardController stdController;

public MultiselectExampleController(ApexPages.StandardController stdController) {  
    strPhone = '';
    strEmail = '';
    Status = 'Completed';
    this.tas = (Task)stdcontroller.getRecord();
// Set the Assigned To field to the current UserId if OwnerId is blank
    if(tas.OwnerId == null) {
        string strOwnerId = UserInfo.getUserId();
        string strOwnerName = UserInfo.getName();
        UserId = strOwnerId;
        UserName = strOwnerName;
        UserName2 = strOwnerName;
// Set Name field to the current ContactId if Owner is blank            
        String conId = string.valueof(tas.WhoId);
        List<Contact> cn = [Select Id, Name FROM Contact where id =:conId];
            if(cn.size() > 0) {
                String ContID = cn[0].Id;
                String ContName = cn[0].Name;
                ContactName = string.valueof(ContName);
                ContactId = string.valueof(ContID);
            }
    }

// Set Phone and Email of the primary contact         
    if(tas.Who.Phone == null) {           
        if(string.valueof(tas.WhoId).startsWith('003')) {
        Contact obj = [SELECT Phone, Email FROM Contact WHERE Id=:tas.WhoId];
        //picking phone from Contact
    strPhone = obj.phone;
    strEmail = obj.Email;
        } else if(string.valueof(tas.WhoId).startsWith('00Q')) {
        Lead obj = [SELECT Phone, Email FROM Lead WHERE Id=:tas.WhoId];
        //picking phone from Lead
        strPhone = obj.phone;
        strEmail = obj.Email;
        } else {
        Account obj = [SELECT Phone FROM Account WHERE Id=:tas.WhoId];
        //picking phone from Account
        strPhone = obj.phone;
        strEmail = '';
        }         
    }            
    tas = (Task)stdController.getRecord();
    this.stdController = stdController; 
} 

//Select the list of Users and Contacts    
public MultiselectExampleController() {  

    selectedUsers = new List<SelectOption>();

    List<User> users = [SELECT Name, Id FROM User Order BY Name ASC];    
    allUsers = new List<SelectOption>();
    for ( User u : users ) {
        allUsers.add(new SelectOption(u.Id, u.Name));
    }

    selectedContacts = new List<SelectOption>();

    List<Contact> contacts = [SELECT Name, Id FROM Contact ORDER BY Name ASC];    
    allContacts = new List<SelectOption>();
    for ( Contact c : contacts ) {
        allContacts.add(new SelectOption(c.Id, c.Name));
    }
}  

// Parse &-separated values and labels from value and put them in option    
private void setOptions(SelectOption[] options, String value) {
    options.clear();
    String[] parts = value.split('&');
    for (Integer i=0; i<parts.size()/2; i++) {
        options.add(new SelectOption(EncodingUtil.urlDecode(parts[i*2], 'UTF-8'), 
          EncodingUtil.urlDecode(parts[(i*2)+1], 'UTF-8')));
    }
}

// Backing for hidden text field containing the options from the left list    
public String leftOptionsHidden { get; set {
       leftOptionsHidden = value;
       setOptions(leftOptions, value);
    }
}

// Backing for hidden text field containing the options from the right list   
public String rightOptionsHidden { get; set {
       rightOptionsHidden = value;
       setOptions(rightOptions, value);
    }
}

//Buttons

Public PageReference saveRecord() {
    tas.Status = Status;
    tas.OwnerId = UserId;
    tas.WhoId = ContactId;
    return new PageReference('/'+tas.Id);
    return null;
}

public PageReference save1() {
    UserName = '';
    UserId = '';
    Boolean first = true;       
    for ( SelectOption so : selectedUsers ) {
        if (!first) {
            UserName += ', ';
            UserId += ', ';                
        }
        UserName += so.getLabel();
        UserId += so.getValue();
        first = false;   
        PageReference pageRef = ApexPages.currentPage();

        pageRef.setRedirect(false);
    }       
    return null;    
} 

public PageReference save2() {
    ContactName = '';
    ContactId = '';
    Boolean first = true;       
    for ( SelectOption so : selectedContacts ) {
        if (!first) {
            ContactName += ', ';
            ContactId += ', ';                
        }
        ContactName += so.getLabel();
        ContactId += so.getValue();
        first = false;   
        PageReference pageRef = ApexPages.currentPage();

        pageRef.setRedirect(false);
    }       
    return null;    
}   
}

The method saveRecord works fine, but values that not defined in this method are not saved (Like Task.Client_Name__c). I want to save all values that was filled in VF page. I need something like this:

tas.Client_Name__c = {!Task.Client_Name__c}

How to do this?

  • 1
    Why do you have both public Task task { get; set; } and Task tas;? – battery.cord Jun 24 '16 at 14:41
  • @battery.cord, Fine... I thoiught that one variable used for vf page and second in the class. My mistake. Deleting the variable Task tas and replacing all tas with Task is an answer. – Viktor Jun 24 '16 at 15:06
2

It seems like you're using two separate variables in this class- Task tas, and public Task task { get; set; }. Since your saveRecords() method only references tas, only this object will be modified.

<apex:InputField value="{!Task.Client_Name__c}"/>

As you can see on your visualforce page, you are referencing the Task variable, not the tas variable, which is causing the behavior described in your post.

I would also be very careful about using both a StandardController with the type of Task, and a variable named the same as the variable provided by the controller inside the controller extension. Both of those variables would be named Task, which can lead to strange behavior when working with the objects on the page.

It's worth noting that I can't tell where your record is being saved, or which record is being saved. Since both the Task from the StandardController and the Task from your extension have the same name, either one of them could be the record used on the page, and in any save methods. I also am unable to determine which record you are saving, since I don't see any insert statements inside of your code, and can only assume you are using another method somewhere to save these records, which makes it harder to determine which record is being saved.

Modify the names of the records involved, and it will be much clearer which records are being used, and where.

| improve this answer | |
1

You have databound your field to the standardcontroller object

<apex:InputField value="{!Task.Client_Name__c}"/>

If you want to save values added in this field you can just do this

stdcontroller.save();

If you want to add some modifications before saving you can

Task t = (Task)stdcontroller.getRecord();
t.Status = Status;
t.OwnerId = UserId;
t.WhoId = ContactId;
stdcontroller.save();

Also checkout the standard controller actions: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_std_actions.htm

| improve this answer | |
  • Thank you for a solution, but this is doesn't work. All other fields are blank after save. – Viktor Jun 24 '16 at 14:16
  • I modified the method to: Public PageReference saveRecord() { tas.Status = Status; tas.OwnerId = UserId; tas.WhoId = ContactId; stdcontroller.save(); insert tas; return new PageReference('/'+tas.Id); } – Viktor Jun 24 '16 at 14:21
  • Also i mentioned, if I used just stdcontroller.save(); return null; the values filled in on the vf page also not saved in the created record. – Viktor Jun 24 '16 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.