Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. global class SL_Batch_Match_LeadsWithAccounts implements Database.Batchable<sObject>{
  2. public static set<Id> OppAcctIds;
  3. //Map<ID, Account> acctMap = new Map<ID, Account>();
  4. List<Account> acctsList;
  5. public static List <Opportunity> oppListToUpdateFlag;
  6. String leadRecordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Sun Homes - LPS').getRecordTypeId();
  7. global Database.QueryLocator start(Database.BatchableContext BC){
  8. String query = 'Select AccountId, Flag__c from Opportunity where Flag__c = 'New'';// And RecordTypeId ='+'''+leadRecordTypeId+''';
  9. return Database.getQueryLocator(query);
  10. }
  11. global void execute(Database.BatchableContext BC, List<sObject>scope){
  12. //OppAcctIds = new set<Id>();
  13. //acctsList = new List <Account>();
  14. oppListToUpdateFlag = new List<Opportunity>();
  15. List <String> acctAndLeadIdList = new List <String>();
  16. for(Sobject s : scope){
  17. Opportunity opp = (Opportunity)s;
  18.  
  19. //OppAcctIds.add(opp.AccountId);
  20. oppListToUpdateFlag.add(opp);
  21. Map<ID, Account> acctMap = new Map<ID, Account>([Select Name, Phone, PersonEmail From Account Where ID =: opp.AccountId]);
  22. for(ID acctID :acctMap.keySet()){
  23. if(opp.AccountID == acctID){
  24.  
  25. 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)]);
  26. for(ID leadID :leadMap.keySet()){
  27. if(leadID != null && !leadMap.get(leadID).IsConverted){
  28. String temp = acctID + '-' +leadID;
  29. acctAndLeadIdList.add(temp);
  30. //mergLeadWithExistingAccount(acctID,leadID);
  31. }
  32. }
  33. }
  34. }
  35. }
  36.  
  37. if(!acctAndLeadIdList.isEmpty()){
  38. mergLeadWithExistingAccount(acctAndLeadIdList);
  39. }
  40. if(!oppListToUpdateFlag.isEmpty()){
  41. updateOpportuniyFlags(oppListToUpdateFlag);
  42. }
  43.  
  44. }
  45.  
  46. public void mergLeadWithExistingAccount(List <String> acctAndLeadIdListRecieved){
  47. list<Database.LeadConvert> leadConverts = new list<Database.LeadConvert>();
  48. for(String acctAndLeadId: acctAndLeadIdListRecieved){
  49. String tempAcctId = acctAndLeadId.substringBefore('-');
  50. String tempLeadId = acctAndLeadId.substringAfter('-');
  51. Database.LeadConvert lc = new database.LeadConvert();
  52. lc.setLeadId(tempLeadId);
  53. lc.setAccountId(tempAcctId);
  54. //LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
  55. //lc.setConvertedStatus(convertStatus.MasterLabel);
  56. lc.setConvertedStatus('Qualified');
  57. //lc.convertedStatus = 'Qualified';
  58. //Database.ConvertLead(lc,true);
  59. lc.setDoNotCreateOpportunity(true);
  60. leadConverts.add(lc);
  61. }
  62. List <Database.LeadConvertResult> results = Database.convertLead(leadConverts);
  63. for(Integer i=0; i <= results.size(); i++){
  64. System.assert(results[i].isSuccess());
  65. }
  66. System.debug(results);
  67. }
  68. public void updateOpportuniyFlags(List<Opportunity> oppList){
  69. List<Opportunity> tempOppList = new List<Opportunity>();
  70.  
  71. for (Opportunity opp: oppList){
  72. opp.Flag__c = 'Processed';
  73. tempOppList.add(opp);
  74. }
  75. update tempOppList;
  76. }
  77. /*public void matchLeadWithAccount(Lead leadRecievedForMatching){
  78. for (Account acc: [Select id from Account where Name=:leadRecievedForMatching.Name AND (PersonEmail =:leadRecievedForMatching.Email OR Phone=:leadRecievedForMatching.Phone)]){
  79. if (acc.id != Null){
  80. //mergLeadWithExistingAccount(acc.id,leadRecievedForMatching);
  81. }
  82. }
  83. }*/
  84. global void finish(Database.BatchableContext BC){
  85.  
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement