Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

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
20 votes
2 answers
13k views

How do I get started working with JSON in Apex?

This is a canonical question and answer developed by the community to help address common questions. If you've been directed here, or your question has been closed as a duplicate, please look through ...
108 votes
10 answers
38k 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
  • 129k
20 votes
6 answers
43k views

How to write a unit-test / test class for trigger?

How do I write a unit-test / test class for a trigger? What are my options? Inline with the trigger? New Class?
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
15 votes
4 answers
6k views

System.Exception: Too many SOQL queries: 101 [duplicate]

I am getting a Too many SOQL queries: 101 error on a trigger when I am trying to deploy another trigger. I am posting the trigger which is causing the error. trigger rollupAccountShowings on Account ...
user avatar
  • 233
67 votes
8 answers
55k 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
18 votes
1 answer
60k views

How to deserialize a JSON String to Apex

How can I deserialize this json object: { "response": { "count": 1, "benchmark": 0.22567009925842, "requests": [ { "request": { ...
user avatar
22 votes
2 answers
25k views

System.CalloutException: You have uncommitted work pending

I have same exception can please Click Here The previous post issue is resolved. Exception: :System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out ...
user avatar
  • 1,026
58 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
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
31 votes
1 answer
88k views

Can anybody explain the UNABLE_TO_LOCK_ROW error?

Error Message: Developer script exception from Nicomatic : quotepotential : quotepotential: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with ...
user avatar
  • 1,412
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
151 votes
8 answers
32k 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
37 votes
3 answers
39k views

HTTP Callout from Triggers

I have a trigger on the Custom Object that calls a class. That class makes an HTTP GET request to a Web Service. However I get the following error: Callout from triggers are currently not supported. ...
user avatar
11 votes
2 answers
3k views

Parent and children relationships accessible via trigger?

I'm sure this has been addressed before but I can't seem to find the answer. In a trigger on a record, aside from the actual fields on the record, what do I have access to? The parent info? that ...
user avatar
27 votes
6 answers
55k views

Where To start Learning With Salesforce Development

Where should I start learning? There are various topics to learn like Objects, Classes, Apex, Visualforce, SOSL, SOQL, Triggers, etc. There are so many options out there to start learning. I couldn't ...
13 votes
2 answers
3k views

apex String instanceof Object?

given: Map<String, String> m = new Map<String, String>(); m instanceof Map<String, Object> is always true then why is m instanceof Map<Object, Object> always false?
user avatar
  • 131
37 votes
6 answers
26k views

Using APEX to assemble HTML Letterhead Emails

I am looking to assemble an HTML Letterhead Email Template for an email service integration project. There are two sObjects that hold this information - EmailTemplate and BrandTemplate. ...
user avatar
129 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
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
15 votes
5 answers
23k views

How can I compose a multipart/form-data request?

I'm trying to integrate Kraken.io into our org so I can have Salesforce queue up image optimization jobs that our employees upload. Kraken.io requires that the requests post a multipart/form-data body ...
user avatar
81 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
  • 811
36 votes
4 answers
7k views

Why is Schema.describeSObjects(types) Slower Than Schema.getGlobalDescribe()?

I was about to reconfigure some of my code to use the newer Schema.describeSObjects(types) method that I had heard was faster than using a globalDescribe. I made some code to quickly check this and ...
user avatar
15 votes
1 answer
2k views

Set Relationship Via Name Pointing Field?

One of the most startling things I ever learned from @BobBuzzard is that you can update a lookup by using the name pointing field. At least, I thought that's what I learned, until a recent debate cast ...
user avatar
  • 142k
4 votes
1 answer
825 views

Why can't I see related data in my Apex, or why are related records showing up as `null`?

This is a canonical question and answer developed by the community to help address common questions. If you've been directed here, or your question has been closed as a duplicate, please look through ...
115 votes
3 answers
98k 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.1k
56 votes
6 answers
29k 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,527
41 votes
4 answers
2k views

Why Are Data Silos Important in Unit Tests?

I have a lot of strong opinions about this issue, but had a hard time finding official documents today to back up my position. A lot of times we say it is bad practice to use seeAllData=true, but we ...
user avatar
  • 142k
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
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
38 votes
4 answers
51k views

What is the Difference Between Controllers and Extensions?

Can any one explain the usage of controller and extensions in Visualforce? In which scenario we have to use controller and extensions?
user avatar
  • 1,849
24 votes
2 answers
25k views

How can i integrate one SFDC org to another SFDC using Rest Api

I am trying to integrate one Salesforce org to other org by using Oauth setup, I wanted to use use consumer key and consumer secret key in my REST class to connect with the org, after connection part ...
user avatar
  • 425
78 votes
2 answers
11k 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
23 votes
3 answers
10k views

Dynamically Determine Calling Context?

We have various ways of invoking apex. Triggers, Inbound Webservices, @InvocableMethod,@RemoteAction and what not. Often we call some or other utility methods from these execution contexts. I have ...
user avatar
0 votes
2 answers
4k views

Need to pass Field api name and its value in API call using field set [closed]

Need to make the requested body for Rest POST call in which key(API name) and value both go dynamically in JSON format. API name comes from field set and its value come from the database using query. ...
user avatar
  • 53
42 votes
7 answers
39k 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
59 votes
6 answers
79k 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
27 votes
3 answers
97k views

System.FinalException: Record is read-only: Trigger.updateCompetitors: line 24

I am writing a trigger to get all the Opportunity competitors into a field on Opportunity, however, getting an error while testing it. Error: updateCompetitors: execution of AfterUpdate caused by: ...
user avatar
  • 271
15 votes
3 answers
7k views

What is the best practice re: API Version of your Apex Classes?

I'm in the process of pushing some updates to some code I originally wrote 12-18 months ago, and the API Version is between 27.0 and 29.0 I know this is to protect the integrity of the test classes ...
user avatar
5 votes
1 answer
2k views

Help on Invokable Apex Test class and @Future callout Apex test Class

I am pretty new to Apex and trying to learn as much as possible. Any help would be really appreciated. I have two classes. Invokeable Apex Class: This class is just getting called by a Process (...
user avatar
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,564
86 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,715
23 votes
2 answers
23k views

Can we access class properties dynamically with Object class?

Is it possible to access Apex class properties dynamically? While we can create Object's, and assign any other class to an Object, I'm not sure how I can access the properties of these Object's. Can ...
user avatar
36 votes
2 answers
4k views

Ghost Schedulable Classes Blocking Deployment

Recently I've been running into issues where deployments were blocked due to a Schedulable job pending or in progress when I'd already cleared the Schedulable job from that class. Any tips for ...
user avatar
17 votes
4 answers
75k views

how to solve callout error? You have uncommitted work pending. Please commit or rollback before calling out

I have written inline Vf page to update the record value. The values are coming from a web service. While updating the record the detail page redirected to VF page. But the record was committed in the ...
user avatar
  • 769
25 votes
2 answers
11k views

Using an sObject as a Map key

Are sObjects supported as the keys for Maps? I thought they were, but it appears that any subsequent changes to the key sObject break the mapping to the value. This might be better shown with an ...
user avatar
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.6k
72 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
53 votes
8 answers
57k 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
23 votes
1 answer
2k views

Is there a specific reason why we can't upcast Sets?

As most stories here begin, I was writing some code for a project that I was working on, and stumbled across an oddity when writing some unit tests. I was testing a method that has a Set<sObject>...
user avatar
  • 47k

1
2 3 4 5
66