Thursday, 23 June 2022

Super Simple Way To Check SOQL Execution Time

 In many scenario's we come across to know the query execution time which mainly helps in performance tuning and Improving the code not to fall under any governor limits.

Ex: In Production around with lakhs of record how the query performing? If we know the execution time then can think of improvisation.

Step1 : Login To Salesforce --> Developer Console --> Debug --> Select "Change Log Levels" --> General Trace Setting For You --> Debug Level Action --> Add/Change --> It opens "Change Debug Level" Pop Up -->Set "Profiling" --> "Finest"


Step 2 : Write the query in anonymous window for which we wanted to know the execution time and then verify at the end of execution log to get the execution time details.




Misleading Error Messages / Not Enough Details For Error Messges

 

System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Task.Whatid

Most of the times we miss to add that field in selection and try to use but in our case its in selection still got that error.

Real Time Scenario We faced this issue when new task field is added on the existing code and Trying to deploy to higher environment.& It took a while to figure out the root cause because same code was working earlier and above mentioned "WhatId" is coming in the SOQL query as well.

Root Cause : Adding the new activity field permission in admin profile which is causing issue, By default activity fields will have access to admin profile not required to mention explicitly and by mentioning explicitly for the new added filed it overridden and has access to only new field and other fields are getting above mentioned error.

Ex: In admin profile under field permissions added readable & edit permission.
<field>Activity.NewCustomField__c</field>

Fix : 
  1. Remove the entries from the package profile file
  2. Change the activity to task    Ex: <field>Task.NewCustomField__c</field>
Troubleshoot & Investigation Steps :
1. Issue occurred during the deployment to higher environment and Deployment happens though api user as per our configuration & It's profile is admin profile .
2. Verify the admin profile changes in the PR & Try to revert and validate the PR.
3. If PR validation success then good to understand the root cause ,If still issue occurs validate the changes incrementally to identify the issue.

Tuesday, 14 June 2022

Concurrent Apex Errors - Exp

This issue we face generally when we have long running jobs.

Which one considers as long running job ?

In this context ,Any Synchronous Apex  runs > 5 seconds considers as long running job. 

When we get this error ?

Ex: If in your org governor limit allowed to run 10 concurrent jobs execute at a time and you get 11th job then you see this issue.

Solution ?

Try to move it to asynchronous wherever possible

How to identify which job / Apex causing this issue ?

It will be challenging to identify if you have larger code base /complex business functionality, So raise request to Salesforce to provide the root cause.

In our case we have signed for signature support and Part of proactive monitoring the details of which apex causing issue is shared.

If you don't fix this concurrent apex issue ,What happens ?

It will lead to performance issues & Fails to process the request which suppose to process can lead to business impact.

What is the apex causing issue in our case?

 Inefficient SOQL Query : One of the SOQL query is taking 

Avg Run Time : 9 Seconds

Max Run Time : 18 seconds

Note : This query is been used in frequent polling of our logic ,So definitely required to fix. 

What is the solution for " Inefficient SOQL Query " ?

  1. Verify the query fields & Filter conditions - Query only required fields & Filter should be on indexed fields
  2. Verify the volume of table data - Always have the delete /purge jobs for older data and keep the table light
  3. Enabling the indexing can be done with external id enablement for quick thing
The volume numbers in our case ,Causing long running ?
Around 11 Lakhs records & 3 fields on the filter condition only 1 field is indexed.

Solution : 
  1. Re-Arrange the query to have indexed fields take precedence in the filter condition.
  2. The other filter field is of data type "text area" so that cant be marked as external id to get the benefit of indexing, So 1st convert the data type "text area" to "text" by seeing the impact none of the existing records are >80 char length and future also not expected the same. Then mark that as external id .
  3. Another field is "Boolean" ,So this is not supported type of "External Id" & Need to re-thing enabling the index of this field really going to improve performance.
    • Ex: if the records are 50% yes and 50% no, It doesn't make sense to index this field ?
  4. In our case it's not required to index Boolean field

Monday, 13 June 2022

System.LimitException: Regex too complicated

I came across this issue recently in production & The root cause is the length of the string which used in the regex.

Our use case it should verify combination of 2 words in received string & It started failing if the received string length is >369 characters

Ex: Card , Delivery keywords should be verified in the received string and it could be of any order. 

Actual Issue : Its hitting the heap size error 6 MB for synchronous 

Solution :

1. Move the logic to asynchronous as heap size will be 12 MB.

But in our case its not possible to move the asynchronous, Because its should be real time on every record insert this need to be tested and batch job cant be run that many time and future method also has 24 hrs limitation 250000 and on every record entry if that future method called that also hits another governor limit.

2.Split the message and verify the keywords in the smaller chunk message, This approach has been taken and working fine.