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?
public Task task { get; set; }
andTask tas;
? – battery.cord Jun 24 '16 at 14:41