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 ...
user avatar
  • 5,248
22 votes
Accepted

How to override Save method in standard controller extension?

You don't use the override keyword in this case, as you aren't extending another class and overriding its methods in a classical object oriented situation. Instead, the Visualforce platform adheres ...
user avatar
  • 19.7k
16 votes
Accepted

SObject row was retrieved via SOQL without querying the requested field in controller extension

A standardcontroller's record will only contain the fields referenced in the visualforce page you're using it on. The standard controller addFields(List) method (docs) allows you to extend this to the ...
user avatar
9 votes
Accepted

'Map key null not found in map' when using apex:pageBlockTable

Things often get a bit hairy when you start to use maps of maps (or mapception :D). You can represent tables of data in this way if you change your variable model. Change the map of maps to be a ...
user avatar
9 votes

Controller Class Inheritance vs Multiple Controller Extensions

Inheritance denotes a commonality or relationship between objects. Extensions let you mix in completely unrelated functionality in a consistent manner. For example, an "address lookup" extension ...
user avatar
  • 434k
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();
user avatar
9 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 ...
user avatar
  • 130k
8 votes
Accepted

Error message: SObject row was retrieved via SOQL without querying the requested field?

The only fields that are queried by default are the fields that are explicitly bound to the main standard controller during the page's construction (before the constructor of your extension is called)....
user avatar
  • 434k
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 = ...
user avatar
8 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 ...
user avatar
  • 86.9k
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 ...
user avatar
  • 434k
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<...
user avatar
  • 130k
6 votes
Accepted

Unknown property

You haven't defined a getter for your Installation__c instance variable. Try replacing this line public final Installation__c installation ; with public final Installation__c installation {get; ...
user avatar
6 votes

custom controller with extension

It is possible to have an extension for a custom controller. Let's say my custom controller is this: public with sharing class MyController { ... } My extension could then have a contructor that ...
user avatar
  • 19.1k
6 votes
Accepted

Writing JSON using JSONGenerator with writeStringField()

Rather than manually constructing the JSON, providing the names you need to use in the JSON are legal Apex identifiers you can leave it to the platform to build the correct serialization and just use ...
user avatar
  • 130k
6 votes
Accepted

Using PageReference to redirect after new record insert

I found an answer here that might work for you as well, where the solution would be to change your "Save" method to the following: public PageReference saveAndRedirect() { if(controller.save() != ...
user avatar
  • 10.4k
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 ...
user avatar
  • 6,029
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"> <...
user avatar
  • 2,569
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 ...
user avatar
  • 143k
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....
user avatar
  • 13.3k
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 ...
user avatar
  • 434k
5 votes

'Map key null not found in map' when using apex:pageBlockTable

Here is a work around for your problem. Add null key in controller Map<String, Map<String, Boolean>> theMap = new Map<String, Map<String, Boolean>>(); theMap.put('...
user avatar
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) { ...
user avatar
  • 130k
5 votes
Accepted

VF ControllerExtension: Too many query rows: 50001 - How to solve

The query limit applies to the sum of all queries in one transaction, so the 'limit 50000' doesn't help here. Alternatively you can check the limits before doing the query: Integer rowsLeft = Limits....
user avatar
  • 325
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 ...
user avatar
  • 2,328
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 ...
user avatar
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 ...
user avatar
  • 10.4k
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 ...
user avatar
  • 53.1k
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 ...
user avatar
  • 62.3k
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 ...
user avatar
  • 434k

Only top scored, non community-wiki answers of a minimum length are eligible