Most Repeated Test Class Failures during Deployments

Screen Shot 2018-12-30 at 9.36.21 AM

Today I am posting about What are the most repeated test class issues we face while deploying components from source to destination org and its solutions…

1.System.LimitException: Too many SOQL queries: 101 

  • Avoid SOQL queries that are inside FOR loops.
  • Make sure you don’t have a recursive loop calling a SOQL.
  • Do not do any DML/CRUD inside a for loop.
  • Avoid more than one DML on single object in single transaction – because each DML invokes trigger of related object can cause addition of SOQL count.
  • Avoid too many field updates in process builders in different conditional branches. Each branch runs a DML and initiate trigger execution.
  • If there are lot of business processes and logic in your trigger then design and isolate processes with help of future and batch if needed.
  • Recursion Handling – To ensure a trigger is not running recursively.
  • Minimize the No.of SOQLs by merging the queries – we will merge the SQL query of single object in a single query if possible. For Example:Opportunity op = [select StageName, Account.OwnerId From Opportunity where id: opId];
    List olis = [select id, productId from OpportunityLineItem where OpportunityId =: op.Id];

Refer For more information:

https://help.salesforce.com/articleView?id=000181404&type=1

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm

2.System.LimitException: Too many query rows: 50001

 

Because Apex runs in a multitenant environment, the Apex runtime engine strictly enforces limits to ensure that runaway Apex code or processes don’t monopolize shared resources. If some Apex code exceeds a limit, the associated governor issues a runtime exception that cannot be handled. These limits count for each Apex transaction. For Batch Apex, these limits are reset for each execution of a batch of records in the execute method.

There is a limit to the number of records that that can be retrieved  by the SOQL Query which is 50000 records. Attempting to query the records having the number more than this will give the error which you are getting.To query the records more than its limit, you need to use a Batch Class which provides a fresh limit of 50k records by resetting the limits.

Follow the Best practices:

https://developer.salesforce.com/page/Apex_Code_Best_Practices

3. First exception on row 0; first error: ENTITY_IS_DELETED, entity is deleted: [] 

 

This error we can avoid by following below….

  1. Avoid hardcoding of Id’s in your class. Ex:- Entity Id’s
  2. Make sure not use ‘ALL ROWS ‘ Keyword.

The ALL ROWS keyword can be used to query all records in an organization, including deleted records and archived activities. So, in other words, the usage of the ALL ROWS keyword resulted in fetching some Account records that were in the DELETED state or in simpler words – they were in the Recycle Bin. And this DELETED Account record was assigned to the AccountId Lookup field on the Contact which is not allowed.

3. use Database.delete(recordToDelete, allOrNone)

Refer:- https://salesforce.stackexchange.com/questions/134150/how-to-bypass-entity-is-deleted

4. System.NullPointerException: Argument 1 cannot be null 

simpson-doh.jpg

For these kind of Error we must need to Debug the code you written..

you must be getting null value. we have to do null check before adding relationship to List/Map Option.

Before u calling your method create dummy data.

5. System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, SampleTrigger: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id xxxxxxxxxxxxxx; first error: 

In Setup’s “Apex Test Execution” page there is an “Options” button that includes a “Disable Apex Parallel Testing” checkbox. Checking that will probably avoid the problem.

Follow Context Variable considerations to find the solution for this issue.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables_considerations.htm

The execution of a trigger will sometimes generate an error message, “execution of [EventName] caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.[TriggerName]: line [XX], column [XX]

This error is caused by a line of code that is trying to use an object that has not been instantiated, or an object’s attribute that has not been initialized.

NOTE: If the field Site was left empty, it will generate the error message as well.

Knowledge Article: https://help.salesforce.com/articleView?id=000063739&type=1

6. System.QueryException: List has no rows for assignment to SObject 

This mean your query not returning any values..

 Its always a best practice to check if list is not empty before doing any DML operation.

Salesforce throws this error when you assign a query result to a singular instance of an object. If you were assigning to a list (or map) this would not be the case, you would just be given an empty list.

To solve your current issue you should use a try/catch block around the query. Your assignment would be in the try and probably post a page message in the catch indicating to the user what the problem was.

try{
    account = [YOUR QUERY];
} catch (Exception e) {
    // add your message to the user or
    account = new Account();
}

Refer this knowledge Article:

https://help.salesforce.com/articleView?id=000159853&type=1

7. This custom field is referenced elsewhere in salesforce.com. : Field Updates.

download (1).jpeg

You cannot delete any field that has been used somewhere. Firstly you have to remove all the reference to that field and then delete it.
To see, where the field is used, click on delete it will take you to the page, as you have in the image attached.
Click on Apex class and then edit that class, remove all the reference to that field and delete it.

8.Field is not writeable:

We will see this error when assigning the custom object to the master standard object.

For Example:

Invoice__c inv = new Invoice__c();
inv.Account__c = accountId;
insert inv;

Then we get this error.

Field is not writable : Invoice__c.Account__c

The fix was actually easy. we  just need to rewrote it to assign the master id on instantiating the child object.

Invoice__c inv = new Invoice__c(Account__c = accountId);
insert inv;

 

Thanks

(Author : Jayakrishna Ganjikunta)

Advertisements

2 thoughts on “Most Repeated Test Class Failures during Deployments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s