22

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 instead of using the old getRecordTypeInfosByName() method as suggested in the original answer. Use this approach instead of the old suggestion below, unless ...


21

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 to a contract that it will look for a method in your extension named 'Save' first, and if it doesn't find that it will fallback to the method in the standard ...


16

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 fields you need in your apex code. cont.addFields(new List<String>{'Assigned_to_Partner__c'}); Set this as the 2nd line in your constructor.


9

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 might be used to provide connectivity to a third-party service (most likely in conjunction with a component) to provide address auto-completion. You could use this ...


9

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 list of a new inner class that contains simply the name of the inner property and a map of the outer properties to boolean values. Also add a method to retrieve a ...


9

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();


8

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). You have two choices here: 1) You can use StandardController.reset() and StandardController.addFields() to add Name as a field to query. 2) You can bind ...


8

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 = Page.SendEmailReceipt1; Test.setCurrentPage(pageRef); pageRef.getParameters().put('Id', String.valueOf(obj.Id)); ApexPages.StandardController sc = new ApexPages....


8

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 attack. Note that the value bound has to be a simple variable reference (e.g. not a dotted expression) for the dynamic SOQL case. So: String companyLike = '%'...


8

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 read-only while caching its value (a lazy-loading pattern), you can synthesize a private setter: get { // ... } private set; That will allow your class ...


7

From the Error Lines, the error is happening here: private List<Contact> performSearch(string searchString) { String soql = 'SELECT id, name FROM Contact'; if(searchString != '' && searchString != null) { soql = soql + ' WHERE name LIKE \'%' + searchString + '%\''; soql = soql + ' AND OwnerId =: currentUserId'; ...


7

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 Redirect flag is set to false on the PageReference. Both pages use an apex:form. Here's an incredibly basic proof of concept that shows this behavior in action: ...


7

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<String, Custom_Object__c>(); to: map = new Map<String, Custom_Object__c>(); so you are assigning to the property in the constructor, not just the ...


6

You can go with the Upsert Option. Upsert can be used in places where you are not sure if you'll have the Id of the Object and you want to insert/update the record. If the object doesn't have an Id, it performs an insert and if it has, it does an update call. public pageReference SaveandClose() { try{ upsert this.Test; } ...


6

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; private set;} This will allow the VF page to be able to access the installation variable.


6

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 simple Apex classes: global class VF_MethodRegister { public class Message { public Item[] Card; public String PersonalID; public ...


6

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 is called when this class is the controller for a page: public with sharing class MyExtension { ... public MyExtension(MyController ctrlParam) { } } You ...


6

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() != null) { PageReference redirectPage = Page.force_OrderDetail; redirectPage.setRedirect(true); redirectPage.getParameters().put('id',controller.getId())...


6

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 Location__c IN :recIds.Keyset() AND Closed__c != TRUE AND On_Hold__c != TRUE]; Map<Id, List<Inventory_Lots__c> lotByLocation = new Map<Id, List<...


6

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"> </apex:repeat>


6

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 above performs a query, pulling down all fields used on the page. In addition to what fields you use on the page, you can use the addFields method (though it ...


6

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.sObjectType.Contact.fields.Name.isUpdateable()){ return null; } For dynamic check you could use Describe functionality. Basically you need to use several methods ...


6

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 is, but that's been very vexing. Quite a few years ago, I stumbled across a particular oddity: StandardController and StandardSetController do not act like DML ...


5

The standard controller addFields method supports field access via relationship fields allowing you to pass the responsibility back to the standard controller to do the querying. But "New" behaving differently to "Edit" suggests that the problem may be more related to differences in how the standard controller handles the two situations. But addFields is ...


5

When trying to understand some new programming concepts or the specifics of a new language or API, the documentation is invaluable. Here's a couple of resources for you. Force.com Apex Code Developer's Guide Visualforce Developer’s Guide Here are some details about the specific code snippets you had questions about. 1) Check out the Exception ...


5

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) { r = Registry.instance(); r.add(Ext1.class, this); ... } private Ext2 getExt2() { return (Ext2) r.get(Ext2.class); } }...


5

You can not really control which action you want to call. The first class in your extensions will have always the highest priority : doc here. //it will always call method from cntrlr1 first <apex:page standardController="Contact" extensions="cntrl1,cntrlr2"> //it will always call method from cntrlr2 first <apex:page standardController="Contact" ...


5

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.getLimitQueryRows() - Limits.getQueryRows(); francePinCodeList = [SELECT name from Quote_line_item__c limit :rowsLeft]; And maybe check if rowsLeft == 0 to ...


5

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('Outer 1', new Map<String, Boolean> { 'Inner 1' => true, 'Inner 2' => false }); theMap.put('Outer 2', new Map<String, Boolean> { 'Inner 1' ...


5

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 DML operations. You never want your user to see the white exception screen so you want to catch all exceptions in your action method. But these exceptions ...


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