I have a method in an Apex class which is used in a visualforce page. I have been asked to move part of the method's code in a batch, since it seems that it is exceeding limits and causing errors.
Here is the code:
@RemoteAction
public static Map<String, String> create(List <Map<String, String>> contactRecords, String campaignId, String caseType, String caseSubType) {
Map<String, String> result = new Map<String, String>();
List<Case> cases = new List<Case>();
List<CampaignMember> cmList = new List<CampaignMember>();
for (Map<String, String> contactRecord : contactRecords) {
String contactId = contactRecord.get('Id ');
if(campaignId != null && contactId != null){
CampaignMember currentCamp = new CampaignMember(ContactId=Id.valueOf(contactId), CampaignId=campaignId);
Case currentCase = new Case(Status='In Progress', unig__Channel__c='TELEMARKETING', Origin='Web',unig__Campaign__c=campaignId, ContactId=Id.valueOf(contactId), UNITA_Case_Type__c =caseType, UNITA_Case_Subtype__c = caseSubType, UNITA_From_Einstein__c=true);
cases.add(currentCase);
cmList.add(currentCamp);
//System.debug('### currentCamp ' + currentCamp);
}
}
try{
//System.debug('### CAMPAING MEMBER LIST ' + cmList.size());
//System.debug('### CASES LIST ' + cases.size());
insert cmList;
insert cases;
}catch(Exception e){
System.debug('### ERROR ' + e);
}
for (Case currentCase2 : cases) {
String currentCaseContactId = currentCase2.contactId;
String currentCaseId = currentCase2.Id;
result.put(currentCaseContactId, currentCaseId);
}
return result;
}
My question is: how do I implement the start method in the batch? I don't need a query, since the contacts are passed to the function as a parameter? How can the batch work with no query in the start method.
I have no experience at all with batches, so I might be missing something. Any help is much appreciated!