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 ...
smukov's user avatar
  • 5,503
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 ...
Keith C's user avatar
  • 135k
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 = ...
Ratan Paul's user avatar
  • 22.5k
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 ...
David Reed's user avatar
  • 92.1k
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....
Patlatus's user avatar
  • 16k
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 ...
sfdcfox's user avatar
  • 481k
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<...
Keith C's user avatar
  • 135k
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 ...
Adrian Larson's user avatar
  • 149k
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 ...
sfdcfox's user avatar
  • 481k
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 ...
xn.'s user avatar
  • 2,418
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 ...
cropredy's user avatar
  • 69.9k
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 ...
sfdcfox's user avatar
  • 481k
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<...
Adrian Larson's user avatar
  • 149k
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....
David Reed's user avatar
  • 92.1k
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}"/>
battery.cord's user avatar
  • 8,767
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'...
David Reed's user avatar
  • 92.1k
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 ...
Keith C's user avatar
  • 135k
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 ...
David Reed's user avatar
  • 92.1k
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 ...
Keith C's user avatar
  • 135k
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 ...
Adrian Larson's user avatar
  • 149k
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....
sfdcfox's user avatar
  • 481k
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. ...
sfdcfox's user avatar
  • 481k
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 ...
sfdcfox's user avatar
  • 481k
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....
highfive's user avatar
  • 6,231
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) ...
cropredy's user avatar
  • 69.9k
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: ...
Joca's user avatar
  • 941
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"&...
Adrian Larson's user avatar
  • 149k
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 ...
Nick C's user avatar
  • 6,426
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(...
sfdcfox's user avatar
  • 481k
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....
sfdcfox's user avatar
  • 481k

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