Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************************************************************************************************
- * Avanxo
- * @author Denise Volpe, Franco Molfino
- * Proyecto: PyMe
- * Descripción: Conversion de Lead en Cuenta y Contacto
- * Cambios (Version)
- * -------------------------------------------
- * No. Fecha Autor Descripción
- * ----- ---------- --------------------- ---------------
- * @version 1.0 16/05/2019 Denise Volpe, Franco Molfino Clase que se encarga de convertir una Lead en un contacto y una cuenta.
- ****************************************************************************************************************/
- public class AVX_LeadHelper_cls {
- public static void cargarDatos(list<Lead> lstLead){
- List<string> lstCuitLeads = new List<String>();
- List<string> lstDNILeads = new List<string>();
- list<Lead> lstLeadIns = new list<Lead>();
- List<Account> lstCuentasAAct = new List<Account>();
- List<Account> lstCuentasInsertar =new List<Account>();
- List<Lead> lstLeadAct = new List<Lead>();
- List<string> lstCuilPos = new List<string>();
- List<Contact> lstContact = new List<Contact>();
- for(lead l:lstLead){
- lstCuitLeads.add(l.AVX_CUIT__c);
- lstDNILeads.add(l.AVX_DNI__c);
- }
- 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];
- lstContact = [SELECT id,AVX_DNI__c, Phone, email,Calle__c,Celular__c FROM Contact WHERE AVX_DNI__c in:lstDNILeads ];
- for(lead l:lstLead){
- boolean ok=false;
- for(Account cuil:lstCuentasAAct){
- if(l.AVX_CUIT__c==cuil.CUIT_CUIL__c){
- ok=true;
- lstLeadAct.add(l);
- }
- }
- if (ok==false){
- lstLeadIns.add(l);
- }
- }
- AVX_LeadHelper_cls.insertarCuentaContacto(lstLeadIns);
- AVX_LeadHelper_cls.updateCuentaContacto(lstCuentasAAct,lstLeadAct,lstContact);
- }
- public static void insertarCuentaContacto(List<Lead> lstLeads){
- LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
- RecordType rt =[SELECT id , name FROM RecordType WHERE name = 'PyME'];
- List<string> cuil =new List<string>();
- List<Account> listaDeCuentasRT =new List<Account>();
- List<Account> listaDeCuentas = new List<Account>();
- List<Contact> listaDeContactos = new List<Contact>();
- List<Database.LeadConvert> leadConvertir =new List<Database.LeadConvert>();
- // Map<id,lead> mapError= new Map<id,Lead>(lstLeads);
- for(Lead l:lstLeads){
- Database.LeadConvert lc = new Database.LeadConvert();
- lc.setLeadId(l.id);
- lc.setDoNotCreateOpportunity(true);
- lc.setConvertedStatus(convertStatus.MasterLabel);
- leadConvertir.add(lc);
- cuil.add(l.AVX_CUIT__c);
- 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 ));
- }
- Database.convertLead(leadConvertir);
- listaDeCuentasRT=[SELECT id, recordtypeId,CUIT_CUIL__c from Account WHERE CUIT_CUIL__c =:cuil ];
- //El recorrido de las leads para actualizar los campos de cuenta y contacto.
- for(Account acc :listaDeCuentasRT){
- acc.recordtypeId = rt.id;
- system.debug('El recordtypeId es' +acc.RecordTypeId);
- system.debug('El tamaño de la lista de cuentas:' +listaDeCuentasRT.size());
- for(Account cuenta:listaDeCuentas){
- if(cuenta.CUIT_CUIL__c == acc.CUIT_CUIL__c){
- if((acc.Phone == null || acc.Phone =='') && (cuenta.Phone != null || cuenta.Phone != ''))
- acc.Phone=cuenta.Phone;
- if((acc.Correo_electronico__c == null || acc.Correo_electronico__c == '' ) && (cuenta.Correo_electronico__c != null || cuenta.Correo_electronico__c != ''))
- acc.Correo_electronico__c=cuenta.Correo_electronico__c;
- if((acc.Website == null || acc.website == '') && (cuenta.Website != null || cuenta.Website == '' ))
- acc.Website=cuenta.website;
- }
- }
- }
- update listaDeCuentasRT;
- }
- public static void updateCuentaContacto(List<Account> lstCuentasAAct, List<Lead> lstLeads, List<Contact> lstContact){
- system.debug('Entra update cuenta contacto');
- system.debug('el tamaño de la lista de cuentasACT' +lstCuentasAAct.size());
- system.debug('el tamaño de la lista de leads' +lstLeads.size());
- LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
- RecordType rt =[SELECT id , name FROM RecordType WHERE name = 'PyME'];
- List<string> cuil =new List<string>();
- List<Account> listaDeCuentasRT =new List<Account>();
- List<Database.LeadConvert> leadConvertir =new List<Database.LeadConvert>();
- for(Lead l:lstLeads){
- for(Account lacc :lstCuentasAAct){
- system.debug('el tamaño de la lista de centasACT' +lstCuentasAAct.size());
- if(lacc.CUIT_CUIL__c == l.AVX_CUIT__c){
- Database.LeadConvert lc = new Database.LeadConvert();
- lc.setLeadId(l.id);
- lc.setDoNotCreateOpportunity(true);
- lc.setConvertedStatus(convertStatus.MasterLabel);
- lc.setAccountId(lacc.id);
- for(Contact cont:lstContact){
- if(l.AVX_DNI__c == cont.AVX_DNI__c){
- lc.setContactId(cont.Id);
- if((cont.Phone == null || cont.Phone=='') && (l.Phone != null || l.Phone!=''))
- cont.Phone=l.Phone;
- if((cont.Email == null || cont.Email=='' ) &&( l.Email != null || l.Email != ''))
- cont.email=l.Email;
- 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 != '') )
- cont.Calle__c=String.format('{0}\n{1}\n{2}\n{3}',new String[]{l.street,l.state,l.PostalCode,l.country});
- if(cont.Celular__c == null )
- cont.Celular__c =Decimal.valueOf(l.MobilePhone);
- }
- }
- leadConvertir.add(lc);
- cuil.add(l.AVX_CUIT__c);
- if((lacc.Phone == null || lacc.Phone != '' ) && (l.Phone == null || l.Phone != ''))
- lacc.Phone=l.Phone;
- if((lacc.Correo_electronico__c == null || lacc.Correo_electronico__c !='') && (l.AVX_Correo_electr_nico__c == null || l.AVX_Correo_electr_nico__c != ''))
- lacc.Correo_electronico__c=l.AVX_Correo_electr_nico__c ;
- 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 != ''))
- lacc.Calle__c=String.format('{0}\n{1}\n{2}\n{3}',new String[]{l.street,l.state,l.PostalCode,l.country});
- if((lacc.Celular__c == null || lacc.Celular__c =='' ) && (l.MobilePhone != null || l.MobilePhone != '') )
- lacc.Celular__c=l.MobilePhone;
- //if(lacc.Actividad__c == null)
- // lacc.Actividad__c=l.AVX_Actividad__c;
- if((lacc.Website == null || lacc.Website == '') && (l.Website !=null || l.Website == ''))
- lacc.Website=l.Website;
- }
- }
- }
- Database.convertLead(leadConvertir);
- upsert lstCuentasAAct;
- upsert lstContact;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement