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
Gus protoGus proto 

Convert lead trigger cpu limits and bulk api

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);
     }
}

 
Best Answer chosen by Gus proto
Mark Moser 10Mark Moser 10
Add a query at the top of the trigger to select the userid of the owner you wish to assign.  It looks like your looking for one particular user to be the owner.  Example:

USER u = [Select id from user where name = 'My New Owner'];

then in the look use u.id instead  of '00535000001ZOZN'

lc.ownerId = u.id;


 

All Answers

Mark Moser 10Mark Moser 10
Add a query at the top of the trigger to select the userid of the owner you wish to assign.  It looks like your looking for one particular user to be the owner.  Example:

USER u = [Select id from user where name = 'My New Owner'];

then in the look use u.id instead  of '00535000001ZOZN'

lc.ownerId = u.id;


 
This was selected as the best answer
Mark Moser 10Mark Moser 10
I would also reduce the batch size of the process that is loading leads.   Each batch is measured on cpu limit, therefore if you reduce the batch size you will reduce the cpu usage