Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- global class SL_Batch_Match_LeadsWithAccounts implements Database.Batchable<sObject>{
- public static set<Id> OppAcctIds;
- //Map<ID, Account> acctMap = new Map<ID, Account>();
- List<Account> acctsList;
- public static List <Opportunity> oppListToUpdateFlag;
- String leadRecordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Sun Homes - LPS').getRecordTypeId();
- global Database.QueryLocator start(Database.BatchableContext BC){
- String query = 'Select AccountId, Flag__c from Opportunity where Flag__c = 'New'';// And RecordTypeId ='+'''+leadRecordTypeId+''';
- return Database.getQueryLocator(query);
- }
- global void execute(Database.BatchableContext BC, List<sObject>scope){
- //OppAcctIds = new set<Id>();
- //acctsList = new List <Account>();
- oppListToUpdateFlag = new List<Opportunity>();
- List <String> acctAndLeadIdList = new List <String>();
- for(Sobject s : scope){
- Opportunity opp = (Opportunity)s;
- //OppAcctIds.add(opp.AccountId);
- oppListToUpdateFlag.add(opp);
- Map<ID, Account> acctMap = new Map<ID, Account>([Select Name, Phone, PersonEmail From Account Where ID =: opp.AccountId]);
- for(ID acctID :acctMap.keySet()){
- if(opp.AccountID == acctID){
- Map<ID, Lead> leadMap = new Map<ID, Lead>([Select IsConverted from Lead Where Name =:acctMap.get(acctID).Name AND (Phone =:acctMap.get(acctID).Phone OR Email=:acctMap.get(acctID).PersonEmail)]);
- for(ID leadID :leadMap.keySet()){
- if(leadID != null && !leadMap.get(leadID).IsConverted){
- String temp = acctID + '-' +leadID;
- acctAndLeadIdList.add(temp);
- //mergLeadWithExistingAccount(acctID,leadID);
- }
- }
- }
- }
- }
- if(!acctAndLeadIdList.isEmpty()){
- mergLeadWithExistingAccount(acctAndLeadIdList);
- }
- if(!oppListToUpdateFlag.isEmpty()){
- updateOpportuniyFlags(oppListToUpdateFlag);
- }
- }
- public void mergLeadWithExistingAccount(List <String> acctAndLeadIdListRecieved){
- list<Database.LeadConvert> leadConverts = new list<Database.LeadConvert>();
- for(String acctAndLeadId: acctAndLeadIdListRecieved){
- String tempAcctId = acctAndLeadId.substringBefore('-');
- String tempLeadId = acctAndLeadId.substringAfter('-');
- Database.LeadConvert lc = new database.LeadConvert();
- lc.setLeadId(tempLeadId);
- lc.setAccountId(tempAcctId);
- //LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
- //lc.setConvertedStatus(convertStatus.MasterLabel);
- lc.setConvertedStatus('Qualified');
- //lc.convertedStatus = 'Qualified';
- //Database.ConvertLead(lc,true);
- lc.setDoNotCreateOpportunity(true);
- leadConverts.add(lc);
- }
- List <Database.LeadConvertResult> results = Database.convertLead(leadConverts);
- for(Integer i=0; i <= results.size(); i++){
- System.assert(results[i].isSuccess());
- }
- System.debug(results);
- }
- public void updateOpportuniyFlags(List<Opportunity> oppList){
- List<Opportunity> tempOppList = new List<Opportunity>();
- for (Opportunity opp: oppList){
- opp.Flag__c = 'Processed';
- tempOppList.add(opp);
- }
- update tempOppList;
- }
- /*public void matchLeadWithAccount(Lead leadRecievedForMatching){
- for (Account acc: [Select id from Account where Name=:leadRecievedForMatching.Name AND (PersonEmail =:leadRecievedForMatching.Email OR Phone=:leadRecievedForMatching.Phone)]){
- if (acc.id != Null){
- //mergLeadWithExistingAccount(acc.id,leadRecievedForMatching);
- }
- }
- }*/
- global void finish(Database.BatchableContext BC){
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement