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
vali Palagiri 1vali Palagiri 1 

CPU Time Exception Error

Hi Folks,


Apex CPU time limit exceeded
Error is in expression '{!importCSVFile}' in component <apex:commandButton> in page fssrscsvuploaded: Class.FSSRSCSVUploaded.importCSVFile: line 73, column 1
An unexpected error has occurred. Your development organization has been notified.

When i upload csv file into object am getting this error.
Find below code:
public with sharing class FSSRSCSVUploaded {
    public Blob csvFileBody {get;set;}
    public string csvAsString {get;set;}
    public string[] csvFileLines {get;set;}
    List<Analyst_Schedule__c> analystlist ;
    List<String> storenames = new List<String>();//Get the all storeNames
    
    public FSSRSCSVUploaded()
    {
        csvFileLines = new String[]{};
            analystlist = new List<Analyst_Schedule__c>();
    }
    public void importCSVFile(){
        
        try{
            if(csvFileBody == null)
            {
                ApexPages.Message Message1 = new ApexPages.Message(ApexPages.severity.INFO,'Please attach AnalystSchedule List');
                ApexPages.addMessage(Message1);
            }
            else
            {
                system.debug('---csvFileBody---'+csvFileBody);
                csvAsString = csvFileBody.toString();
                csvFileLines = csvAsString.split('\n');
                
                for(Integer i=1; i < csvFileLines.size(); i++ )
                {
                    Analyst_Schedule__c annObj = new Analyst_Schedule__c();
                    String[] csvRecordData = csvFileLines[i].split(',');
                    
                    annObj.Name=csvRecordData[0];
                    annObj.Chain__c=csvRecordData[1];
                    //annObj.Location__c=csvRecordData[2];
                    storenames.add(csvRecordData[2]);
                    annObj.Energy_Base__c=csvRecordData[3];
                    annObj.Controller__c=csvRecordData[4];
                    annObj.Schedule_Confirmed__c=csvRecordData[5];
                    analystlist.add(annObj);
                }
                if(analystlist.size() > 0)
                    insert analystlist;
                
                ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.CONFIRM,'insertion was completed');
                ApexPages.addMessage(msg);
            }
        }
        catch (Exception e)
        {
            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.Severity.ERROR,'An error has occured');
            ApexPages.addMessage(errorMessage);
        }
        
        //Query for the AnalystSchedule records that have matching store names
        Map<String, Id> mapIds = new Map<String, Id>();
        for(Store__c sto:[SELECT ID,Name From Store__c where Name IN: storenames])
        {
            
            mapIds.put(sto.Name, sto.Id);
        }
        //Set<Id> setIds=new Set<Id>();
        //set the Store Id for Each storeNames
        for(Integer i=0; i < analystlist.size();)
        {
            Analyst_Schedule__c ana = analystlist[i];
           Id id = mapIds.get(storenames[i]);
            if(id != null)
            {    
                ana.Location__c = id;
            }
            else
            {
                ApexPages.Message msg1 = new ApexPages.Message(ApexPages.Severity.ERROR,'No match for the StoreName');
                ApexPages.addMessage(msg1);
            }
        }
    }
}
Please give anysuggesitions for this solution.
Thanks in Advance.

Thanks,
Khadhar
bhanu_prakashbhanu_prakash
Hi Khadhar,

Mark as best answer, If it resloves !!​​
Please modify your code as per below link code standards
https://help.salesforce.com/articleView?id=000232681&language=en_US&type=1

Mark as resloved if it helps :) :)
Thanks, 
Bhanu Prakash
visit ForceLearn.com
Raj VakatiRaj Vakati
The issue is becasue of the CSV file whihc you are trying to parse .. when you are loading the file with large data you are getting this issue ...

To fix it make sure you are not parsing any empty line . so that it will reduce the CPU time
vali Palagiri 1vali Palagiri 1
Hi Raj,

Present testing purpose i uplode one record on excel sheet, So am getting on these perticular area like 
or(Integer i=0; i < analystlist.size();)
        {
            Analyst_Schedule__c ana = analystlist[i];
           Id id = mapIds.get(storenames[i]);
            if(id != null)
            {    
                ana.Location__c = id;
            }
If u have any idea about this please respond me. It is look up custom obj.

Thanks in Advance.
Thanks,
Khadhar