Hot answers tagged

16 votes
Accepted

What Type of Joins are Supported in SOQL?

Thanks to tugce for pointing out on Twitter the existence of web.archive.org, which has a cached version of the link in question. Using that, I was able to retrieve the old documentation. It seems ...
user avatar
  • 144k
7 votes

how to get sandbox name in apex?

There is now an official solution that is out with the Spring' 22 release. You can use the new getSandboxName() method: System.Domain d = System.DomainParser.parse(URL.getOrgDomainUrl()); System.debug(...
user avatar
7 votes
Accepted

QUERY_TOO_COMPLICATED pulling a specific record over REST API

Triggers are not firing when a record is queried, but formula fields are recalculated. This error is normally happening, if the SOQL query to get record information, exceeds the character limit of 20K....
user avatar
7 votes

No viable alternative at character " "

Double quotes " are not used for SOQL query to specify a filtered value in conditional expression. You can use single quotes with escaping \'. Take a look at Quoted String Escape Sequences public ...
user avatar
7 votes
Accepted

Is it possible to sort this SOQL statement in one query?

The ordering that looks like [1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 3, 4, 5, 6, 7, 8, 9] is called lexicographic ordering. When you have a mix of alpha and numeric characters, that's the ...
user avatar
  • 49.5k
7 votes

Odd Behaviour in SOQL Query

This behavior is related to how ContentVersion, Document and ContentDocumentLink interact with each other and how they are accessed. Your first query is "open" (OR), your second query is &...
user avatar
  • 1,340
7 votes
Accepted

Is "FOR UPDATE" available in dynamic SOQL?

You can't test record locking in testmethods so you'll need to use Execute Anonymous and some simulator that forces the FOR UPDATE locking condition I borrowed from Dan Appleman's 4th Edition Advanced ...
user avatar
  • 63k
6 votes
Accepted

Why does SOQL return related records when run directly but not when run with Apex?

When you debug an SObject record, the output is not necessarily going to show you everything in memory, as it coerces the complex data type into a String to render. What you observe when you system....
user avatar
  • 144k
6 votes
Accepted

Is it possible to get a Custom Setting without the cost of a SOQL query and at the same time do it DYNAMICALLY without hardcoding the setting?

There's no way to do this directly; there's no unique common parent type or interface, we can't access namespace properties dynamically, we don't have proper language reflection, etc. Basically, the ...
user avatar
  • 438k
6 votes
Accepted

SOQL query: What is the different between "1638369480000" and "1638369480000.0"

SOQL uses 32-bit signed integers, meaning the maximum integer value is 2,147,483,647. SOQL doesn't understand long literals (e.g. something like 1638369480000L). It'll complain about the 'L', and if ...
user avatar
  • 49.5k
6 votes

SOQL query performance with many or few fields

In my experience, there is effectively no difference whatsoever. Query selectivity is unaffected, so query run time should be as well. If you look at any two queries in the Query Plan where the only ...
user avatar
  • 144k
6 votes
Accepted

Non-selective query against large object type (more than 200000 rows) - Solution

There are two culprits that will almost always be the issue. One is easy to fix, and that is if your collections of Id values may contain null, which will cause a table scan. Make sure you strip out ...
user avatar
  • 144k
6 votes

I am getting error in Query

There are a few syntax errors with the SOQL query that are causing this error to occur. It looks like you are trying to alias the field names; however, this feature is only available for aggregate ...
user avatar
5 votes
Accepted

Method does not exist or incorrect signature: void indexOf(Group) from the type String

This is one of those cases where the error correctly tells you what the issue is. indexOf() is a String method. This means it must be used/called on a String; but in this case it also takes a String ...
user avatar
  • 2,222
5 votes
Accepted

Which Tooling API Objects can be queried in Apex?

As of this writing, the following types are available in the Schema namespace and can be queried (with one noted exception). These objects are listed in the Tooling API Developer Guide, but not in the ...
user avatar
  • 144k
5 votes
Accepted

Map or String: what do I have to use?

Using a map like that obfuscates the parameters, making it so developers have to read the method to know what parameters to use. Worse, these kinds of maps are case sensitive. I would strongly ...
user avatar
  • 438k
5 votes

Will acc.Contacts return list of all Contacts related to the Account?

Given the code you have, no, acc.Contacts will not return any Contacts. To get related Contacts, you need to query for them. Since you haven't queried for them, you get an empty list (honestly, I'm a ...
user avatar
  • 49.5k
5 votes
Accepted

Is a SOQL query with FOR UPDATE enough to safely delete a record?

The documentation you're looking for is Avoiding Deadlocks. As a developer, use care when locking rows to ensure that you are not introducing deadlocks. Verify that you are using standard deadlock ...
user avatar
  • 438k
5 votes
Accepted

How to replace a SOQL query with a string?

Instead of for (List<AggregateResult> agg: query) you should have for (List<AggregateResult> agg: Database.query(query)). You can find additional detail regarding Dynamic SOQL here. Best ...
user avatar
5 votes

What is the difference between soql for loop vs soql list

I think you're accidentally conflating the two approaches we can take when it comes to SOQL for loops. Have the loop variable be a single SObject instance (e.g. for(Contact cont :[query here])) Have ...
user avatar
  • 49.5k
5 votes
Accepted

Unable to compare two numeric fields in SOSQL

Salesforce does not allow direct field to field comparison in SOQL and SOSL queries: https://blog.bessereau.eu/assets/pdfs/salesforce_soql_sosl.pdf. fieldExpression Syntax: fieldName ...
user avatar
5 votes

Compare 2 datetime in Apex batch class

You're dramatically overcomplicating this code. You do not need Dynamic SOQL at all. Just do return Database.getQueryLocator([ SELECT Id, Name, Start_Date__c FROM Notification__c WHERE ...
user avatar
  • 87.3k
5 votes
Accepted

How to retreive objects ambiguous lookup field with SOQL?

What you have here is a polymorphic relationship field. There is documentation on Working with polymorphic relationships In a normal query that pulls in data from a parent record, you use the ...
user avatar
  • 49.5k
5 votes
Accepted

SOQL query in .js file in LWC

No, you can't write a SOQL in the js file, but if the SOQL would have been something like "SELECT fields FROM object WHERE Id = 'xxx'" or "SELECT fields FROM object WHERE Id IN ...
user avatar
  • 3,738
4 votes

Is there a maximum value for LIMIT in a SOQL query?

As of 08/2021, it appears that the Salesforce API validates the limit with int8 datatype. The largest possible number for that is 2,147,483,647. Any number larger than that will return a Malformed ...
user avatar
4 votes

Dynamic SOQL with multiple conditions and undefined amount of variables

You use Dynamic Apex. String[] filters = new String[0]; if(firstName != null) { filters.add('FirstName = :firstName'); } if(lastName != null) { filters.add('LastName = :lastName'); } // ... etc ......
user avatar
  • 438k
4 votes

How to use a constant in SOQL in a test class

It is a bind variable, so it needs to be used with colon. Also it should be declared outside the test method. Moreover, when you use @IsTest annotation on the method, you do not need to add testMethod ...
user avatar
4 votes
Accepted

How to retrieve multiple objects records in the Batch Apex execute method?

In execute method parameter of the records is List<sObject> which is not a concrete SObject, but a general Sobject. In order, to get field values from Opportunity, you can cast SObject to ...
user avatar
4 votes

Retrieve accounts that don't have any open opportunities

You need to write a query with IN for receiving account id list from your nested query SELECT AccountId from Opportunity where IsClosed = false. So your query will be followingly, SELECT Id FROM ...
user avatar
  • 1,082
4 votes
Accepted

How to add value into a map which is stored in another map?

Taking time to try to understand the error message you're getting will help you understand how to move forward. Although you have paraphrased the error and not included the stack trace (when asking ...
user avatar
  • 49.5k

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