Skip to main content
25 votes

Only variable references are allowed in dynamic SOQL/SOSL

If you use : in your query, Apex expects a variable. In case you are querying with the string, you should not use the :, but escape single quotes(\'). So your query should look like this: asset = ...
Novarg's user avatar
  • 5,264
24 votes

Database Query Locator vs Simple Query

Before we proceed about QueryLocator class and let's see the limits of it. I wrote a sample debug statement to see the limits of the number of rows returned by query locator and normal square bracket [...
Pranay Jaiswal's user avatar
12 votes

Any reason to skip DML on empty lists?

When I look at the results of the following coding, I would still use if (!list.isEmpty()) { DML list; } List<Account> accs = new List<Account>(); System.debug(Limits.getCpuTime()); // 1 //...
loeachim's user avatar
  • 463
10 votes
Accepted

updating a records using external Id in Apex

You need to use the external id in the update call upsert flist Externald__c; For part two of your question, seeing that you want to relate the parent and child foo records you can t do it the way ...
Gareth Jordan's user avatar
10 votes
Accepted

Difference between Database.executeBatch vs System.scheduleBatch

The performance of all Batch Apex (all asynchronous Apex, really) can vary based on system load. That applies regardless of whether the batch is enqueued immediately or with a time delay, based on the ...
David Reed's user avatar
  • 93.2k
9 votes
Accepted

Database.Query Variable Does Not Exist

The only field you can directly retrieve from an sObject is Id. All other fields must be referenced dynamically: pickListValuesList.add(new RelatedObject(s.Id,(String)s.get('Name'))); This is ...
sfdcfox's user avatar
  • 495k
7 votes

Only variable references are allowed in dynamic SOQL/SOSL

The dynamic SOQL documentation explains that simple bind variables (introduced with a :) can be used, that is a reference to a variable (but not a dotted expression or method call). So the pattern to ...
Keith C's user avatar
  • 136k
7 votes

Using query for a specific date

According to the documentation article Query: Find Subscribers by Date or Time Frame the date format to use is 'YYYY-MM-DD' so in your case '2017-05-29' should be correct. However in the ...
Markus Dang - formerly Slabina's user avatar
7 votes
Accepted

Using query for a specific date

EventDate is a datetime - "2017-05-30 22:35:27.122" which is not the same as "2017-05-30" (midnight on 30th May). Try evaluating against: cast(EventDate As Date) This truncates EventDate so you're ...
Macca's user avatar
  • 8,256
7 votes
Accepted

Database Query Locator vs Simple Query

Database.QueryLocator : when used the governor limit for the total number of records retrieved by SOQL queries is bypassed. For example, a batch Apex job for the Account object can return a ...
Hemavantha Rajesh Varma Mudunu's user avatar
6 votes

Dynamic field for Database.Upsert

You can use a literal field reference (SObjectField): Sobject[] records = new Account[] { new Account(Name='Test',Industry='Technology',ExtId__c='12345')}; SObjectField field = Account.ExtId__c; ...
sfdcfox's user avatar
  • 495k
5 votes

Prevent insert to be rolled back even if class (later) runs into an exception?

As per How can I cause side effects outside an execution context?, Summer '17 now has Platform Events GA. Note the following from the docs: Platform Events and Transactions Unlike custom ...
Daniel Ballinger's user avatar
5 votes
Accepted

DML Database method is covered by unit tests, how to determine reason for no test coverage of the SaveResult variable?

Code coverage includes the last line that executed. Presumably, you threw an exception here, so the next line remains uncovered. If it failed because of validation, it would be a DMLException, ...
sfdcfox's user avatar
  • 495k
5 votes
Accepted

Exception in Database.save result in trigger context

You're passing true as the second parameter (allOrNone) to Database.update(). What you're observing is the documented behavior for that situation: ... using the Database class method, you can ...
David Reed's user avatar
  • 93.2k
5 votes
Accepted

SOQL Order by multiple fields with specific values

I believe you're looking for this: SELECT Name, CreatedDate, Status FROM Account ORDER BY Status, CreatedDate This will put all the same Status values next to each other, which each record in the ...
sfdcfox's user avatar
  • 495k
5 votes
Accepted

Querying external database from Salesforce Marketing Cloud

The only way to directly access the external DB is via an API call. The drawback would be that any calls of significant size would be a heavy resource draw and could potentially time out or require ...
Gortonington's user avatar
  • 30.7k
5 votes
Accepted

Query based on variable field name

This is addressed by using Dynamic SOQL. In your case you need to do something like: String phoneNumber = '1'; String param = 'MobilePhone__c'; Account[] accounts = Database.query( 'SELECT Id, ...
Phil W's user avatar
  • 36.9k
4 votes
Accepted

Put value of a field other than ID from list to set

You have to write the loop yourself e.g.: Set<String> s = new Set<String>(); for (SObject sob : Database.query(someSOQL)) s.add((String) sob.get(someField));
Keith C's user avatar
  • 136k
4 votes

Pulling data from data views for one business unit only using query

If what you're trying to do is select results from your child BU you're running the query in, don't use the "ENT" schema. The way to refer to the "_Sent" data view specific to the Business Unit you're ...
Macca's user avatar
  • 8,256
4 votes
Accepted

Salesforce DB connect with SQL Management Studio

Salesforce does not use SQL Server, so you can't just connect from SQL Server Management Studio. A quick Google search found at least one product that claims to allow you to connect from Management ...
Daniel Hoechst's user avatar
4 votes
Accepted

Is it possible to not use an ExternalId field for Database.Upsert?

The error message is phrased confusingly. It means there's three qualities that must be met for the field to be used: Must be an External Id Must be custom or standard Must be indexed This doesn't ...
David Reed's user avatar
  • 93.2k
4 votes
Accepted

Dynamic field for Database.Upsert

The signature of Database.upsert() is public static Database.UpsertResult[] upsert(sObject[] recordsToUpsert, Schema.SObjectField externalIdField, Boolean allOrNone) While the Boolean parameter is ...
David Reed's user avatar
  • 93.2k
4 votes

SObject.addError() method does not prevent DML Operation

From the documentation addError(errorMsg) Marks a trigger record with a custom error message and prevents any DML operation from occurring. So you can only this method in trigger context. In your ...
ytiq's user avatar
  • 4,557
4 votes

Database.Query does not return fields with null value

If you are checking in the debug logs, then that is how it will be displayed. It behaves the same for the normal SOQL query as well as for the dynamic SOQL i.e. with Database.Query So, if you perform ...
Vijay Ganji's user avatar
  • 6,656
4 votes

Salesforce Relational Database ACID

Is this considered one transaction: Say I query a list of Opportunities, I update these Opportunities and run an 'Update' DML on the list. This then triggers some Apex and Flows. Is this all ...
sfdcfox's user avatar
  • 495k
4 votes

Salesforce DB query cache (how long?)

There's no way to know for certain. Caches are designed to last as long as possible, but also to be evicted when they are invalidated (e.g. because a record changed) or when resources are required. ...
sfdcfox's user avatar
  • 495k
4 votes
Accepted

How to rollback DML after catching exception in unit test

A computer has two main types of data storage, called temporary memory and permanent memory (or permanent storage). Temporary memory is commonly called RAM, and it's what a computer uses when working ...
sfdcfox's user avatar
  • 495k
4 votes

Difference between Upsert and Update database methods

Records sent to update must have the Id field populated (at the very least) to be able to perform the DML. upsert can take a mix of new records (without an Id) and existing records (with an Id). ...
Derek F's user avatar
  • 62.5k
3 votes
Accepted

Salesforce database : how does it work

The specifics of how the database connects to the application layer are abstracted away from us. In Java, a generic interface had to be provided to allow access to the database, which means that even ...
sfdcfox's user avatar
  • 495k
3 votes

Database DML method executes twice in any record has been failed

When the allOrNone=false operation fails, SF does a rollback of the entire failed transaction (including what happened in other triggers and rolling back DML limits) before it goes ahead and tries ...
Aidan's user avatar
  • 13.7k

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