Posts

Showing posts from June, 2020

System.QueryException: Aggregate query has too many rows for direct assignment, use FOR loop

This exception is talking about an “aggregate query” not an “aggregate function.” I had never heard the term “aggregate query” before, and I suspect you haven’t either. After hunting around, I found the relevant Salesforce documentation – Salesforce Apex Developer Guide: SOQL For Loops. The documentation explains that Salesforce might throw the System.QueryException when a query has a sub-query (this is the “aggregate query”), and our sub-query returns more than 200 child records. For example, if we have an Account with more than 200 AccountTeamMembers, we might get the exception when we run the following code with a query on Account with a sub-query for child AccountTeamMembers.  Code that could throw an exception if there are more than 200 child records: List<Account> accounts = [Select Id, (Select Id, AccountId , UserId From AccountTeamMembers) , (Select Id , OwnerId, AccountId from Opportunities) From Account]; for ( Account acc : accounts ) {     // Either...

How to run a scheduled job every 15 mins?

You can write a Batch Class and then Schedule it with the help of a scheduler.Let me explain it to you with the help of an example: BATCH CLASS  : global class test_BATCH implements Database.Batchable<sObject> { global (Database.QueryLocator | Iterable<sObject>)  start (Database.BatchableContext BC) { String query = 'SELECT Id,Name FROM Account'; return Database.getQueryLocator(query); } global void  execute (Database.BatchableContext BC, List<Account> scope) { for(Account a : scope){ a.Name = a.Name + 'Updated'; } update scope; } global void  finish (Database.BatchableContext BC) { } }   SCHEDULABLE CLASS : global class scheduledBatchable implements  Schedulable { global void execute(SchedulableContext sc) { test_BATCH b = new test_BATCH(); //Parameters of ExecuteBatch(context,BatchSize) database.executebatch(b,10); } } ***Note: if batch size is not mentioned it is 200 by default.   CUSTOM TIMING String time = '0 0 * * * ?'...

The Developer Console didn't set the DEVELOPER_LOG trace flag on your user. Having an active trace flag triggers debug logging. You have 1,978 MB of the maximum 1,000 MB of debug logs. Before you can edit trace flags, delete some debug logs.

1. In developer console, Select tab "Query Editor" at the bottom of the console. 2. Select check box "Use Tooling API". 3. Execute the below query: SELECT Id FROM ApexLog   4. Select All Rows. Click the first row and then press Shift button and click last row to select all the rows. Delete all rows using Delete button. https://help.salesforce.com/articleView?id=code_debug_log_delete.htm&type=5

Troubleshoot 'System.LimitException: Batchable instance is too big' error

If a batch apex class implements Database.Stateful, the instance state is stored in the database.  At this time we do a check against the size of the instance state and t he exception is thrown when the Apex Heap Size limit is exceeded. For synchronous Apex the heap size limit is 6 MB and for asynchronous Apex the limit is 12 MB.  This exception can obviously occur in a scenario where large collections are persistently maintained. Even if some chunks fail with this error, the batch job is completed. As per below lines in  Batch Apex Developer Guide   "If the first transaction succeeds but the second fails, the database updates made in the first transaction are not rolled back." To work around this issue, you can use the following steps: 1. Avoid using Database.Stateful 2. Reduce the amount of persistent data 3. Process less records per batch   In some cases Database.Stateful isn't being implemented or the steps above are not helpful in working around this e...

Locking Statements in Salesforce

In Apex, you can use  FOR UPDATE  to lock sObject records while they’re being updated in order to prevent race conditions and other thread safety problems. While an sObject record is locked, no other client or user is allowed to make updates either through code or the Salesforce user interface. The client locking the records can perform logic on the records and make updates with the guarantee that the locked records won’t be changed by another client during the lock period. The lock gets released when the transaction completes. To lock a set of sObject records in Apex, embed the keywords  FOR UPDATE  after any inline SOQL statement. For example, the following statement, in addition to querying for two accounts, also locks the accounts that are returned: Account [] accts = [SELECT Id FROM Account LIMIT 2 FOR UPDATE]; You can’t use the  ORDER BY  keywords in any SOQL query that uses locking.

Difference between Export & Export All in Data Loader

Image
Difference between Export & Export All in data loader is really interesting. It was asked this question in one of my job interviews. There are two buttons available in the data loader ‘Export’ & ‘Export All’. The difference in both button functionality is very small. When we use the ‘Export’ button for any object in Salesforce, all records( excluding records present in Recycle Bin) present in the system for that particular object are exported to a .csv file. But when we use Export All, all records (including records present in Recycle Bin) for that particular object are exported to a .csv file. Deleted records present in the recycle bin are also called ‘soft Deleted’ records. When you choose fields to be extracted using a data loader, you can select the IsDeleted field as well because it tells which record is soft-deleted and which is not. (IsDeleted = true) –> record has been soft deleted. (IsDeleted = false) –> record has not been deleted. I clicked on Export All an...