You need to sign in to do that
Don't have an account?

Apex CPU time limit exceeded ..
Hi friends,
I have list of records,when i click the particular records it display the details of records.some records not display it's loading too long time after that am getting err like this.. Apex CPU time limit exceeded and An unexpected error has occurred. Your development organization has been notified. why this err occur how to resolve this please can anyone help me..thanks in advance..
Regards,
kathir
Becoz your Apex execution time is too long, it's limit exceeded that's why error thrown. The salesforce limit exceeded.
The Maximum CPU time on the salesforce servers - 10,000 milliseconds (Synchronous limit) 60,000 milliseconds(Asynchronous limit)
Hi kodi,
Thanking you for your response.it is possible resolve..i need more explanation...
Regards,
kathir
SInce your code execution takes a lot of time than allowed limit, try refining your code by removing unnecessary for loop, for loop inside for loop. Utilize sets,maps advantages and refine your code. Also you can move the piece of code that does not need to be executed in the same context to future method.
By doing everything mentioed above you can overcome this issue. Hope this helps
Thanks,
George
Visit My blog here
Please remove any unwanted code, query has inside for loop, query has inside contructor.
Review ur code and follow salesforce best practice.
Hi,
If i any correction here below code please help me..
string QueryString='';
grdData = new String[]{};
set<id> transId = new set<id>();
List<Transaction_Relationship__c> lstTransactinRelation = [Select t.Transaction__r.Id from Transaction_Relationship__c t where (( t.RecordType.Name='Buyer/Investor' or t.RecordType.Name='Seller' or t.RecordType.Name='IPO') and t.Transaction__r.Online__c ='Yes' and t.Transaction__c <> '')];
for(Transaction_Relationship__c d : lstTransactinRelation)
{
transId.add( d.Transaction__r.Id);
}
Regards,
kathir
Hi friends,
how do i find out unwanted code inside the for loop can you explain more..
Regards,
kathir
Please post the complete code
Hi,
Following the best pracitces, I have modified your code below. I have moved your query into for loop, which will make processing efficient. Also, for RecordType check, now I am checking on RecordTypeId instead of RecordType.Name.
Let me kow if you need more info on amendmant in above code.
Happy to help you!
Hi friend,
Thanku for reply,your code working fine but am getting same err apex cpu limit exceed..here below my small part of code.. what should i change please help me...
regards,
kathir
Hi,
I think the culprit is you "SearchQuery", could you please share where you are using it and how?
Happy to help you!
Hi,
Here attach my searc query code..
Regards,
kathir
Hi Digamber,
please help me what do i change my code and how can i avoid cpu limit exceed err..thanks in advance..
Regards,
kathir
I can see there is a FOR SOQL inside another FOR SOQL, which may be pain point. Just for testing purpose, could you please comment out below line and see if proble still persists
Happy to help you!
Hi Digamber,
Thanku for ur reply.Actually i comment some line that means what you are point out the line,code is working but getting same err.why this same err come again and again.Actually am trying to do this last 3 days but i did not get output.please help me..
Regards,
kathir
Hi,
I took a look and can see you are using FOR log to query, but it will eventually return all records and then process them one by one:-
Insted of this, it should be modified something like:-
The main problem seems to be your object has quite big and you are processing multiple objects in one go.
Lets give it a try and let me know if it help you!
Happy to help you!
Hi Digamber,
I have changed for loop given below.but am getting err Variable does not exist: e.Announce_Date__c at line
//for(Transaction__c e : Database.query(SearchQuery))
for(List<Transaction__c> lstTransaction : Database.query(SearchQuery))
{
DateTime dte = e.Announce_Date__c; // this line err
string BuyerInvestor='';
string str=QueryString.replace('\'','');
if(e.Target__r.Name =='null' || e.Target__r.Name ==null )
e.Target__r.Name = '';
Regards,
kathir
Hi,
You will need to put an extra for loop, something like below:-
Let me know if you have any question.
Happy to help you!
Hi Digamber,
code is working fine but getting same err like this apex CPU time limit exceeded.then what we have change please help me Digaber.
Regards,
kathir
Do you have skype ID, where we can discuss it. Screen share will help.
Hi Digamber,
When i click the list of records some record is display, some record not display getting CPU limt err..
Regards,
kathir
Hi,
I understand it. You are getting this error, because probably your controller class is taking too long to process all the data. Having skype Id will help to look at your org and see exactly what you are trying to do.
Hi Digamber,
Dont mind Digamber am not having skype id now,actually i will create skype id through in my concern today evening.If i get skype id let i know you sure. Please help and keep it touch with me.thanking you.
Regards,
kathir
Hi Digamber,
Have i send my whole controller code now.
Regards,
kathir
Hi Digamber,
Please give me suggestion how to resolve time limit err.
Regards,
kathir
An unexpected error has occurred. Your solution provider has been notified. (Database)
He friends,
Am very difficult to handling this task but i did not get exact output.please can any one help me here below my database code..
Regards,
kathir
Just as a cosmetic/debugging suggestion - one thing you could do to increase readability
is also to use maps/sets to validate your logic.
ie:
Then iterate using the maps
ie, This method prevents all of the transaction relationships records from being queried, for every transaction resulting from the search query.
Here's one fixed known issue: https://success.salesforce.com/issues_view?id=a1p3A000001RXBZQA4&title=sobjecttype-getdescribe-and-sobjectfield-getdescribe-increase-apex-cpu-consumption-in-api-version-44
And another: https://success.salesforce.com/issues_view?id=a1p3A0000008q7VQAQ&title=apex-cpu-time-limit-is-not-enforced-if-an-apex-test-runs-in-synchronous-mode
I think the latter is more relevant to our issue but we're not concerned about tests - it's actual processes in production. Anyone have any insights?
I am getting the below error pls help me in the mail while booking(student) the course by annual/biannual ? Not able to reproduce the case in sandbox?
GET RECORDS: BookingLookup
Find one CampaignMember record where:
Id Equals {!CampaignMemberID} (00v1r00002f2Ym4AAE)
Result
Successfully found record.
{!CourseDepartment} = Singing
{!CourseSubject} = Choirs
UPDATE RECORDS: UpdateAccountRecord
Find all Account records where:
Id Equals {!StudentAccountID} (001w000001e9z30)
Update the records’ field values.
Acting_Notificationsc = {!Acting} (false)
Art_Drawing_Notificationsc = {!ArtDrawing} (false)
Comedy_Notificationsc = {!Comedy} (false)
Dance_Notificationsc = {!Dance} (false)
Filmmaking_Notificationsc = {!Filmmaking} (false)
Music_Notificationsc = {!Music} (false)
Musical_Theatre_Notificationsc = {!MusicalTheatre} (false)
Photography_Notificationsc = {!Photography} (false)
Singing_Notificationsc = {!Singing} (true)
Writing_Notificationsc = {!Writing} (false)
Result
Failed to update records that meet the filter criteria.
Error Occurred: The flow tried to update these records: null. This error occurred: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: wbsendit.AccountTrigger: System.LimitException: Apex CPU time limit exceeded. You can look up ExceptionCode values in the SOAP API Developer Guide.
Previously when I used to get APEX CPU time limit error when running a test class two things i used to do.
- Move test.starttest to a different position.
- Or make smaller chunk methods so that each method covers a part of the code.
Today when I was trying to fix this.
- I have to create new test class instead of separated methods.
Did any one face similar issue.
Srikar, are you referring to having to create a test class instead of using test methods within your functional class?
If so, that was a change salesforce made quite a while ago. Test methods must be in a separate test class.
I am facing Apex cpu time limit error .what i have to change in below code. please help me
public List<salesforcerequestdocumentSfaccountreque.Contract> buildContractClassHierarchy (SB_Account_Letter__c serviceLetter, list<Asset__c> assetList, SB_Ref_Table__c letterDetails, list<Contract_c__c> selectedcontractList)
{
list<salesforcerequestdocumentSfaccountreque.Contract> result = new list<salesforcerequestdocumentSfaccountreque.Contract>();
set<Id> contractSet = new set<Id>();
list<Contract_c__c> allContracts = new list<Contract_c__c>();
list<Asset__c> allAssets = new list<Asset__c>();
list<Buyout__c> allBuyouts = new list<Buyout__c>();
if (letterDetails.Class__c == '7')
{
for (Contract_c__c sc :selectedcontractList)
{
contractSet.add(sc.id);
}
}
for (Asset__c a: assetList){
contractSet.add(a.Contract_Number__c);
}
allContracts = this.getContractList(serviceLetter, contractSet, letterDetails);
allAssets = this.getAssetList(allContracts, assetList);
allBuyouts = this.getBuyoutList(contractSet);
for (Contract_c__c c: allContracts)
{
salesforcerequestdocumentSfaccountreque.Contract cVO = new salesforcerequestdocumentSfaccountreque.Contract();
cVO.Assets = new salesforcerequestdocumentSfaccountreque.Assets();
cVO.Assets.Asset = new list<salesforcerequestdocumentSfaccountreque.Asset>();
populateContractInfo (c, cVO);
System.debug('medar heap after populateContractInfo call: ' + Limits.getHeapSize() );
for (Asset__c a: allAssets)
{
if (c.Id == a.Contract_Number__c)
{
salesforcerequestdocumentSfaccountreque.Asset aVO = new salesforcerequestdocumentSfaccountreque.Asset();
System.debug('medar heap before populateAssetInfo call:' + Limits.getHeapSize() );
aVO = this.populateAssetInfo (a);
System.debug('medar Heap after populateAssetInfo call: ' + Limits.getHeapSize() );
system.debug('medar Heap size is: ' + Limits.getHeapSize());
cVO.Assets.Asset.add (aVO);
System.debug('medar after cVO.Assets.Asset.add (aVO)' );
system.debug('medar Heap size is before null aVO: ' + Limits.getHeapSize());
aVO = null;//medar attempting to reduce heap
system.debug('medar Heap size is after null aVO: ' + Limits.getHeapSize());
}
}
if (letterDetails.Class__c == '2')
{
cVO.Buyouts = new salesforcerequestdocumentSfaccountreque.Buyouts();
cVO.Buyouts.Buyout = new list<salesforcerequestdocumentSfaccountreque.Buyout>();
for (Buyout__c b: allBuyouts)
{
if (c.Id == b.Contract__c)
{
salesforcerequestdocumentSfaccountreque.Buyout bVO = new salesforcerequestdocumentSfaccountreque.Buyout();
System.debug('medar before bVO = this.populateBuyoutInfo(b)' );
bVO = this.populateBuyoutInfo(b);
System.debug('medar after bVO = this.populateBuyoutInfo(b)' );
cVO.Buyouts.Buyout.add(bVO);
system.debug('medar Heap size before bVO is nulled: ' + Limits.getHeapSize());
bVO = null;//medar trying to reduce heap
system.debug('medar Heap size after bVO is nulled: ' + Limits.getHeapSize());
System.debug('medar after cVO.Buyouts.Buyout.add(bVO);' );
}
}
}
System.debug('medar before result.add(cVO);' );
result.add(cVO);
System.debug('medar after result.add(cVO);' );
}
return result;
}