Skip to main content

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
26 votes
2 answers
27k 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 ...
109 votes
11 answers
42k 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 ...
Keith C's user avatar
  • 136k
50 votes
3 answers
14k 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 ...
Christian Deckert's user avatar
21 votes
6 answers
46k 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?
19 votes
1 answer
90k views

How to deserialize a JSON String to Apex

How can I deserialize this json object: { "response": { "count": 1, "benchmark": 0.22567009925842, "requests": [ { "request": { ...
codeSlayer's user avatar
15 votes
4 answers
7k 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 ...
user4675's user avatar
  • 233
72 votes
8 answers
69k 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(), ...
Benj's user avatar
  • 11.2k
15 votes
5 answers
33k 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 ...
Gup3rSuR4c's user avatar
  • 1,006
60 votes
3 answers
32k 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 ...
Ralph Callaway's user avatar
52 votes
6 answers
25k 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 ...
jwolf's user avatar
  • 800
22 votes
1 answer
27k views

System.CalloutException: You have uncommitted work pending

I have this exception related to my previous question: System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out When clicking preview as an admin the ...
Ramesh Somalagari's user avatar
16 votes
2 answers
7k 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?
davec's user avatar
  • 181
63 votes
10 answers
86k 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'];...
Prady's user avatar
  • 12.3k
160 votes
8 answers
37k 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 ...
David Gillen's user avatar
  • 2,997
38 votes
4 answers
42k 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. ...
Axaykumar Varu's user avatar
27 votes
6 answers
56k 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 ...
18 votes
1 answer
3k 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 ...
Adrian Larson's user avatar
  • 150k
31 votes
1 answer
94k 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 ...
Teja's user avatar
  • 1,422
11 votes
2 answers
4k 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 ...
PartOfTheOhana's user avatar
141 votes
2 answers
12k 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? ...
Robert Sösemann's user avatar
75 votes
1 answer
6k 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 ...
Matt and Neil's user avatar
87 votes
4 answers
54k 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 ...
Swati's user avatar
  • 871
71 votes
5 answers
36k 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 = '{...
Greg Grinberg's user avatar
38 votes
6 answers
31k 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. ...
Andy Boettcher's user avatar
125 votes
3 answers
131k 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 ...
ca_peterson's user avatar
40 votes
4 answers
13k 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 ...
Caspar Harmer's user avatar
5 votes
1 answer
2k 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 ...
86 votes
2 answers
16k 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 ...
Mark Pond's user avatar
  • 23k
58 votes
6 answers
32k 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 ...
Ryan Elkins's user avatar
  • 7,687
43 votes
4 answers
3k 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 ...
Adrian Larson's user avatar
  • 150k
21 votes
6 answers
128k views

How to solve callout error on VF Page? 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 ...
uma451's user avatar
  • 809
128 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 ...
Ralph Callaway's user avatar
40 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?
Priyanka's user avatar
  • 1,909
0 votes
2 answers
5k 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. ...
Harish's user avatar
  • 63
63 votes
6 answers
98k 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.
Christian Deckert's user avatar
47 votes
10 answers
54k 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-...
Paul Sasik's user avatar
  • 1,119
39 votes
2 answers
5k 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 ...
Ralph Callaway's user avatar
31 votes
3 answers
113k 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: ...
user5208's user avatar
  • 311
15 votes
3 answers
9k 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 ...
Nathan Williams's user avatar
6 votes
1 answer
2k views

How to improve schema access performance [duplicate]

We have Apex code that needs to interrogate the schema to dynamically handle configuration-based behaviour (e.g. for defining fields that should be loaded or saved) for specific types of object (such ...
Phil W's user avatar
  • 36.8k
5 votes
1 answer
3k 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 (...
Aditya Singh's user avatar
90 votes
8 answers
15k 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 ...
Bob Roberts's user avatar
  • 2,785
56 votes
6 answers
47k 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 ...
AMM's user avatar
  • 4,704
35 votes
1 answer
34k views

Error: MIXED_DML_OPERATION on setup and non-setup objects

In my test class i have the creation of a dummy custom setting and a dummy lead: Group g1 = new Group(Name='group name', type='Queue'); insert g1; QueuesObject q1 = new ...
Enry's user avatar
  • 3,726
28 votes
2 answers
14k 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 ...
Daniel Ballinger's user avatar
25 votes
3 answers
2k views

What is a use case where one would use an SObject as a Map key?

In Dan Appleman's book Advanced Apex Programming, he explains how using SObjects as Map keys (i.e. Map<SObject, *value*>) is supported but it's poor practice, since the key is actually ...
Brian Mansfield's user avatar
25 votes
2 answers
26k 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 ...
sneha's user avatar
  • 435
24 votes
4 answers
14k 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 ...
Pranay Jaiswal's user avatar
23 votes
2 answers
29k 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 ...
Shrivaths Kulkarni's user avatar
8 votes
1 answer
4k views

ApexMocks Troubleshooting Guide

Canonical Question and Answer When using ApexMocks, I'm getting unexpected results. What could be the possible reasons? Here are some of the symptoms I observe: Null pointer exception in my service ...

1
2 3 4 5
71