23
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 ...
- 5,383
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 ...
- 132k
9
votes
Accepted
How to instantiate a class in custom controller from within controller extension?
To instantiate a subclass you would need to prefix the parent class name, not its variable reference. Try this.
MyClass.MyType myTypeObj = new MyClass.MyType();
- 984
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 ...
- 89.6k
8
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 = ...
- 22.2k
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 ...
- 456k
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<...
- 132k
6
votes
Accepted
4 level, nested for-loop. Bulkified?
I would start off by putting the different locations in maps. So something like:
List<Inventory_Lots__c> icList = [SELECT Id, Location__c, Closed__c, On_Hold__c from Inventory_Lots__c WHERE ...
- 6,089
6
votes
Accepted
How to iterate over a list of objects in visualforce
You don't need to put "get" as it's resolved implicitly by Salesforce (getter methods are called as properties). so you should be able to use:-
<apex:repeat value="{!Attributes}" var="att">
<...
- 2,579
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 ...
- 146k
6
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....
- 14.4k
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 ...
- 456k
5
votes
Multiple controller extensions communication
This week I needed to reference once controller from another and ended up using this code:
public with sharing class Ext1 {
private Registry r;
public Ext1(ApexPages.StandardController sc) {
...
- 132k
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 ...
- 2,358
5
votes
Accepted
Visualforce still sees the old controller variable value even after update
You would need to rerender the Javascript functions so that the new values could be included from the controller.
Right now, while the values are changed on the controller, the existing definitions ...
- 100k
5
votes
Accepted
Test ControllerExt for Flow
According to this blog post you will not be able to get the code coverage, he states that Salesforce has confirmed this. blog link:
**ANOTHER NOTE! You will NOT be able to get 100% test coverage ...
- 10.5k
5
votes
Accepted
SObject row was retrieved via SOQL without querying the requested field. Transaction_Survey__c.Transaction_Score_Other_Reason__c
You need to use this in the VF page to have it work:
Transaction_Survey__c.Transaction_Score_Other_Reason__c
instead of this:
survey.Transaction_Score_Other_Reason__c
As it stands, you are only ...
- 53.6k
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 ...
- 65.8k
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 ...
- 456k
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<...
- 146k
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....
- 89.6k
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}"/>
- 8,656
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'...
- 89.6k
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 ...
- 132k
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 ...
- 89.6k
4
votes
Accepted
Which custom save() is better?
They are not the same.
First scenario
Error is thrown, but caught by the try catch. You are then adding the error to the page by using the ApexPages.addMessage() function. This is a better ...
- 658
4
votes
Accepted
How can I test create an instance of a VisualForce Controller Extension for testing?
You will need to first provide object reference and then use that as a constructor, please see below example:
ex=TestDataFactory.createexam(tform.Id,extaker.id,NEWx);
ex.Name='Chemistry';
ex....
- 88.5k
4
votes
Launching Flow using VF page
There is a known issue describing that when the last screen creates the variable it is not accessible in visualforce. I'm hopeful that once this issue is resolved you will no longer require a screen ...
- 10.5k
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 ...
- 132k
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 ...
- 146k
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
controller-extension × 439visualforce × 322
apex × 239
controller × 73
standardcontroller × 61
unit-test × 47
custom-controller × 18
javascript × 17
visualforce-component × 14
custom-object × 10
standardsetcontroller × 10
soql × 9
extension × 9
code-coverage × 8
class × 8
pageblocktable × 8
pagereference × 7
trigger × 6
custom-button × 6
lookup × 5
map × 5
dynamic-visualforce × 5
parameters × 5
inline-visualforce × 5
lightning-aura-components × 4