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
SFDC ROCKSFDC ROCK 

Getting System.LimitException: Apex CPU time limit exceeded

Hi All,
Getting below error when inserting 100 records
System.LimitException: Apex CPU time limit exceeded

String ob= Schema.getGlobalDescribe().get('Opportunity').getDescribe().getRecordTypeInfosById().get(opp.RecordTypeId).getName();
if(ob=='oops'){
//updating one field
}

Any alternate way to get recordtype name ? or other way to write above code ?
 
VinayVinay (Salesforce Developers) 
Hi,

Try to check debug logs which gives more information on CPU time.

Review below link and follow steps mentioned.

https://help.salesforce.com/articleView?id=000339361&language=en_US&type=1&mode=1

Thanks,
Vinay Kumar
Sumit SFSumit SF
Hi Rock,

1. SELECT Id, Name, DeveloperName, SobjectType FROM RecordType WHERE SobjectType='Contact'

2  SELECT Id, RecordtypeId, RecordType.name FROM Account where Id = '<your id>';
Andrew GAndrew G
At a guess, are you doing the Schema.getGlobalDescribe inside a For Loop?

If you are only testing the records for a particular recordType, (which it seems as you are doing it via name), consider if the Schema call can be done outside the for loop. and you test inside the loop using the Id, not the name.  You can still make it portable because you set the Id value outside the loop using the Schema methods
 
Id rtId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('oops').getRecordTypeId();
Then in your loop test against rtId
for(Opportunity opp : oppList) {
  if(opp.RecordTypeId==rtId){
    //updating one field
  }
}


Regards
Andrew