I am running into cpu limit with this am I am missing somthing? And also how can I adjust the code to avoid harcoding the ID like i did.
Trigger AutoConverter on Lead (after insert,after update) {
LeadStatus convertStatus = [
select MasterLabel
from LeadStatus
where IsConverted = true
limit 1
];
List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
for (Lead lead: Trigger.new) {
if (!lead.isConverted ) {
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(lead.Id);
lc.setDoNotCreateOpportunity(TRUE);
lc.setConvertedStatus(convertStatus.MasterLabel);
String leadOwner = lead.OwnerId;
if(leadOwner.startsWith('00G')){
Lc.OwnerId = '00535000001ZOZN'; }
leadConverts.add(lc);
}
}
if (!leadConverts.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
}
}
https://salesforce.stackexchange.com/questions/19399/any-reason-to-skip-dml-on-empty-lists
– Alexander Aeons Torn Feb 12 '20 at 16:56