Hot answers tagged

30 votes
Accepted

SOQL to find All Users with a Custom Permission

Update Please note that for the running user, you can use the FeatureManagement class: Boolean isEnabled = FeatureManagement.checkPermission('My_Permission_Api_Name'); Surprisingly, I don't think ...
Adrian Larson's user avatar
  • 150k
22 votes
Accepted

Make SOQL Case-Insensitive?

It's case-insensitive by default. From the Force.com SOQL and SOSL Reference on WHERE conditionExpression: comparisonOperator Case-insensitive operators that compare values. Operator = Name Equals ...
Adrian Larson's user avatar
  • 150k
22 votes

StartsWith in Select Query

Use LIKE with a wildcard (%). WHERE Name LIKE 'Womp%' This particular wildcard means "zero or more characters"
Caspar Harmer's user avatar
18 votes

Retrieve globalpicklist/globalvalueset without SOQL

Schema.DescribeFieldResult F = <Object_API_Name>.<Field_API_Name>.getDescribe(); Schema.sObjectField T = F.getSObjectField(); List<PicklistEntry> entries = T.getDescribe()....
Hari Enaganti's user avatar
17 votes
Accepted

Query Which Profiles Have Read Access To Specific Object?

The first query I got to work was on the ObjectPermissions object: SELECT Parent.Profile.Name FROM ObjectPermissions WHERE Parent.IsOwnedByProfile = true AND SObjectType = 'MyObject__c' This query ...
Adrian Larson's user avatar
  • 150k
15 votes
Accepted

Are ID's actually Ascending?

In the current architecture, it is impossible for this to happen. Within a single transaction, ID values are assigned sequentially from a pool of ID values. A Salesforce instance is called a "pod,...
sfdcfox's user avatar
  • 487k
14 votes
Accepted

SOQL returning org data in test class. Not using isTest(SeeAllData=true)

Isolation of Test Data from Organization Data in Unit Tests Starting with Apex code saved using Salesforce API version 24.0 and later, test methods don’t have access by default to pre-existing data in ...
Adrian Larson's user avatar
  • 150k
14 votes

Multiple Child Objects in single SOQL query?

You can write like this: In your case "Test3" is parent,"test1" and "test2" are childs. Try below query, [select id,name,(select id,name from test1s__r),(select id,name from test2s__r) from ...
Annappa's user avatar
  • 2,312
13 votes
Accepted

How do I find merged Records?

In the immediate aftermath (and a short time thereafter), you can find deleted records with a MasterRecordId in the Recycle Bin. SELECT MasterRecordId FROM Account WHERE MasterRecordId != null ALL ...
Adrian Larson's user avatar
  • 150k
13 votes
Accepted

Apex object field for reverse lookup

Yes. You are looking for a Left Outer Join, where the type of query you first reference is a Right Outer Join. Your query might look like: SELECT (SELECT Id FROM Awards__r) FROM Person__c You can ...
Adrian Larson's user avatar
  • 150k
13 votes

Possible to use bind :variable inside SELECT clause?

You have to use dynamic SOQL if you want to add fields dynamically. Merge syntax is not supported. List<String> fields = new List<String> { 'Id', 'Name' }; String soql = 'SELECT ' + ...
Adrian Larson's user avatar
  • 150k
13 votes
Accepted

Possible bug in SOQL LIMIT clause

This will be the Alias Notation for objects. E.g. SELECT count() FROM Contact c, c.Account a WHERE a.name = 'MyriadPubs' In your case LIMIT43 is actually an alias for User. So you will find this ...
Daniel Ballinger's user avatar
12 votes
Accepted

How to query the data that has been modified in last 30 MINUTES

The easiest way might be to make the following Minutes_Since_Modified__c formula: (NOW() - LastModifiedDate) * 24 * 60 Then to query for it: SELECT Id FROM Account WHERE Minutes_Since_Modified__c <...
Adrian Larson's user avatar
  • 150k
12 votes

Dynamic Query - System.QueryException: expecting a colon, found '.'

Explanation The core problem here is that implicit type conversion from List<String> to String will cause truncation. It looks like this happens after the tenth element. You can see it for ...
Adrian Larson's user avatar
  • 150k
12 votes
Accepted

SOQL Performance With LIMIT 1

In the case of assignment to a single record and filtering on an Id, there is zero impact on your governor usage. It won't affect CPU Time, and if you were already enforcing exactly one result by ...
Adrian Larson's user avatar
  • 150k
12 votes

Inline query has too many rows for direct assignment, use FOR loop

You get this error when you have a child list that is too large. Have a look at SOQL For Loops: You might get a QueryException in a SOQL for loop with the message Aggregate query has too many ...
Adrian Larson's user avatar
  • 150k
12 votes
Accepted

Didn't understand relationship in FROM part of query call

The syntax for a child relationship is: [namespace__]relationship_name__r So if the relationship_name is Impacted_Products and the namespace is MC, your sub-query would be: (SELECT Id FROM ...
Adrian Larson's user avatar
  • 150k
12 votes
Accepted

Can Cast Child Related List to Single SObject Record - Compiler error?

Yes, it's always legal to assign a List<SObject> that came from a query into a SObject variable, and it will succeed during runtime only if there's exactly one element. Note that child objects ...
sfdcfox's user avatar
  • 487k
11 votes
Accepted

Search for a field in all objects

SOQL/SOSL won't help you here. You can, however, write a query using the Tooling API like this: SELECT Id,TableEnumOrId FROM CustomField WHERE DeveloperName = 'Owner' The easiest way to do this is ...
sfdcfox's user avatar
  • 487k
11 votes
Accepted

Variable scope - database.query() has access to variables not in scope

Conjecture This seems like an issue with the garbage collector internals, as if exiting the block scope created by if/do/for, etc does not cause the garbage collector to run and clear out heap ...
Adrian Larson's user avatar
  • 150k
11 votes
Accepted

DateTime.Format() Documentation example causes exception

Attribution Use: system.debug(Datetime.now().formatGMT('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'')); The esteemed Mr Ballinger also recommends the following, but it is wrapped in double quotes and slower, ...
Adrian Larson's user avatar
  • 150k
11 votes
Accepted

SOQL to get Permission Set Ids for a specific profile Name users

You can use an inner-join sub-select as follows: SELECT PermissionSetId, AssigneeId FROM PermissionSetAssignment WHERE PermissionSet.Name = 'Something' AND AssigneeId IN ( SELECT Id FROM User ...
Adrian Larson's user avatar
  • 150k
11 votes
Accepted

SOQL Style Guidelnes - Large SELECT Clause

The convention I adopt when selecting many fields is to: Put the SELECT clause on its own line Indent each line in the actual fields by 1 tab/4 spaces Generally put as many fields as possible on one ...
Adrian Larson's user avatar
  • 150k
11 votes
Accepted

Why is the Id of the detail page URL different from the Apex context?

They're the same. system.assertEquals(Id.valueOf('00ea0000001p22SAAQ'), Id.valueOf('00ea0000001p22S')); Salesforce Ids have a 15 digit Id and an 18 digit Id. The longer version is "case-safe" in ...
Adrian Larson's user avatar
  • 150k
11 votes

Query Activity system error in Automation Studio

Typically if Query Activities fail, it's one of these 6 things: Primary key violation -- your query results in duplicate rows not allowed by the primary key Inserting a null value into a non-nullable ...
Adam Spriggs's user avatar
  • 30.3k
11 votes
Accepted

Optimizing SQL for large volumes

My preferred method is to leverage the hidden, pseudo-identity _customObjectKey column to break up queries into manageable chunks. As long as your scheme can accommodate the overwrite-update/append-...
Adam Spriggs's user avatar
  • 30.3k
10 votes

Build a String for Database.query

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 ...
Keith C's user avatar
  • 136k
10 votes
Accepted

Data Loader Error no viable alternative at character

You are only allowed to use single quotes in your query. Replace all of your double quote characters (") with single quote characters (').
Adrian Larson's user avatar
  • 150k
10 votes
Accepted

Is there any way I can use mapping to avoid a SOQL Query here?

Yes, of course. The first step is to gather a collection of the Account ids. Set<Id> accountIds = new Set<Id>(); for (Order order : trigger.new) accountIds.add(order.AccountId); The next ...
Adrian Larson's user avatar
  • 150k
10 votes
Accepted

System.QueryException: expecting a colon, found 'setids'

When you reference an apex variable in a query (standard, or dynamic as is your case), you need to prefix the variable name with a colon :. This is called expression binding. You can still use ...
Derek F's user avatar
  • 60.8k

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