Friday, August 12, 2022

Types of Apex Errors in Salesforce

 Get Familiar with Apex Errors!

Humans are bound to make some mistakes. We all face some errors while we code. It is perfectly fine if errors are encountered.

The main thing which we need to focus here is why do we get such exceptions and how can we resolve them.

  1. List has no rows for assignment to SObject: When query doesn't return any record. Check your query and put appropriate filters.
  2. List has more than 1 row for assignment: When query return more than 1 record, so use a list of Sobject to get the result. 
  3. Uncommitted work pending: When a DML is performed before the call-out. Avoid doing the DMLs before a callout or use future method.
  4. CPU time limit exception: A synchronous transaction will have limit 10 secs to complete. If it is taking more time then this error will come. If possible use asynchronous methods like future, queueable apex or batch.
  5. Mixed DML Operation: When there is a DML in setup and non setup object records in a same transaction. To avoid this one of the DML can be in future method.
  6. Too many SOQL Queries: When more than 100 SOQL queries are fired in a transaction. Use Limit class to get the number of queries at a particular line and get the details to fix it. 
  7. Sobject row was retrieved via SOQL without querying the requested field: When the query is written without adding the required fields in the query and then you are using those fields to assign values, then this will error will come.
  8. INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []: When you are giving wrong salesforce id to update the record. Example you are updating account record and somehow contact’s Id is mapped with account Id field.



    Please share the blog and leave a comment about your thoughts.
    Click here for more blogs.

No comments:

Post a Comment

Mostly Viewed