Thursday, May 26, 2016

Developer: Loader/Trigger Errors....Too many batch retries in the presence of Apex triggers and partial failures.”

Too many batch retries in the presence of Apex triggers and partial failures. Usually you get this while loading data thru data loader. However, instead of the standard sensible error messages you get something like "Too many...."

If you ever run into this error; turn on the logs, identify the trigger and straight go to your trigger and review the code.

Most likely there is a bulk DML operation happening in the trigger which has AllorNone option set to True.

Review the following Salesforce document which explains in details the process:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_bulk_exceptions.htm

Review the data is being loaded and try to load with smaller sets and see if the problem is locatable.
Fix the problem and then load in bulk.

Developer: Too many SOQL queries....

When you hit this error; standard best practice will ask you to review the following:

1. Since Apex runs on a Multitenant structure, Apex runtime engine strictly enforces limits to ensure code doesn't monopolize shared resources. Learn about the Governors Limit.
2. Avoid SOQL queries that are inside FOR loop.
3. Check out the Salesforce Developer Blog where you can find Best Practices for Triggers.
4. Review best practices for Trigger and Bulk requests on our Force.com Apex Code Developer's Guide.
5. Be sure you're following the key coding principals for Apex Code in our Developer's Guide.


Sometimes the answer is not that obvious that you review a piece of code and you find the solution.

Here's my list of steps:

Review your WF/Process Builder rules; many times Admins write basic updates on the same entity causing continous firing of Before/After triggers. Try to move them to code.

Review the WF rules and move them to formulas; check for ISCHANGED so that the rules are not fired perpetually.

If this too doesn't solve your issues; turn the debug logs on. Look for SQL_EXECUTE_BEGIN.  Salesforce does a good job providing the query num that is being fired. Try to observe the pattern; atleast one query or process is being fired repeatedly. Work on that process or query; typically making it @future has helped solve the issue. However, if you are in an environment where the code is not being written by you or jumps from class to class that keeping track can become challenging and confusing use the log route to zero in on your problem.

Tuesday, May 17, 2016

Developer: DML operations not allowed on user...

Trying to build a VF page to load Accounts, Contacts and User.

While I could use the Upsert operation on Account and Contacts, User object would not allow me to use User.

"DML operation not allowed on User" was the error throw while trying to save the class.

Solutions

I worked around this by adding an operation flag to a file and creating 2 list one for insert
and one for update.

I then would check the value in the flag and then based on whether it is Insert or Update
add the record to the appropriate list and then do a bulk operation.

Developer: Salesforce Chunking error.....what is it

Error: System.TypeException: Cannot have more than 10 chunks in a single operation. Please rearrange the data to reduce chunking.

If you have received this error then most likely you are operating with a hybrid List of Sobjects
meaning you have something like

List<Sobject> S = new List<Sobject>()

and then in this list S you are adding object records from different entities like
Accoint Lead and other Custom entities.

Sometimes incorporating a simple list.sort() method works but if this does not work then you might have to break down your list into different Lists specific to each object and then perform the operation.

Referring to this post will help understand the situation better:
http://bartoszborowiec.wordpress.com/2014/06/15/execution-failed-system-typeexception-cannot-have-more-than-10-chunks-in-a-single-operation-please-rearrange-the-data-to-reduce-chunking/

but if the sort method does not work then use seperate lists and proceed.

Thursday, May 12, 2016

Developer: TestClass Run Failures: Unable to run testclass for Class ID...

I received this error recently while running test classes "Unable to run testclass for Class ID....".
The test class ran for a while before spewing out this error. There were 10-12 test methods in the test class but none of them showed as success.

Solutions

Go to Setup-->Develop-->Apex Test Class Execution.

Click on the Select Options button.

Uncheck Disable Parallel Testing and the Store Aggregate code coverage and run your tests again.

If you find that your tests run successfully you can try and check them back and re-run your tests.

It's just one of the things which worked me; nothing with a definitive logic here.


Monday, May 2, 2016

Developer: Trigger Design Patterns

Some standards to follow while writing Triggers:

a) Write only 1 trigger per object to handle all scenarios
b) Trigger should not hold any logic--bulk of the logic should be done outside of the core trigger code
     in a seperate class
c) If your trigger has an after update then make sure that you are covering for recursive trigger traps
by using a boolean variable at the start of the execution

Write your trigger with the most basic of codes:

trigger OpportunityTrigger on Opportunity (after insert, after update) {
  if(Trigger.isAfter && Trigger.isInsert) {
    OpportunityTriggerHandler.handleAfterInsert(Trigger.new);
  } else if(Trigger.isAfter && Trigger.isUpdate) {
    OpportunityTriggerHandler.handleAfterInsert(Trigger.new, Trigger.old);
  }
}

Do all the handling in 

public class OpportunityTriggerHandler {
  public static void handleAfterInsert(List opps) {
    // handler logic
  }
}

Over time as your logic increases it will be a lot readable and maintainable.

Admin: Dataloading: Uploading Attachments to Salesforce

Occassionally there may be a need to upload PDF files from other systems and also have them attach to the respective transaction records in Salesforce.

Solution

Dataloader can be used to load attachments and have them associated to their corresponding transaction as well.


To upload attachments:
  1. Confirm that the CSV file you intend to use for attachment importing contains the following required columns (each column represents a Salesforce.com field):
    • ParentId - the Salesforce.com ID of the parent record.
    • Name - the name of the attachment file, such as myattachment.jpg.
    • Body - the absolute path to the attachment on your local drive.
      Ensure that the values in the Body column contain the full file name of the attachments as they exist on your computer. For example, if an attachment named myattachment.jpg is located on your computer at C:\Export, Body must specify C:\Export\myattachment.jpg. Your CSV file might look like this:
      ParentId,Name,Body
      50030000000VDowAAG,attachment1.jpg,C:\Export\attachment1.gif
      701300000000iNHAAY,attachment2.doc,C:\Export\files\attachment2.doc
    The CSV file can also include other optional Attachment fields, such as Description.