Questions tagged [apex]

Questions relating to Apex, the native programming language for the Force.com platform. Use it for general questions on syntax, errors, constructs, and rules of use. Most questions should include a code *excerpt* to help answerers understand specifically what has gone wrong or why you need help.

Filter by
Sorted by
Tagged with
151 votes
8 answers
33k views

Unit testing code which has logic around the CreatedDate

An example of the problem would be trying to test a trigger on a case which will fire on update, and look for time differences between when a case was created and now. Ideally in the unit tests I want ...
user avatar
  • 2,897
131 votes
2 answers
9k views

Any reason to skip DML on empty lists?

In parts of my code I have DML statements with an if around to only do the DML if the list is not empty: if( !recordsToUpdate.isEmpty() ) { update recordsToUpdate; } Couldn't I just write? ...
user avatar
127 votes
2 answers
26k views

How to reduce a large internal view state / what is in the internal view state?

I'm building an account search page that lets users select accounts and then pass them to other pages to perform various actions on them. Unfortunately users are reporting hitting the view state ...
user avatar
117 votes
2 answers
118k views

Is it possible to run a SOQL Query and get a MAP returned?

It is possible to run query and the results inserted into an Apex list: List<Opportunity> opportunities = [SELECT Opportunity.OwnerId, Opportunity....
user avatar
  • 4,073
116 votes
3 answers
99k views

How can I efficiently generate a Set<Id> from a List<SObject> structure?

I have a bit of code where I've written a SOQL query that returns a list of SObject records, however I need a Set<Id> structure to pass to another method that I don't have control of (and thus ...
user avatar
  • 22.2k
108 votes
10 answers
39k views

How do I start to debug my own Apex code?

Sometimes questions are posted here that would best be answered by helping the questioner learn how to do their own debugging. (Remember that proverb?) I'd like to have something to link to on this ...
user avatar
  • 130k
87 votes
7 answers
14k views

Apex static code analysis

What static code analysis tools are there for apex? The only one I seem to be able to find is the Force.com Security Source Code Scanner but that has the draw back of placing your request into a queue ...
user avatar
  • 2,725
84 votes
4 answers
49k views

SFDC: Understanding With Sharing, Without Sharing & Unspecified Sharing Classes

Per my understanding of With Sharing, Without Sharing and non-sharing-specified Classes in Apex, I would like to jot down the following notes and leave certain blanks for not sure answers; I wanted to ...
user avatar
  • 841
80 votes
5 answers
11k views

Pokemon Catch - Generic Exception Handling In Apex

I read a great post about this topic on Stack Overflow: Is it really that bad to catch a general exception? However, I asked this question on our exchange because I am interested in a more specific ...
user avatar
  • 143k
79 votes
2 answers
12k views

Salesforce, Oracle and the Seven Dwarfs

Over my time as a Salesforce developer, I've written some code which on occasion managed to surface Oracle plsql exceptions and curiously the error text contains the name of one of Disney's Seven ...
user avatar
  • 22.3k
71 votes
4 answers
27k views

Elegant way to convert Set<Id> into String for Dynamic SOQL IN comparison

When I have a Set of Id's and I want to do a dynamic SOQL query that I want to use it in for an IN comparison, I have to convert the Set to string in the format of: ('id1','id2',id3',...) The way I ...
user avatar
  • 19.1k
70 votes
6 answers
24k views

What is a good set of naming conventions to use when developing on the Force.com platform?

I've looked in the Apex developer's guide and a saw the Naming Conventions section which has basically only has this: We recommend following Java standards for naming, that is, classes start with a ...
user avatar
  • 28.7k
69 votes
1 answer
5k views

InstallHandler runs under a special ghost user. What rights does it have?

When you install a managed package with a post-install script, Salesforce creates a special ghost user under which all of the post install activities occur. I think this is undocumented. Here are some ...
user avatar
67 votes
8 answers
56k views

Get lists of dependent picklist options in Apex

I have a pair of dependent picklists. In Apex code, how can I determine what options are valid in the dependent field for each option in the controlling field? I've tried using getPicklistValues(), ...
user avatar
  • 10.9k
63 votes
5 answers
29k views

How do you deserialize json properties that are reserved words in apex?

Is there any way to deserialize JSON into an object using JSON.deserialize if some of the property names in the JSON are reserved words in apex? I want to do something like this: string jsonString = '...
user avatar
63 votes
4 answers
25k views

Call Apex class method on the fly (dynamically)

Is there any way I can call apex method from class if both class name and method are stored in the string. String strClass = 'BatchUtil'; String strMethod = 'updateAccounts' now I want to call above ...
user avatar
63 votes
1 answer
51k views

Setting a from address in SingleEmailMessage

I want to set a from address in a salesforce SingleEmailMessage, but can't see a way to do this and there is no standard method described in the documentation. I want the email to be sent from ...
user avatar
  • 1,076
61 votes
10 answers
83k views

What would be the best approach to get the recordtype id

I know of two approaches to get the recordtype id of a particular record type Use SOQL RecordType RecType = [Select Id From RecordType Where SobjectType = 'Account' and DeveloperName = 'Business'];...
user avatar
  • 11.9k
59 votes
6 answers
80k views

How to run a scheduled job every 15 minutes?

Using the salesforce GUI it seams impossible to schedule a job every 15 minutes. Or more than once a day.
user avatar
59 votes
3 answers
26k views

Lead Conversion Trigger Order of Execution

I've got a project where I'm looking to tweak the standard lead conversion process. Triggers and lead conversion has always been funky in past projects so I was hoping to start the design off by ...
user avatar
57 votes
5 answers
8k views

How can I get 'Recent Items' object Ids in Apex (soql) is it possible

'Recent Items' pane shows recently viewed records for the selected tab. The list is derived from your recent items and includes records owned by you and other users. Is there any way to get this in ...
user avatar
57 votes
2 answers
58k views

Apex List For Loops 101

I just have a simple question about how list for loops work that has been bugging me. Does the for loop variable refer to the actual list item? Here's what I mean: Say I have a list of accounts. ...
user avatar
  • 1,667
56 votes
6 answers
30k views

Can report data be accessed programatically?

Is there any way to access existing reports and their data to be used in apex? Sure, I could write the SOQL manually but if I want it to mirror a specific report I would have to make code changes ...
user avatar
  • 7,537
55 votes
1 answer
100k views

Using sObject.get() to fetch a field value from a related parent object

I have a contact in a generic sObject in apex and while I'm able to easily get detail field values using sObject.get('MyContactField__c') I'm not able to get fields off the related parent object, ...
user avatar
  • 14.5k
55 votes
3 answers
5k views

Should we use process builder at all?

Forgive me if this isn’t an appropriate topic for this board but I would like to have an open and honest discussion about Process Builder. We have a multi-tenant org (different business units using ...
user avatar
  • 1,551
53 votes
8 answers
58k views

How Can I Tell the Day of the Week of a Date?

I'd like to build in some logic based on the weekday (e.g. Monday, Tuesday, Wednesday) of a given date. How can I identify the date, using either formulas or Apex code?
user avatar
  • 10.9k
53 votes
6 answers
45k views

Deleting Triggers/Classes from Production

I was recently told ( Deleting a Class with IDE - cannot delete from Production? ): You cannot delete a class in production directly. You will need delete the class from your sandbox and then ...
user avatar
  • 4,584
53 votes
4 answers
4k views

Apex debugging in Winter '16 - how well does it work?

This Debug Apex Code in Eclipse (Generally Available) release note says: The Apex Debugger extends the Force.com IDE plug-in for Eclipse and behaves similarly to debuggers available for other ...
user avatar
  • 130k
49 votes
3 answers
13k views

General trigger bulkification - best practices

A general question asked from time to time on stack exchange is question of trigger bulkification. Therefore I have 2 questions: Which best practices are you using? What is the best way to handle ...
user avatar
49 votes
7 answers
43k views

How to get the Runtime-Type of an Object dynamically (for Primitive Data Types and SObjects)

Is it possible to get the Type of an Object (it's not an SObject)? String s = 'test'; doSomething(s); public static void doSomething(o Object) { // need to find out if o is a string or an ...
user avatar
  • 33.6k
49 votes
4 answers
1k views

How can we get "Developer" bugs logged with Technical Support?

I recently found a bug, which I mentioned on The Force.com Discussion Boards, as well as submitting a case, and after a few days fumbling around with the case, they closed it by saying that they only ...
user avatar
  • 434k
49 votes
5 answers
18k views

SOQL Record Locking : For Update

Well I was trying to Implement Record Locking apex but Salesforce Document doesn't seem to help much. Even tried searching stackexchange there was a old question but the answer form StackExchange and ...
user avatar
  • 5,529
48 votes
6 answers
18k views

Is it possible to test apex that relies on field history tracking?

I have some code that relies on field history tracking by querying the History for a custom object. This works fine, but it appears that the history records aren't created during the test so none of ...
user avatar
  • 760
48 votes
3 answers
20k views

Why use Batch Apex?

What is the point of batch apex? I've been trying to do some research but everything I find just tells me how to use it or goes over scenarios where it might be useful - without telling me why I would ...
user avatar
  • 7,537
48 votes
2 answers
6k views

Winter 16 recalculateFormulas() fails when sObject has relationship fields

Using the new method on sObject recalculateFormulas() fails when the Sobject includes relationship fields - note account.name in the query. FATAL_ERROR System.UnexpectedException: Opportunity: bad ...
user avatar
  • 62.4k
47 votes
5 answers
11k views

How do I get the intersection of RecordType and Picklist values inside Apex?

#Context# This question has been asked before in the past and I've found several discussions of it here on StackExchange and elsewhere (links below to pages I've found in my research). So far I'm not ...
user avatar
  • 952
46 votes
5 answers
131k views

How to check in Apex if a Text field is blank

I'm unsure what's the shortest and most robust way to check whether a Text field is blank / empty? /*1*/ Boolean isBlank = record.txt_Field__c == ''; /*2*/ Boolean isBlank = record.txt_Field__c == ...
user avatar
46 votes
6 answers
65k views

Howto wildcard SELECT * to query all fields of objects in APEX?

I have created sObject and I need all the fields and I have many. Is there a way I can directly get all of them rather then typing each one of them.
user avatar
  • 463
46 votes
3 answers
8k views

Where are all past Salesforce Release Notes

As a follow on to the recently posted question "What are all of the reasons why Salesforce customers should upgrade the API version on their Apex classes, triggers, and pages?". Where can I find all ...
user avatar
  • 2,271
46 votes
2 answers
7k views

Inheriting non-implicit constructors on Apex classes

If I have an abstract parent class like this: abstract class Vehicle { public Vehicle(Person driver) { // } } and then I create a child class like this: public class MagicCarpet ...
user avatar
46 votes
2 answers
8k views

Test.loadData() - undocumented (but useful) behavior loading relationships

Premise I want to load Accounts and their related Contacts using Test.loadData() and StaticResources. How do I do this given that the Contact records must have the ID of their parent Account yet the ...
user avatar
  • 62.4k
45 votes
6 answers
39k views

How do I generate a random string?

How does one generate a random string sequence with Apex? I need to generate a code when a particular object is created in our Salesforce environment. I'd like the code to contain numbers from 0-9 ...
user avatar
45 votes
8 answers
71k views

SOQL Alias a column

I am wondering how you would do something simple like this in SOQL. select first_name as name from tableA I can not find any documentation on Aliasing a column which is very common in all forms of ...
user avatar
  • 589
45 votes
5 answers
4k views

What are all of the reasons why Salesforce customers should upgrade the API version on their Apex classes, triggers, and pages?

I haven't been able to find any official documentation or community discussion of the compelling reasons to upgrade the API version on Apex classes, triggers, and pages. I know there is the general ...
user avatar
44 votes
5 answers
135k views

list.IsEmpty() vs list.Size() > 0 vs list != null

I want to check the list is empty or not if not proceed further. So, which is preferable in apex and what are the differences between them? if(myList.size() > 0) or if(!myList.isEmpty()) or ...
user avatar
  • 13.3k
42 votes
7 answers
40k views

How to generate a GUID/UUID?

What is the best way to generate a GUID/UUID from Apex code (such as a trigger)? Preferably in the following format: nnnnnnnn-nnnn-nnnn-nnnnnnnnnnnnnnnnn Example: 13219ec0-3a81-44c5-a300-...
user avatar
  • 1,049
42 votes
1 answer
19k views

Testing AuraHandledExceptions?

It seems like when I call getMessage on a thrown AuraHandledException, I see "Script-thrown Exception" instead of the message that I have set. Any idea why this may be happening? Controller: @...
user avatar
  • 1,487
42 votes
1 answer
874 views

Why is the Salesforce developer documentation NOT indexed on Google anymore?

For years now I'm working frequently with some Salesforce developer documentations, e.g.: Apex Developer Guide Visualforce Developer Guide The best and easiest way in my workflow to find stuff (in ...
user avatar
  • 33.6k
41 votes
4 answers
56k views

Can I find out if the current user has access to a record without querying?

I run a query from an apex class (with sharing), and I get no results I can be sure the user doesn't have permissions (if I know otherwise that the records do exist). But can I get this information ...
user avatar
  • 1,621
41 votes
4 answers
35k views

Is there any difference in equals and == for String variables?

if ('text'.equals(str)) or if ('text' == str) Is there any difference? If yes, then what is it? Added, after answered. String q = 'teXt'; system.debug('teXt'.equals(q)); // true system.debug('...
user avatar

1
2 3 4 5
686