function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kathir DevanKathir Devan 

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 notifiedwhy this err occur how to resolve this please can anyone help me..thanks in advance..

 

Regards,

kathir

KodiKodi
Hi,

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)
Kathir DevanKathir Devan

Hi kodi,

 

Thanking you for your response.it is possible resolve..i need more explanation...

 

Regards,

kathir

georggeorg

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

KodiKodi
Hi Kathir,

Please remove any unwanted code, query has inside for loop, query has inside contructor.
Review ur code and follow salesforce best practice.
Kathir DevanKathir Devan

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

 

 

VaasuVaasu
From where you are calling this code?
Kathir DevanKathir Devan

Hi friends,

 

how do i find out unwanted code inside the for loop can you explain more..

 

 

Regards,

kathir

georggeorg

Please post the complete code

digamber.prasaddigamber.prasad

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.

 

Set<Id> setRecordTypeIds = new Set<Id>();
Schema.DescribeSObjectResult d = Schema.SObjectType.Transaction_Relationship__c; 
Map<String,Schema.RecordTypeInfo> rtMapByName = d.getRecordTypeInfosByName();
setRecordTypeIds.add(rtMapByName.get('Buyer/Investor').getRecordTypeId());
setRecordTypeIds.add(rtMapByName.get('Seller').getRecordTypeId());
setRecordTypeIds.add(rtMapByName.get('IPO').getRecordTypeId());
string QueryString='';
grdData = new String[]{};
set<id> transId = new set<id>();
for(List<Transaction_Relationship__c> lstTransactinRelation : [Select t.Transaction__r.Id from Transaction_Relationship__c t where (t.RecordTypeId in: setRecordTypeIds and t.Transaction__r.Online__c ='Yes' and t.Transaction__c <> '')])
{
	for(Transaction_Relationship__c d : lstTransactinRelation)
	{
		transId.add( d.Transaction__r.Id);
	}
}

 

Let me kow if you need more info on amendmant in above code.

 

Happy to help you!

 

 

Kathir DevanKathir Devan

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...

   public void BindGrid()
    {  
         strShow='';      
         try
          { 
            
            if(SearchCountry == null || SearchCountry == '')
             {
              strCheck='NA';
             }
             else if(SearchText == '' || SearchText == null)
             {
                
                 if(strCheck.equals('Ch'))
                 {
                     SearchText = SearchCountry;
                     SearchCountry='';                     
                 }                
                 
                 strCheck='NA';
             }
             else
             {
                          
                if(strCheck.equals('Ch'))
                 {
                     SearchText = SearchCountry;
                 }                  
                 else
                     SearchText =SearchText ;
                 
                 
                  SearchCountry='';
                  strCheck='NA';
             }          
           
Set<Id> setRecordTypeIds = new Set<Id>();
Schema.DescribeSObjectResult d = Schema.SObjectType.Transaction_Relationship__c; 
Map<String,Schema.RecordTypeInfo> rtMapByName = d.getRecordTypeInfosByName();
setRecordTypeIds.add(rtMapByName.get('Buyer/Investor').getRecordTypeId());
setRecordTypeIds.add(rtMapByName.get('Seller').getRecordTypeId());
setRecordTypeIds.add(rtMapByName.get('IPO').getRecordTypeId());
string QueryString='';
grdData = new String[]{};
set<id> transId = new set<id>();
for(List<Transaction_Relationship__c> lstTransactinRelation : [Select t.Transaction__r.Id from Transaction_Relationship__c t where (t.RecordTypeId in: setRecordTypeIds and t.Transaction__r.Online__c ='Yes' and t.Transaction__c <> '')])
{
    for(Transaction_Relationship__c d1 : lstTransactinRelation)
    {
        transId.add(d1.Transaction__r.Id);
    }
}
           /* 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);
             }*/
 //Query to display an transaction details
            string SearchQuery='Select t.Target__r.Id,t.Target__r.Name,t.Id,t.Amount__c, t.Announce_Date__c, t.Target__c, t.Target_Sector2__c, t.Transaction_Headline__c, t.Type2__c,t.Profile_Link__c  from Transaction__c t where (t.Id in : transId) ';
            system.debug('SearchQuery@@@@@@@@@@@@@@@@@@@@@@@@@@'+SearchQuery);
            if(SearchText == '$')
            {
                SearchText = '\'$';
            }
            if(SearchText != null || SearchText != '')
            { 
                if(trantypePDF == true)
                {
                
                    if(url == '/apex/transactionsearch' || url == '/apex/transactionsearch?var=mmf' || url =='/apex/transactionsearch?sub=aggmaf')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery + 'and ((t.Profile_Link__c  like \'%' + SearchText + '%\') )';
                    }
                    else if(url =='/apex/transactionsearch?sub=Bankmaf')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery + 'and ((t.Profile_Link__c  like \'%' + SearchText + '%\') )  and t.Target_Sector2__c LIKE :bank';
                    }
                    else if(url =='/apex/transactionsearch?sub=paymaf')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery + 'and ((t.Profile_Link__c  like \'%' + SearchText + '%\') )  and t.Target_Sector2__c LIKE :pay';
                    }
                    else if(url =='/apex/transactionsearch?sub=secmaf')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery + '   and ((t.Profile_Link__c  like \'%' + SearchText + '%\') ) and t.Target_Sector2__c LIKE :sec';
                    }
                    else if(url =='/apex/transactionsearch?sub=insmaf')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery + 'and ((t.Profile_Link__c  like \'%' + SearchText + '%\') ) and t.Target_Sector2__c LIKE :ins';
                    }
                    else if(url =='/apex/transactionsearch?sub=helmaf')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery + 'and ((t.Profile_Link__c  like \'%' + SearchText + '%\') ) and t.Target_Sector2__c LIKE :hel';
                    }
                    else if(url =='/apex/transactionsearch?sub=fbpmaf')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery + ' and ((t.Profile_Link__c  like \'%' + SearchText + '%\')) and t.Target_Sector2__c LIKE :fsbo';
                    }
                    //textbox search for transaction end here
                    //textbox search for MA start here
                   else  if(url =='/apex/masearch?var=ma' || url =='/apex/masearch?sub=aggma')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery + ' and ((t.Profile_Link__c  like \'%' + SearchText + '%\') )   and  t.Type2__c LIKE :maa';
                    }
                    else  if(url =='/apex/masearch?sub=Bankma')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery + 'and ((t.Profile_Link__c  like \'%' + SearchText + '%\') ) and  t.Type2__c LIKE :maa and t.Target_Sector2__c LIKE :bank';
                    }
                    else  if(url =='/apex/masearch?sub=payma')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery + 'and ((t.Profile_Link__c  like \'%' + SearchText + '%\') ) and  t.Type2__c LIKE :maa and t.Target_Sector2__c LIKE :pay';
                    }
                    else if(url =='/apex/masearch?sub=secma')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery + ' and ((t.Profile_Link__c  like \'%' + SearchText + '%\') ) and  t.Type2__c LIKE :maa and t.Target_Sector2__c LIKE :sec';
                    }
                    else if(url =='/apex/masearch?sub=insma')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery + 'and ((t.Profile_Link__c  like \'%' + SearchText + '%\') ) and  t.Type2__c LIKE :maa and t.Target_Sector2__c LIKE :ins';
                    }
                     else if(url =='/apex/masearch?sub=helma')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery + 'and ((t.Profile_Link__c  like \'%' + SearchText + '%\') ) and  t.Type2__c LIKE :maa and t.Target_Sector2__c LIKE :hel';
                    }
                   else  if(url =='/apex/masearch?sub=fbpma')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery + ' and ((t.Profile_Link__c  like \'%' + SearchText + '%\')) and  t.Type2__c LIKE :maa and t.Target_Sector2__c LIKE :fsbo';
                    }
                    //textbox search for MA end here
                    //textbox search for Financing start here
                    else if(url=='/apex/MAFinancing?var=fin' || url=='/apex/MAFinancing?sub=aggfin')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery + ' and ((t.Profile_Link__c  like \'%' + SearchText + '%\')) and  t.Type2__c LIKE :finn';
                    }
                    else if(url =='/apex/MAFinancing?sub=Bankfin')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery + 'and ((t.Profile_Link__c  like \'%' + SearchText + '%\')) and  t.Type2__c LIKE :finn and t.Target_Sector2__c LIKE :bank';
                    }
                      else if(url =='/apex/MAFinancing?sub=payfin')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery + 'and ((t.Profile_Link__c  like \'%' + SearchText + '%\')) and  t.Type2__c LIKE :finn and t.Target_Sector2__c LIKE :pay';
                    }
                    else if(url =='/apex/MAFinancing?sub=secfin')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery + 'and ((t.Profile_Link__c  like \'%' + SearchText + '%\')) and  t.Type2__c LIKE :finn and t.Target_Sector2__c LIKE :sec';
                    }
                    else if(url =='/apex/MAFinancing?sub=insfin')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery + 'and ((t.Profile_Link__c  like \'%' + SearchText + '%\')) and  t.Type2__c LIKE :finn and t.Target_Sector2__c LIKE :ins';
                    }
                     else if(url =='/apex/MAFinancing?sub=helfin')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery + 'and ((t.Profile_Link__c  like \'%' + SearchText + '%\')) and  t.Type2__c LIKE :finn and t.Target_Sector2__c LIKE :hel';
                    }
                    else if(url =='/apex/MAFinancing?sub=fbpfin')
                    {
                    SearchText= 'pdf';
                    SearchQuery = SearchQuery +'and ((t.Profile_Link__c  like \'%' + SearchText + '%\')) and  t.Type2__c LIKE :finn and t.Target_Sector2__c LIKE :fsbo';
                    }
                    if(SearchText != null && SearchText != '')
                    QueryString =QueryString + ';' +'Key:' + SearchText;
              
                }

 regards,

kathir

digamber.prasaddigamber.prasad

Hi,

 

I think the culprit is you "SearchQuery", could you please share where you are using it and how?

 

Happy to help you!

Kathir DevanKathir Devan

Hi,

Here attach my searc query code..

//advance search in checkbox            
            transactionPDF='';
            transactionStatus='';
            if(tranStatusPending)
            {
                    if(transactionStatus=='')
                        transactionStatus='\'Pending\'';
                    else
                        transactionStatus=transactionStatus+','+'\'Pending\'';
            }
            if(tranStatusClosed)
            {
                    if(transactionStatus=='')
                        transactionStatus='\'Closed\'';
                    else
                        transactionStatus=transactionStatus+','+'\'Closed\'';
            }
            if(tranStatusCancelled)
            {
                    if(transactionStatus=='')
                        transactionStatus='\'Cancelled\'';
                    else
                        transactionStatus=transactionStatus+','+'\'Cancelled\'';
            }
          
          //check the condition of advance search
            List<Transaction__c> que = Database.query(SearchQuery);
            listSize = que.size();
            system.debug('_______llllllllll__________'+listSize +offset+queryLimit);
             if(listSize > offset+queryLimit)
               lastOff = true;
               else 
               lastOff = false;
            if( ( transactionStatus != null) ||  ( StartDate != null) || (EndDate != null) || (excludeAmount != true) || (amountMin != null) || (amountMax != null))
             {
                    if( transactionStatus != null && transactionStatus != '')
                    {
                     SearchQuery = SearchQuery+ ' and(t.Transaction_status__c in ('+transactionStatus+')) ';
                     QueryString=QueryString +';' +'TS:'+ transactionStatus;
                   
                    }
                   
                  if( StartDate != null && EndDate != null && StartDate != '' && EndDate != '' )
                    {
                    if(url == '/apex/transactionsearch' || url == '/apex/transactionsearch?var=mmf' || url =='/apex/transactionsearch?sub=aggmaf')
                    {
                     SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+'))';
                    }
                       
                      else if(url =='/apex/transactionsearch?sub=Bankmaf')
                    {
                  
                    SearchQuery = SearchQuery + ' and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and t.Target_Sector2__c LIKE :bank';
                    }
                     else if(url =='/apex/transactionsearch?sub=paymaf')
                    {
                  
                    SearchQuery = SearchQuery + ' and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and t.Target_Sector2__c LIKE :pay';
                    } 
                    else if(url =='/apex/transactionsearch?sub=secmaf')
                    {
                    
                    SearchQuery = SearchQuery + ' and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and t.Target_Sector2__c LIKE :sec';
                    }
                    else if(url =='/apex/transactionsearch?sub=insmaf')
                    {
                   SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and t.Target_Sector2__c LIKE :ins';
                    }
                    else if(url =='/apex/transactionsearch?sub=helmaf')
                    {
                   
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and t.Target_Sector2__c LIKE :hel';
                    }
                    else if(url =='/apex/transactionsearch?sub=fbpmaf')
                    {
                   
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and t.Target_Sector2__c LIKE :fsbo';
                    }
                    //textbox search for transaction end here
                    //textbox search for MA start here
                   else  if(url =='/apex/masearch?var=ma' || url =='/apex/masearch?sub=aggma')
                    {
                    
                    SearchQuery = SearchQuery + ' and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+'))  and  t.Type2__c LIKE :maa';
                    }
                    else  if(url =='/apex/masearch?sub=Bankma')
                    {
                    
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :maa and t.Target_Sector2__c LIKE :bank';
                    }
                    else  if(url =='/apex/masearch?sub=payma')
                    {
                    
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :maa and t.Target_Sector2__c LIKE :pay';
                    }
                    
                    else if(url =='/apex/masearch?sub=secma')
                    {
                   
                    SearchQuery = SearchQuery + ' and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :maa and t.Target_Sector2__c LIKE :sec';
                    }
                    else if(url =='/apex/masearch?sub=insma')
                    {
                    
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :maa and t.Target_Sector2__c LIKE :ins';
                    }
                    else if(url =='/apex/masearch?sub=helma')
                    {
                    
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :maa and t.Target_Sector2__c LIKE :hel';
                    }
                   else  if(url =='/apex/masearch?sub=fbpma')
                    {
                    
                    SearchQuery = SearchQuery + ' and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :maa and t.Target_Sector2__c LIKE :fsbo';
                    }
                    //textbox search for MA end here
                    //textbox search for Financing start here
                    else if(url=='/apex/MAFinancing?var=fin' || url=='/apex/MAFinancing?sub=aggfin')
                    {
                    
                    SearchQuery = SearchQuery + ' and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :finn';
                    }
                    else if(url =='/apex/MAFinancing?sub=Bankfin')
                    {
                    
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :finn and t.Target_Sector2__c LIKE :bank';
                    }
                    else if(url =='/apex/MAFinancing?sub=payfin')
                    {
                    
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :finn and t.Target_Sector2__c LIKE :pay';
                    }
                    else if(url =='/apex/MAFinancing?sub=secfin')
                    {
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :finn and t.Target_Sector2__c LIKE :sec';
                    }
                    else if(url =='/apex/MAFinancing?sub=insfin')
                    {
                   
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :finn and t.Target_Sector2__c LIKE :ins';
                    }
                    else if(url =='/apex/MAFinancing?sub=helfin')
                    {
                   
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :finn and t.Target_Sector2__c LIKE :hel';
                    }
                    else if(url =='/apex/MAFinancing?sub=fbpfin')
                    {
                   
                    SearchQuery = SearchQuery +'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :finn and t.Target_Sector2__c LIKE :fsbo';
                    } 
                      QueryString=QueryString +';' +'SD:'+ StartDate.replace('-','') + ';'+ 'ED:'+ EndDate.replace('-','') ;
                      
                    }
                  if( excludeAmount == true)
                    {
                        SearchQuery = SearchQuery + ' and (t.Amount__c > 0) ';
                        QueryString=QueryString +';' +'EX:'+ excludeAmount ;
                    }
                  if((amountMax != 0 &&  amountMin == 0) || (amountMax != 0))
                    {
                        QueryString=QueryString +';' +'AMi:'+ amountMin + ';' + 'AMa:'+ amountMax ;
                        SearchQuery = SearchQuery + ' and ((t.Amount__c >= '+(amountMin==null?0:amountMin)+' ) and (t.Amount__c <= '+(amountMax==null?0:amountMax)+' )) ';
                    }
               SearchQuery = SearchQuery + ' ORDER BY t.Announce_Date__c Desc LIMIT :queryLimit  OFFSET :offset ';
               system.debug('_____________'+SearchQuery );
            }
            
           

           for(Transaction__c e : Database.query(SearchQuery))
             {                 
                 DateTime dte = e.Announce_Date__c;
                 string BuyerInvestor='';
                 string str=QueryString.replace('\'','');
                 if(e.Target__r.Name =='null' || e.Target__r.Name ==null )
                 e.Target__r.Name = '';
                
                 e.Transaction_Headline__c = SpecialCharHandler(e.Transaction_Headline__c);
                 if( e.Target__r.Name !='' ||  e.Target__r.Name !=null )
                    e.Target__r.Name  = SpecialCharHandler(e.Target__r.Name );
                   
                    if(e.Profile_Link__c=='null' || e.Profile_Link__c==null )
                    e.Profile_Link__c= '';
                 if( e.Profile_Link__c != '' ||  e.Profile_Link__c != null)
                   e.Profile_Link__c = SpecialCharHandler(e.Profile_Link__c);
                 
                  string RltdCompany = '';
                  string strtransId = e.Id;
                  string strprofile = e.Profile_Link__c;
                  integer countRelCompany = 0;
                  for(Transaction_Relationship__c f : [Select r.Related_Company_Id__c,r.Related_Company_Name__c from Transaction_Relationship__c r  where r.Transaction__r.Id =:strtransId and r.RecordType.Name = 'Buyer/Investor' order by r.Related_Company_Name__c asc])
                    {
                       
                       string s = ((f.Related_Company_Name__c == null || f.Related_Company_Name__c =='')?'':f.Related_Company_Name__c) ;
                       if( s != null && s != '')
                       {
                         s = SpecialCharHandler(s);
                         RltdCompany = RltdCompany + '<a target=\'_parent\' href=\'transactionsearch?AccountId='+f.Related_Company_Id__c +'\'>' + s + '</a>' + ';  ';
                         countRelCompany = countRelCompany +1;
                       }
                      
                    }
              
                  if( countRelCompany != 0)
                  {
                      RltdCompany = RltdCompany.substring(0,RltdCompany.length()-3);
                      
                  }
                 //Insert data into the grid
                 
                    if(e.Amount__c == 0.00 || e.Amount__c == NULL )
                    {
                        string s ='';
                        if(str != null && str != '')  
                        {
                           if(e.Profile_Link__c=='null'|| e.Profile_Link__c==null || e.Profile_Link__c=='')// to check the pdf link is empty 
                            {
                                 grdData.add('["'+ dte.format('MM/dd/yyyy') +  '","' + ((e.Type2__c == null || e.Type2__c == '')?'':e.Type2__c) + '", "'+'<a target=\'_parent\' href=\'TransactionDetail?id='+ e.Id +'&keyword='+str+'\'>' + ((e.Transaction_Headline__c == null || e.Transaction_Headline__c == '')?'':e.Transaction_Headline__c)+'</a>' + '", "'+  '<a target=\'_parent\' href=\'TransactionSearch?AccountId='+e.Target__r.Id +'\'>' + e.Target__r.Name + '</a>' + '","'+ RltdCompany + '", "' + e.Target_Sector2__c + '","'+ s + '","'+  '"]');  
                            }
                            else if(e.Profile_Link__c!='null'|| e.Profile_Link__c!=null || e.Profile_Link__c!='')// to check the pdf link is  not  empty 
                            {
                                 grdData.add('["' + dte.format('MM/dd/yyyy') +  '","' + ((e.Type2__c == null || e.Type2__c == '')?'':e.Type2__c) + '", "'+ '<a target=\'_parent\' href=\'TransactionDetail?id='+ e.Id +'&keyword='+str+'\'>' +((e.Transaction_Headline__c == null || e.Transaction_Headline__c == '')?'':e.Transaction_Headline__c)+'</a>'+  '<h5>'+ '(FT Partners profile Available)'+'</h5> ' + '", "'+  '<a target=\'_parent\' href=\'TransactionSearch?AccountId='+e.Target__r.Id +'\'>' + e.Target__r.Name + '</a>' + '","'+ RltdCompany+ '", "' + e.Target_Sector2__c + '","'+ s + '"]');  
                            }
                       }
                        else{                        
                                if(e.Profile_Link__c=='null'|| e.Profile_Link__c==null || e.Profile_Link__c=='') // to check the pdf link is empty 
                                {
                                    grdData.add('["' + dte.format('MM/dd/yyyy') +  '","' + ((e.Type2__c == null || e.Type2__c == '')?'':e.Type2__c) + '", "'+ '<a target=\'_parent\' href=\'TransactionDetail?id='+ e.Id +'\'>' +((e.Transaction_Headline__c == null || e.Transaction_Headline__c == '')?'':e.Transaction_Headline__c) +'</a>'+ '", "'+  '<a target=\'_parent\' href=\'TransactionSearch?AccountId='+e.Target__r.Id +'\'>' + e.Target__r.Name + '</a>' + '","'+ RltdCompany + '","' + e.Target_Sector2__c + '","' + s + '" ]');  
                                }
                                else  if(e.Profile_Link__c !='null'|| e.Profile_Link__c !=null || e.Profile_Link__c !='')// to check the pdf link is not empty 
                                {
                                    grdData.add('["'+ dte.format('MM/dd/yyyy') +  '","' + ((e.Type2__c == null || e.Type2__c == '')?'':e.Type2__c) + '", "'+'<a target=\'_parent\' href=\'TransactionDetail?id='+ e.Id +'\'>' + ((e.Transaction_Headline__c == null || e.Transaction_Headline__c == '')?'':e.Transaction_Headline__c)+'</a>'+  '<h5> '+'(FT Partners profile Available) '+'</h5> ' + '", "'+  '<a target=\'_parent\' href=\'TransactionSearch?AccountId='+e.Target__r.Id +'\'>' + e.Target__r.Name + '</a>' + '","'+ RltdCompany + '","' + e.Target_Sector2__c + '","' + s + '"]');  
                                }
                            }
                    }  
                  
                    else
                    { 
                         if(str != null && str != '')  
                         {     
                          if(e.Profile_Link__c=='null'|| e.Profile_Link__c==null || e.Profile_Link__c=='')// to check the pdf link is empty 
                            {               
                                grdData.add('["'+ dte.format('MM/dd/yyyy') + '","' +  ((e.Type2__c == null || e.Type2__c == '')?'':e.Type2__c) + '", "'+'<a target=\'_parent\' href=\'TransactionDetail?id='+ e.Id +'&keyword='+str+'\'>' + ((e.Transaction_Headline__c == null || e.Transaction_Headline__c == '')?'':e.Transaction_Headline__c)+'</a>' + '", "'+  '<a target=\'_parent\' href=\'TransactionSearch?AccountId='+e.Target__r.Id +'\'>' + e.Target__r.Name + '</a>' + '","'+ RltdCompany + '","' + e.Target_Sector2__c + '","'+ e.Amount__c   + '","'+  ''+ '"]');  
                            }
                            else if(e.Profile_Link__c !='null'|| e.Profile_Link__c !=null || e.Profile_Link__c !='')// to check the pdf link is empty 
                            {               
                                grdData.add('["'+ dte.format('MM/dd/yyyy') + '","' + ((e.Type2__c == null || e.Type2__c == '')?'':e.Type2__c) + '", "'+ '<a target=\'_parent\' href=\'TransactionDetail?id='+ e.Id +'&keyword='+str+'\'>'+ ((e.Transaction_Headline__c == null || e.Transaction_Headline__c == '')?'':e.Transaction_Headline__c)+'</a>'+  '<h5> '+ '(FT Partners profile Available) '+'</h5>' + '", "'+  '<a target=\'_parent\' href=\'TransactionSearch?AccountId='+e.Target__r.Id +'\'>' + e.Target__r.Name + '</a>' + '","'+ RltdCompany + '","' + e.Target_Sector2__c + '","'+ e.Amount__c   + '"]');  
    

 Regards,

kathir

Kathir DevanKathir Devan

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

 

digamber.prasaddigamber.prasad

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

 

for(Transaction_Relationship__c f : [Select r.Related_Company_Id__c,r.Related_Company_Name__c from Transaction_Relationship__c r  where r.Transaction__r.Id =:strtransId and r.RecordType.Name = 'Buyer/Investor' order by r.Related_Company_Name__c asc])
                    {
                       
                       string s = ((f.Related_Company_Name__c == null || f.Related_Company_Name__c =='')?'':f.Related_Company_Name__c) ;
                       if( s != null && s != '')
                       {
                         s = SpecialCharHandler(s);
                         RltdCompany = RltdCompany + '<a target=\'_parent\' href=\'transactionsearch?AccountId='+f.Related_Company_Id__c +'\'>' + s + '</a>' + ';  ';
                         countRelCompany = countRelCompany +1;
                       }
                      
                    }

Happy to help you!

 

Kathir DevanKathir Devan

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

digamber.prasaddigamber.prasad

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:-

for(Transaction__c e : Database.query(SearchQuery)){

 

Insted of this, it should be modified something like:-

 

for(List<Transaction__c> lstTransaction : Database.query(SearchQuery)){
}

 

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!

 

Kathir DevanKathir Devan

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

digamber.prasaddigamber.prasad

Hi,

 

You will need to put an extra for loop, something like below:-

 

for(List<Transaction__c> lstTransaction : Database.query(SearchQuery))
{
	for(Transaction__c e : lstTransaction){
		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 = '';

 Let me know if you have any question.

 

Happy to help you!

 

 

Kathir DevanKathir Devan

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

 

digamber.prasaddigamber.prasad

Do you have skype ID, where we can discuss it. Screen share will help.

Kathir DevanKathir Devan

Hi Digamber,

 

When i click the list of records some record is display, some record not display  getting CPU limt err..

 

Regards,

kathir

digamber.prasaddigamber.prasad

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.

 

Kathir DevanKathir Devan

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

 

Kathir DevanKathir Devan

Hi Digamber,

 

Have i send  my  whole controller code now.

 

 

Regards,

kathir

 

Kathir DevanKathir Devan

Hi Digamber,

 

Please give me suggestion how to resolve time limit err.

 

Regards,

kathir

Kathir DevanKathir Devan

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..

 //check the condition of advance search
             List<Transaction__c> que = Database.query(SearchQuery);
             listSize = que.size();
             system.debug('_______llllllllll__________'+listSize +offset+queryLimit);
             if(listSize > offset+queryLimit)
             lastOff = true;
             else 
             lastOff = false;
             if(( transactionStatus != null) ||  ( StartDate != null) || (EndDate != null) || (excludeAmount != true) || (amountMin != null) || (amountMax != null))
             {
                    if( transactionStatus != null && transactionStatus != '')
                    {
                     SearchQuery = SearchQuery+ ' and(t.Transaction_status__c in ('+transactionStatus+')) ';
                     QueryString=QueryString +';' +'TS:'+ transactionStatus;
                   
                    }
                   
                    if( StartDate != null && EndDate != null && StartDate != '' && EndDate != '' )
                    {
                    if(url == '/apex/transactionsearch' || url == '/apex/transactionsearch?var=mmf' || url =='/apex/transactionsearch?sub=aggmaf')
                    {
                     SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+'))';
                    }
                       
                      else if(url =='/apex/transactionsearch?sub=Bankmaf')
                    {
                  
                    SearchQuery = SearchQuery + ' and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and t.Target_Sector2__c LIKE :bank';
                    }
                     else if(url =='/apex/transactionsearch?sub=paymaf')
                    {
                  
                    SearchQuery = SearchQuery + ' and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and t.Target_Sector2__c LIKE :pay';
                    } 
                    else if(url =='/apex/transactionsearch?sub=secmaf')
                    {
                    
                    SearchQuery = SearchQuery + ' and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and t.Target_Sector2__c LIKE :sec';
                    }
                    else if(url =='/apex/transactionsearch?sub=insmaf')
                    {
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and t.Target_Sector2__c LIKE :ins';
                    }
                    else if(url =='/apex/transactionsearch?sub=helmaf')
                    {
                   
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and t.Target_Sector2__c LIKE :hel';
                    }
                    else if(url =='/apex/transactionsearch?sub=fbpmaf')
                    {
                   
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and t.Target_Sector2__c LIKE :fsbo';
                    }
                  //textbox search for transaction end here
                  //textbox search for MA start here
                    else  if(url =='/apex/masearch?var=ma' || url =='/apex/masearch?sub=aggma')
                    {
                    
                    SearchQuery = SearchQuery + ' and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+'))  and  t.Type2__c LIKE :maa';
                    }
                    else  if(url =='/apex/masearch?sub=Bankma')
                    {
                    
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :maa and t.Target_Sector2__c LIKE :bank';
                    }
                    else  if(url =='/apex/masearch?sub=payma')
                    {
                    
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :maa and t.Target_Sector2__c LIKE :pay';
                    }
                    
                    else if(url =='/apex/masearch?sub=secma')
                    {
                   
                    SearchQuery = SearchQuery + ' and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :maa and t.Target_Sector2__c LIKE :sec';
                    }
                    else if(url =='/apex/masearch?sub=insma')
                    {
                    
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :maa and t.Target_Sector2__c LIKE :ins';
                    }
                    else if(url =='/apex/masearch?sub=helma')
                    {
                    
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :maa and t.Target_Sector2__c LIKE :hel';
                    }
                    else  if(url =='/apex/masearch?sub=fbpma')
                    {
                    
                    SearchQuery = SearchQuery + ' and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :maa and t.Target_Sector2__c LIKE :fsbo';
                    }
                  //textbox search for MA end here
                  //textbox search for Financing start here
                    else if(url=='/apex/MAFinancing?var=fin' || url=='/apex/MAFinancing?sub=aggfin')
                    {
                    
                    SearchQuery = SearchQuery + ' and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :finn';
                    }
                    else if(url =='/apex/MAFinancing?sub=Bankfin')
                    {
                    
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :finn and t.Target_Sector2__c LIKE :bank';
                    }
                    else if(url =='/apex/MAFinancing?sub=payfin')
                    {
                    
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :finn and t.Target_Sector2__c LIKE :pay';
                    }
                    else if(url =='/apex/MAFinancing?sub=secfin')
                    {
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :finn and t.Target_Sector2__c LIKE :sec';
                    }
                    else if(url =='/apex/MAFinancing?sub=insfin')
                    {
                   
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :finn and t.Target_Sector2__c LIKE :ins';
                    }
                    else if(url =='/apex/MAFinancing?sub=helfin')
                    {
                   
                    SearchQuery = SearchQuery + 'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :finn and t.Target_Sector2__c LIKE :hel';
                    }
                    else if(url =='/apex/MAFinancing?sub=fbpfin')
                    {
                   
                    SearchQuery = SearchQuery +'and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) and  t.Type2__c LIKE :finn and t.Target_Sector2__c LIKE :fsbo';
                    } 
                      QueryString=QueryString +';' +'SD:'+ StartDate.replace('-','') + ';'+ 'ED:'+ EndDate.replace('-','') ;
                      
                    }
                    if( excludeAmount == true)
                    {
                        SearchQuery = SearchQuery + ' and (t.Amount__c > 0) ';
                        QueryString=QueryString +';' +'EX:'+ excludeAmount ;
                    }
                    if((amountMax != 0 &&  amountMin == 0) || (amountMax != 0))
                    {
                        QueryString=QueryString +';' +'AMi:'+ amountMin + ';' + 'AMa:'+ amountMax ;
                        SearchQuery = SearchQuery + ' and ((t.Amount__c >= '+(amountMin==null?0:amountMin)+' ) and (t.Amount__c <= '+(amountMax==null?0:amountMax)+' )) ';
                    }
                    SearchQuery = SearchQuery + ' ORDER BY t.Announce_Date__c Desc LIMIT :queryLimit  OFFSET :offset ';
                    system.debug('_____________'+SearchQuery );
             }
            
           
           for(List<Transaction__c> lstTransaction : Database.query(SearchQuery))
           {    
           for(Transaction__c e : Database.query(SearchQuery))
             {                 
                 DateTime dte = e.Announce_Date__c;
                 string BuyerInvestor='';
                 string str=QueryString.replace('\'','');
                 if(e.Target__r.Name =='null' || e.Target__r.Name ==null )
                 e.Target__r.Name = '';
                
                 e.Transaction_Headline__c = SpecialCharHandler(e.Transaction_Headline__c);
                 if( e.Target__r.Name !='' ||  e.Target__r.Name !=null )
                 e.Target__r.Name  = SpecialCharHandler(e.Target__r.Name );
                   
                 if(e.Profile_Link__c=='null' || e.Profile_Link__c==null )
                 e.Profile_Link__c= '';
                 if( e.Profile_Link__c != '' ||  e.Profile_Link__c != null)
                 e.Profile_Link__c = SpecialCharHandler(e.Profile_Link__c);
                 
                 string RltdCompany = '';
                 string strtransId = e.Id;
                 string strprofile = e.Profile_Link__c;
                 integer countRelCompany = 0;
                 
                  
                 for(Transaction_Relationship__c f : [Select r.Related_Company_Id__c,r.Related_Company_Name__c from Transaction_Relationship__c r  where r.Transaction__r.Id =:strtransId and r.RecordType.Name = 'Buyer/Investor' order by r.Related_Company_Name__c asc])
                    {
                       
                       string s = ((f.Related_Company_Name__c == null || f.Related_Company_Name__c =='')?'':f.Related_Company_Name__c) ;
                       if( s != null && s != '')
                       {
                         s = SpecialCharHandler(s);
                         RltdCompany = RltdCompany + '<a target=\'_parent\' href=\'transactionsearch?AccountId='+f.Related_Company_Id__c +'\'>' + s + '</a>' + ';  ';
                         countRelCompany = countRelCompany +1;
                       }
                      
                    }
              
                  if( countRelCompany != 0)
                  {
                      RltdCompany = RltdCompany.substring(0,RltdCompany.length()-3);
                      
                  }
                 //Insert data into the grid
                 
                    if(e.Amount__c == 0.00 || e.Amount__c == NULL )
                    {
                        string s ='';
                        if(str != null && str != '')  
                        {
                           if(e.Profile_Link__c=='null'|| e.Profile_Link__c==null || e.Profile_Link__c=='')// to check the pdf link is empty 
                            {
                                 grdData.add('["'+ dte.format('MM/dd/yyyy') +  '","' + ((e.Type2__c == null || e.Type2__c == '')?'':e.Type2__c) + '", "'+'<a target=\'_parent\' href=\'TransactionDetail?id='+ e.Id +'&keyword='+str+'\'>' + ((e.Transaction_Headline__c == null || e.Transaction_Headline__c == '')?'':e.Transaction_Headline__c)+'</a>' + '", "'+  '<a target=\'_parent\' href=\'TransactionSearch?AccountId='+e.Target__r.Id +'\'>' + e.Target__r.Name + '</a>' + '","'+ RltdCompany + '", "' + e.Target_Sector2__c + '","'+ s + '","'+  '"]');  
                            }
                            else if(e.Profile_Link__c!='null'|| e.Profile_Link__c!=null || e.Profile_Link__c!='')// to check the pdf link is  not  empty 
                            {
                                 grdData.add('["' + dte.format('MM/dd/yyyy') +  '","' + ((e.Type2__c == null || e.Type2__c == '')?'':e.Type2__c) + '", "'+ '<a target=\'_parent\' href=\'TransactionDetail?id='+ e.Id +'&keyword='+str+'\'>' +((e.Transaction_Headline__c == null || e.Transaction_Headline__c == '')?'':e.Transaction_Headline__c)+'</a>'+  '<h5>'+ '(FT Partners profile Available)'+'</h5> ' + '", "'+  '<a target=\'_parent\' href=\'TransactionSearch?AccountId='+e.Target__r.Id +'\'>' + e.Target__r.Name + '</a>' + '","'+ RltdCompany+ '", "' + e.Target_Sector2__c + '","'+ s + '"]');  
                            }
                       }
                        else{                        
                                if(e.Profile_Link__c=='null'|| e.Profile_Link__c==null || e.Profile_Link__c=='') // to check the pdf link is empty 
                                {
                                    grdData.add('["' + dte.format('MM/dd/yyyy') +  '","' + ((e.Type2__c == null || e.Type2__c == '')?'':e.Type2__c) + '", "'+ '<a target=\'_parent\' href=\'TransactionDetail?id='+ e.Id +'\'>' +((e.Transaction_Headline__c == null || e.Transaction_Headline__c == '')?'':e.Transaction_Headline__c) +'</a>'+ '", "'+  '<a target=\'_parent\' href=\'TransactionSearch?AccountId='+e.Target__r.Id +'\'>' + e.Target__r.Name + '</a>' + '","'+ RltdCompany + '","' + e.Target_Sector2__c + '","' + s + '" ]');  
                                }
                                else  if(e.Profile_Link__c !='null'|| e.Profile_Link__c !=null || e.Profile_Link__c !='')// to check the pdf link is not empty 
                                {
                                    grdData.add('["'+ dte.format('MM/dd/yyyy') +  '","' + ((e.Type2__c == null || e.Type2__c == '')?'':e.Type2__c) + '", "'+'<a target=\'_parent\' href=\'TransactionDetail?id='+ e.Id +'\'>' + ((e.Transaction_Headline__c == null || e.Transaction_Headline__c == '')?'':e.Transaction_Headline__c)+'</a>'+  '<h5> '+'(FT Partners profile Available) '+'</h5> ' + '", "'+  '<a target=\'_parent\' href=\'TransactionSearch?AccountId='+e.Target__r.Id +'\'>' + e.Target__r.Name + '</a>' + '","'+ RltdCompany + '","' + e.Target_Sector2__c + '","' + s + '"]');  
                                }
                            }
                    }  
                  
                    else
                    { 
                         if(str != null && str != '')  
                         {     
                          if(e.Profile_Link__c=='null'|| e.Profile_Link__c==null || e.Profile_Link__c=='')// to check the pdf link is empty 
                            {               
                                grdData.add('["'+ dte.format('MM/dd/yyyy') + '","' +  ((e.Type2__c == null || e.Type2__c == '')?'':e.Type2__c) + '", "'+'<a target=\'_parent\' href=\'TransactionDetail?id='+ e.Id +'&keyword='+str+'\'>' + ((e.Transaction_Headline__c == null || e.Transaction_Headline__c == '')?'':e.Transaction_Headline__c)+'</a>' + '", "'+  '<a target=\'_parent\' href=\'TransactionSearch?AccountId='+e.Target__r.Id +'\'>' + e.Target__r.Name + '</a>' + '","'+ RltdCompany + '","' + e.Target_Sector2__c + '","'+ e.Amount__c   + '","'+  ''+ '"]');  
                            }
                            else if(e.Profile_Link__c !='null'|| e.Profile_Link__c !=null || e.Profile_Link__c !='')// to check the pdf link is empty 
                            {               
                                grdData.add('["'+ dte.format('MM/dd/yyyy') + '","' + ((e.Type2__c == null || e.Type2__c == '')?'':e.Type2__c) + '", "'+ '<a target=\'_parent\' href=\'TransactionDetail?id='+ e.Id +'&keyword='+str+'\'>'+ ((e.Transaction_Headline__c == null || e.Transaction_Headline__c == '')?'':e.Transaction_Headline__c)+'</a>'+  '<h5> '+ '(FT Partners profile Available) '+'</h5>' + '", "'+  '<a target=\'_parent\' href=\'TransactionSearch?AccountId='+e.Target__r.Id +'\'>' + e.Target__r.Name + '</a>' + '","'+ RltdCompany + '","' + e.Target_Sector2__c + '","'+ e.Amount__c   + '"]');  
                            }                   
                       }
                        else
                        {                          
                           if(e.Profile_Link__c=='null'|| e.Profile_Link__c==null || e.Profile_Link__c=='')// to check the pdf link is empty 
                            {               
                                grdData.add('["' + dte.format('MM/dd/yyyy') +  '","' + ((e.Type2__c == null || e.Type2__c == '')?'':e.Type2__c) + '", "'+'<a target=\'_parent\' href=\'TransactionDetail?id='+ e.Id +'\'>' + ((e.Transaction_Headline__c == null || e.Transaction_Headline__c == '')?'':e.Transaction_Headline__c) + '</a>'+ '", "'+  '<a target=\'_parent\' href=\'TransactionSearch?AccountId='+e.Target__r.Id +'\'>' + e.Target__r.Name + '</a>' + '","'+ RltdCompany + '","' + e.Target_Sector2__c + '","'+ e.Amount__c + '"]');  
                            }
                            else  if(e.Profile_Link__c!='null'|| e.Profile_Link__c!=null || e.Profile_Link__c!='')// to check the pdf link is empty 
                            { 
                                grdData.add('["'+ dte.format('MM/dd/yyyy')  +  '","' + ((e.Type2__c == null || e.Type2__c == '')?'':e.Type2__c) + '", "'+'<a target=\'_parent\' href=\'TransactionDetail?id='+ e.Id +'\'>' + ((e.Transaction_Headline__c == null || e.Transaction_Headline__c == '')?'':e.Transaction_Headline__c)+ '</a>'+  '<h5> '+'(FT Partners profile Available) '+'</h5>' + '", "'+  '<a target=\'_parent\' href=\'TransactionSearch?AccountId='+e.Target__r.Id +'\'>' + e.Target__r.Name + '</a>' + '","'+ RltdCompany + '","' + e.Target_Sector2__c + '","'+ e.Amount__c + '"]');   
                            } 
                          }
                    }
                     
               }
            
              if(grdData.size() == 0)
               {
                   grdData.add('["","","","No Transactions Found","","","",""]'); 
               }
             
            }
          }
        catch(Exception ex)
        {
          grdData = new String[]{}; 
          grdData.add('["","","","No Transactions Found","","","",""]');   
          ApexPages.addMessages(ex);
          system.debug('Bind grid catch$______________$'+ ex);
       }
   }

 

Regards,

kathir

 

KevinBrKevinBr
Wow. I'm not a big fan of huge if/else blocks.
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:
// setup your strings for the urls here
string prmAnnounceDate = ' and  ((t.Announce_Date__c >= '+StartDate+') and (t.Announce_Date__c <= '+EndDate+')) \n';
string prmAnnounceDateBank = prmAnnounceDate  + ' and t.Target_Sector2__c LIKE :bank \n';
string prmAnnounceDatePay = prmAnnounceDate  + ' and t.Target_Sector2__c LIKE :pay\n';
..
map<string,string> mapURLParms = new map<string,string>();
// add your url2parms here
mapURLParms.put('/apex/transactionsearch',prmAnnounceDate );
mapURLParms.put('/apex/transactionsearch?var=mmf',prmAnnounceDate );
mapURLParms.put('/apex/transactionsearch?sub=aggmaf',prmAnnounceDate );
mapURLParms.put('/apex/transactionsearch?sub=Bankmaf',prmAnnounceDateBank);
mapURLParms.put('/apex/transactionsearch?sub=paymaf'',prmAnnounceDatePay);

...
if (mapURLParms.containsKey(url)) {
  string urlParm = mapURLParms.get(url);
  SearchQuery = SearchQuery + urlParm;
}

 
KevinBrKevinBr
Best practice would have you put each soql for loop into maps..
Then iterate using the maps

ie,
map<id,transaction__c> mapTransactions = new map<id,transaction__c>();
for(List<Transaction__c> t1 : Database.query(SearchQuery)) {
  mapTransactions.put(t1.Id,t1)
}

map<id,Transaction_Relationship__c > mapTrxRelationship = new map<id,Transaction_Relationship__c >();
for (Transaction_Relationship__c fr : [Selectr.Related_Company_Id__c,r.Related_Company_Name__c from Transaction_Relationship__c r  wherer.Transaction__r.Id =:strtransId and r.RecordType.Name = 'Buyer/Investor' order byr.Related_Company_Name__c asc]) {
  mapTrxRelationship.put(fr.Transaction__r.Id,fr);
}

for (id trxId : mapTransactions.keySet()) {
  Transaction__c trx = mapTransactions.get(trxId);
  Transaction_Relationship__c fr = mapTrxRelationship.get(trxId);
}
This method prevents all of the transaction relationships records from being queried, for every transaction resulting from the search query.

 
bouscalbouscal
Not sure if this is resolved or not and see that's it's almost 5 years old but I noticed a query inside a loop which is a no-no.
for(Transaction_Relationship__c f : [Select r.Related_Company_Id__c,r.Related_Company_Name__c from Transaction_Relationship__c r  where r.Transaction__r.Id =:strtransId and r.RecordType.Name = 'Buyer/Investor' order by r.Related_Company_Name__c asc])
Put the results of the query in a list or a map since you only need the two fields then loop through that.

 
Kristen Aldrich 14Kristen Aldrich 14
We are getting this error with increased frequency since the Summer '19 release. I found some known issues that have since been deemed fixed and some similar comments on forums but can't quite figure out why the error is being thrown. This is the exact message we got most recently when running an Address verification batch job using Experian EDQ: "npsp.TDTM_Account: System.LimitException: Apex CPU time limit exceeded"

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? 
Arun Sarvan SamArun Sarvan Sam
Hi Friends,

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.
Srikar Goud BeemagouniSrikar Goud Beemagouni
Hi Friends,
 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.
bouscalbouscal

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.

aparna d 1aparna d 1
Hi,
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;   
    }
 
bouscalbouscal
how many records are your queries pulling?