24
votes
Accepted
How to set record type of records while saving the records?
SUMMER '18 UPDATE
You can now use the getRecordTypeInfosByDeveloperName() to get a Record Type Id by Developer Name without spending a SOQL query. This should be the preferred approach going forward ...
10
votes
Build a String for Database.query
The best way to avoid this problem is to use binding syntax if you need to use dynamic SOQL - it is easier to get right and cleaner to read. Importantly it also eliminates the risk of a SOQL Injection ...
9
votes
Accepted
How to Write Test Class for Standard Controller Along With Extensions
When you use ApexPages.currentPage().getParameters().get('Id');
You need to set Test.setCurrentPage(pageRef); and pageRef.getParameters().put('Id', String.valueOf(obj.Id));
PageReference pageRef = ...
9
votes
Accepted
Variable is not visible
You cannot assign to a property
AccLinks = (kvl != null && kvl.Value__c != null ? kvl.Value__c : 'some_url');
if that property does not have a setter method declared. To make the property ...
7
votes
Accepted
How do I check CRUD and FLS in apex?
Please refer to this knowledge article.
Basically you just need to add some checks like
if (!Schema.sObjectType.Contact.fields.Name.isAccessible()){
return '';
}
or
if (!Schema....
7
votes
Accepted
Redirect to another VF page while using a Same Controller?
The page's view state is preserved when the following conditions hold:
The new page uses the same controller as the old page.
The new page uses a subset of the extensions as the old page.
The ...
7
votes
Accepted
Can you output map values in visualforce inline using a string key?
You have a local variable called map in your constructor that hides the map property. (See e.g. Variable shadowing for more about that.)
Change:
Map<String, Custom_Object__c> map = new Map<...
6
votes
Accepted
SObject row was retrieved via SOQL without querying the requested field: Opportunity.OwnerId
When using a StandardController, there are a few different ways to manipulate the way it queries for the record. Note your line in the constructor:
opp = (Opportunity)stdController.getRecord();
The ...
6
votes
Accepted
Queues, Email Notifications, Standard Controller Extensions. Oh My!
Congratulations. You found one of the ways that StandardController (and StandardSetController) is broken (at least, for now). I've been trying to get a straight answer about why it's as broken as it ...
5
votes
Accepted
How to write the superduper (=proper and stable) Custom Save method
I wouldn't necessarily call it the perfect solution, but here are some ideas I've used to make my code more maintainable.
Separate your Visualforce action method from your save method that performs ...
5
votes
Accepted
Visualforce Page Error
This one is easy to hit
integer last = listTaskFollow.size(); // returns 1 if one member in list
listTask.add(listTaskFollow[last]); // index[1] should be index[0] when list has 1 element
You ...
5
votes
Formula fields not auto populating -- until after insert -- need a workaround
You can't evaluate cross-object formulas on a record that does not exist in the database. You'll have to insert it and then query the record back. One "trick" that I've used in the past is to insert a ...
5
votes
Accepted
get set from controller extension to visualforce page
You are setting a local variable rather than your controller property.
Change:
Set<String> myProperty { get; set; }
public MyExtension(ApexPages.StandardController controller)
{
Set<...
5
votes
Accepted
Unknown constructor
You need a recordSetVar="accounts" attribute on your <apex:page> tag. Otherwise, the Visualforce system believes you to be using an ApexPages.StandardController rather than an ApexPages....
5
votes
Accepted
Add Custom Error Message to VF Page
Your command button is calling the standard controller save. You need to use your custom action in the save button, shown below:
<apex:commandButton value="Save" action="{!onSave}"/>
5
votes
Accepted
Variable does not exist in method
searchDir is an instance variable. It is bound to a specific instance of the class Direccion - here, the controller extension.
static methods, such as @RemoteAction JavaScript remoting actions, aren'...
5
votes
Accepted
How to generate pptx files with the VisualForce page and Apex Controller
As well as labeling the content that your controller returns as of a specific content-type, you actually have to format the data to match. For PPTX that is a zipped set of XML files, which given that ...
5
votes
Accepted
How to fetch apex:input field value into extension class
The ApexPages.currentPage().getParameters() call allows you to access URL parameters supplied to your page. It has nothing to do with values bound to components on your page.
Your input fields are ...
4
votes
Accepted
Is apex getter method runs only once regardless of how many times it is called on VF page?
I have observed Apex properties - a convenient way to implement getters and setters - only being referenced once even though the Visualforce includes multiple references. It seems like there is some ...
4
votes
Unknown Property on Controller Extension
Your properties need a getter to be defined. While the approach mentioned by @Ratan will work, you can also "lazy load" your results in a way that does not consume a new query every time. Here is a ...
4
votes
Accepted
Comparing the duplicate value in VF page
If you want just unique values, consider using AggregateResult instead:
public AggregateResult[] getSkillReview() {
return [SELECT Skill__c Skill FROM Skill_Review__c WHERE Interview__c = :review....
4
votes
Accepted
How to save fields from visual force page with extension as controller
The record you receive in your constructor is where the changes made by the user will appear. In the other question, you query the record back from the database, thus ignoring the user's input. ...
4
votes
Accepted
Controller Error: expecting right curly bracket, found 'for' at line 12 column 8
Your troubles start here:
public class getData{
Which I presume you meant to be some sort of method, based on its name:
public Spend[] getData() {
This is important, because only functions can ...
4
votes
Use controller extension attribute in Javascript
You have some JavaScript issues. There's no size() function in JavaScript arrays and no types like int. Try below for loop in you javascript instead of yours.
for(var i=0;i<theControllerValue....
4
votes
Accepted
Pass value from the visualforce page to a global variable of a class
A good solution to this is to use a proxy object in the controller to capture the dateTime value
public Event evt { // proxy object, fld StartDateTime is creatable
get {
if (this.evt == null)
...
4
votes
Accepted
Visualforce Delete Row: Missing id at index: 0
The problem is that no-database-saved items are being added to your 'delete' lists.
You should validate that, before add an item to your 'delete' lists, the item is a valid record saved in database:
...
4
votes
Accepted
SObject row was retrieved via SOQL without querying- Using Dynamic References
Repro
The MVR here is pretty simple:
<apex:page standardController="Contact" recordSetVar="records">
<apex:pageBlock>
<apex:pageBlockTable value="{!records}" var="record"&...
4
votes
Accepted
How to call controller extesion action from VF component
I found 3 strange reasons why the addMethod wasn't being called. I'd love to hear from anyone that has any ideas on the why, or where I might be wrong.
OnClick attribute
For some reason the onClick ...
4
votes
Accepted
How to get a Javascript variable to the apex controller class
You can't have submitted the value by the time your constructor is called; the page hasn't loaded yet. However, you should be able to check the referrer from Apex Code this way:
public APage(...
4
votes
Accepted
Passing InputText to Controller Extension
The getter is returning the original value that was queried. Just use a normal getter, and assign the default value in your constructor.
public i3_ProjectTimesheet_Controller_Edit(ApexPages....
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
controller-extension × 438visualforce × 322
apex × 237
controller × 71
standardcontroller × 60
unit-test × 47
custom-controller × 18
javascript × 17
visualforce-component × 14
custom-object × 10
standardsetcontroller × 10
extension × 9
soql × 8
code-coverage × 8
class × 8
pageblocktable × 8
pagereference × 7
custom-button × 6
trigger × 5
lookup × 5
map × 5
parameters × 5
inline-visualforce × 5
lightning-aura-components × 4
visual-workflow × 4