Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.86 KB | None | 0 0
  1. /***************************************************************************************************************
  2. * Avanxo
  3. * @author Denise Volpe, Franco Molfino
  4. * Proyecto: PyMe
  5. * Descripción: Conversion de Lead en Cuenta y Contacto
  6. * Cambios (Version)
  7. * -------------------------------------------
  8. * No. Fecha Autor Descripción
  9. * ----- ---------- --------------------- ---------------
  10. * @version 1.0 16/05/2019 Denise Volpe, Franco Molfino Clase que se encarga de convertir una Lead en un contacto y una cuenta.
  11. ****************************************************************************************************************/
  12. public class AVX_LeadHelper_cls {
  13.  
  14.  
  15. public static void cargarDatos(list<Lead> lstLead){
  16. List<string> lstCuitLeads = new List<String>();
  17. List<string> lstDNILeads = new List<string>();
  18. list<Lead> lstLeadIns = new list<Lead>();
  19. List<Account> lstCuentasAAct = new List<Account>();
  20. List<Account> lstCuentasInsertar =new List<Account>();
  21. List<Lead> lstLeadAct = new List<Lead>();
  22. List<string> lstCuilPos = new List<string>();
  23. List<Contact> lstContact = new List<Contact>();
  24.  
  25.  
  26. for(lead l:lstLead){
  27. lstCuitLeads.add(l.AVX_CUIT__c);
  28. lstDNILeads.add(l.AVX_DNI__c);
  29.  
  30. }
  31. lstCuentasAAct=[SELECT id, CUIT_CUIL__c,recordTypeId,Phone, calle__c,Correo_electronico__c,Celular__c,Actividad__c,website from Account WHERE CUIT_CUIL__c in:lstCuitLeads];
  32. lstContact = [SELECT id,AVX_DNI__c, Phone, email,Calle__c,Celular__c FROM Contact WHERE AVX_DNI__c in:lstDNILeads ];
  33.  
  34. for(lead l:lstLead){
  35. boolean ok=false;
  36. for(Account cuil:lstCuentasAAct){
  37. if(l.AVX_CUIT__c==cuil.CUIT_CUIL__c){
  38. ok=true;
  39. lstLeadAct.add(l);
  40. }
  41. }
  42. if (ok==false){
  43. lstLeadIns.add(l);
  44. }
  45.  
  46. }
  47.  
  48. AVX_LeadHelper_cls.insertarCuentaContacto(lstLeadIns);
  49. AVX_LeadHelper_cls.updateCuentaContacto(lstCuentasAAct,lstLeadAct,lstContact);
  50.  
  51.  
  52.  
  53.  
  54. }
  55.  
  56. public static void insertarCuentaContacto(List<Lead> lstLeads){
  57. LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
  58. RecordType rt =[SELECT id , name FROM RecordType WHERE name = 'PyME'];
  59.  
  60. List<string> cuil =new List<string>();
  61. List<Account> listaDeCuentasRT =new List<Account>();
  62. List<Account> listaDeCuentas = new List<Account>();
  63. List<Contact> listaDeContactos = new List<Contact>();
  64. List<Database.LeadConvert> leadConvertir =new List<Database.LeadConvert>();
  65. // Map<id,lead> mapError= new Map<id,Lead>(lstLeads);
  66.  
  67. for(Lead l:lstLeads){
  68. Database.LeadConvert lc = new Database.LeadConvert();
  69. lc.setLeadId(l.id);
  70. lc.setDoNotCreateOpportunity(true);
  71. lc.setConvertedStatus(convertStatus.MasterLabel);
  72. leadConvertir.add(lc);
  73. cuil.add(l.AVX_CUIT__c);
  74. listaDeCuentas.add(new account(CUIT_CUIL__C = l.AVX_CUIT__c ,Name = l.Company,Website=l.Website,Correo_electronico__c =l.AVX_Correo_electr_nico__c ));
  75.  
  76. }
  77.  
  78. Database.convertLead(leadConvertir);
  79.  
  80.  
  81.  
  82. listaDeCuentasRT=[SELECT id, recordtypeId,CUIT_CUIL__c from Account WHERE CUIT_CUIL__c =:cuil ];
  83.  
  84. //El recorrido de las leads para actualizar los campos de cuenta y contacto.
  85.  
  86. for(Account acc :listaDeCuentasRT){
  87. acc.recordtypeId = rt.id;
  88. system.debug('El recordtypeId es' +acc.RecordTypeId);
  89. system.debug('El tamaño de la lista de cuentas:' +listaDeCuentasRT.size());
  90. for(Account cuenta:listaDeCuentas){
  91. if(cuenta.CUIT_CUIL__c == acc.CUIT_CUIL__c){
  92. if((acc.Phone == null || acc.Phone =='') && (cuenta.Phone != null || cuenta.Phone != ''))
  93. acc.Phone=cuenta.Phone;
  94. if((acc.Correo_electronico__c == null || acc.Correo_electronico__c == '' ) && (cuenta.Correo_electronico__c != null || cuenta.Correo_electronico__c != ''))
  95. acc.Correo_electronico__c=cuenta.Correo_electronico__c;
  96. if((acc.Website == null || acc.website == '') && (cuenta.Website != null || cuenta.Website == '' ))
  97. acc.Website=cuenta.website;
  98.  
  99. }
  100. }
  101. }
  102. update listaDeCuentasRT;
  103.  
  104. }
  105. public static void updateCuentaContacto(List<Account> lstCuentasAAct, List<Lead> lstLeads, List<Contact> lstContact){
  106. system.debug('Entra update cuenta contacto');
  107. system.debug('el tamaño de la lista de cuentasACT' +lstCuentasAAct.size());
  108. system.debug('el tamaño de la lista de leads' +lstLeads.size());
  109.  
  110.  
  111. LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
  112. RecordType rt =[SELECT id , name FROM RecordType WHERE name = 'PyME'];
  113.  
  114. List<string> cuil =new List<string>();
  115. List<Account> listaDeCuentasRT =new List<Account>();
  116. List<Database.LeadConvert> leadConvertir =new List<Database.LeadConvert>();
  117. for(Lead l:lstLeads){
  118.  
  119.  
  120. for(Account lacc :lstCuentasAAct){
  121. system.debug('el tamaño de la lista de centasACT' +lstCuentasAAct.size());
  122.  
  123. if(lacc.CUIT_CUIL__c == l.AVX_CUIT__c){
  124. Database.LeadConvert lc = new Database.LeadConvert();
  125. lc.setLeadId(l.id);
  126. lc.setDoNotCreateOpportunity(true);
  127. lc.setConvertedStatus(convertStatus.MasterLabel);
  128. lc.setAccountId(lacc.id);
  129. for(Contact cont:lstContact){
  130. if(l.AVX_DNI__c == cont.AVX_DNI__c){
  131. lc.setContactId(cont.Id);
  132. if((cont.Phone == null || cont.Phone=='') && (l.Phone != null || l.Phone!=''))
  133. cont.Phone=l.Phone;
  134.  
  135. if((cont.Email == null || cont.Email=='' ) &&( l.Email != null || l.Email != ''))
  136. cont.email=l.Email;
  137.  
  138. if((cont.Calle__c == null || cont.Calle__c=='') && (l.street != null || l.Street != '') && (l.State != null || l.State != '') && (l.PostalCode != null || l.PostalCode != '') && (l.country != null || l.Country != '') )
  139. cont.Calle__c=String.format('{0}\n{1}\n{2}\n{3}',new String[]{l.street,l.state,l.PostalCode,l.country});
  140.  
  141. if(cont.Celular__c == null )
  142. cont.Celular__c =Decimal.valueOf(l.MobilePhone);
  143. }
  144.  
  145. }
  146. leadConvertir.add(lc);
  147. cuil.add(l.AVX_CUIT__c);
  148.  
  149. if((lacc.Phone == null || lacc.Phone != '' ) && (l.Phone == null || l.Phone != ''))
  150. lacc.Phone=l.Phone;
  151.  
  152. if((lacc.Correo_electronico__c == null || lacc.Correo_electronico__c !='') && (l.AVX_Correo_electr_nico__c == null || l.AVX_Correo_electr_nico__c != ''))
  153. lacc.Correo_electronico__c=l.AVX_Correo_electr_nico__c ;
  154.  
  155. if((lacc.Calle__c == null || lacc.Calle__c == '') && (l.street != null || l.Street != '') && (l.State != null || l.State != '') && (l.PostalCode != null || l.PostalCode != '') && (l.country != null || l.Country != ''))
  156. lacc.Calle__c=String.format('{0}\n{1}\n{2}\n{3}',new String[]{l.street,l.state,l.PostalCode,l.country});
  157. if((lacc.Celular__c == null || lacc.Celular__c =='' ) && (l.MobilePhone != null || l.MobilePhone != '') )
  158. lacc.Celular__c=l.MobilePhone;
  159.  
  160. //if(lacc.Actividad__c == null)
  161. // lacc.Actividad__c=l.AVX_Actividad__c;
  162. if((lacc.Website == null || lacc.Website == '') && (l.Website !=null || l.Website == ''))
  163. lacc.Website=l.Website;
  164. }
  165.  
  166. }
  167. }
  168. Database.convertLead(leadConvertir);
  169.  
  170.  
  171.  
  172. upsert lstCuentasAAct;
  173. upsert lstContact;
  174.  
  175. }
  176.  
  177.  
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement