function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
alexjalexj 

test trigger after insert Lead and Account

I have created two triggers to manage the double between Lead and Account (SearchDoubleAndFusionLead and SearchDoubleAndFusionAccount). The two triggers are similar and run correctly.
I have to cover triggers with test class to deploy my test on my prod.
I have write a test class (TestSearchDoubleAndFusion) to test thes trigger. On this class I insert leads and accounts with the same email what it run triggers.
I feel that the test run only the first insert and doesn't consider the case where I insert a account while there are already a lead with the same email and vice versa.

I feel that it run trigger for inserts out of start.Test and stopTest but not for the inserts between.

 

Below one of trigger and the test

trigger SearchDoubleAndFusionLead on Lead (after insert) 
{
    if(checkRecursive.runOnce())
    {
        Set<ID> ids = Trigger.newMap.keySet();
        Set<String> leadmail = new Set<String>();
        List<Lead> updatedLead = new List<Lead>();
        List<Account> updatedAccount = new List<Account>();
        List<Lead> deletedLead = new List<Lead>();
        String demande ='';
        boolean existingAccount = false;
        String URL = ' https://cs16.salesforce.com/';
        
        for (Lead l : trigger.new)// ajout des nouveaux id
        {
            leadmail .add(l.Adresse_mail__c);
            System.debug(l.Adresse_mail__c);
        }
        
        //récupération pistes et comptes existants
        List<Lead> leads = [SELECT  ... from Lead];
        List<Account>accounts = [SELECT ...  FROM Account] ;

        existingAccount = false;
        
        for (Lead lnew : trigger.new)
        {
        
            if(lnew.demande__c !=null || lnew.Demande_contact__c !=null || lnew.Objet_demande__c !=null){
                demande = lnew.Salutation+' '+lnew.FirstName+' '+ lnew.LastName +' a fait une nouvelle demande de contact ' + lnew.Demande_contact__c+' : \n'+lnew.Objet_demande__c+'\n'+lnew.demande__c+' \n'+URL +lnew.Id;
            }
            
            for(Account acc : accounts)
            {
                if(acc .Adresse_mail__c == lnew.Adresse_mail__c){
                    existingAccount = true;
                    System.debug('compte existant');
                    //champs identification de la piste (non modifiable)
                    if(acc.Salutation == null){acc.Salutation = lnew.Salutation;}
                    if(acc.FirstName == null){acc.FirstName = lnew.FirstName;}
                    if(acc.LastName == null){acc.LastName = lnew.LastName;}
                    if(acc.Adresse_mail__c == null){acc.Adresse_mail__c = lnew.Adresse_mail__c;}   
                    
                 
                    //information sur l'adresse. Si un est modifié, tous le sont
                    if(lnew.Street !=null || lnew.City !=null || lnew.PostalCode !=null || lnew.Country !=null || lnew.State !=null)
                    {
                       acc.PersonMailingStreet = lnew.Street;
                       acc.PersonMailingCity = lnew.City ;
                       acc.PersonMailingPostalCode = lnew.PostalCode;
                       acc.PersonMailingCountry = lnew.Country;
                       acc.PersonMailingState = lnew.State ;
                    }
        
                    if(lnew.Phone !=null){acc.PersonHomePhone = lnew.Phone ;}
                    if(lnew.Telephone_portable__c !=null){acc .PersonMobilePhone =  lnew.Telephone_portable__c ;}          
                    if(lnew.Rating !=null){acc .Rating = lnew.Rating ;}
                    if(lnew.Company ==null){acc.Soci_t__c = lnew.Company;}
           
                   if(lnew.Type_d_utilisation__c !=null){acc .Type_d_utilisation__c = lnew.Type_d_utilisation__c ;}
                   if(lnew.locataire__c !=null){acc.locataire__c = lnew.locataire__c;}
                   if(lnew.proprietaire__c !=null){acc.proprietaire__c = lnew.proprietaire__c;}
                   
                    if(lnew.demande__c !=null || lnew.Demande_contact__c !=null || lnew.Objet_demande__c !=null){
                        acc.demande__c = lnew.demande__c;
                        acc.Demande_contact__c = lnew.Demande_contact__c;
                        acc.Objet_demande__c = lnew.Objet_demande__c;
                        demande = acc.Salutation+' '+acc.FirstName+' '+ acc.LastName +' a fait une nouvelle demande de contact ' + acc.Demande_contact__c+' : \n'+acc.Objet_demande__c+'\n'+acc.demande__c+'\n'+URL +acc.Id;
                    }
        
                    
                    //champ relatif à la newsletter
                    if(lnew.Inscription_la_newsletter__c == true){
                        if(acc.Inscription_la_newsletter__c == false){
                            acc.Inscription_la_newsletter__c = true;
                            acc.Date_d_inscription_la_newsletter__c = lnew.Date_d_inscription_la_newsletter__c;
                            acc.D_sinscription_la_newsletter__c = false;
                        }
                    }
                    if(lnew.D_sinscription_la_newsletter__c == true){
                        if(acc.D_sinscription_la_newsletter__c == false){
                            acc.D_sinscription_la_newsletter__c = true;
                            acc.Date_de_d_sinscription_la_newsletter__c = lnew.Date_de_d_sinscription_la_newsletter__c;
                            acc.Raison_de_la_d_sinscription__c = lnew.Raison_de_la_d_sinscription__c;
                            acc.Inscription_la_newsletter__c = false;
                         }
                    }
                    //champ relatif au jeu : pas de mise à jour possible
                    if(acc.accept_jeux__c == false && lnew.accepte_jeux__c == true){
                        acc.accept_jeux__c= lnew.accepte_jeux__c;
                        acc.Date_inscription_au_jeu__c = lnew.Date_inscription_au_jeu__c;                         
                        acc.Email_parr__c = lnew.Email_parr__c;
                        acc.Email_parrainage_2__c = lnew.Email_parrainage_2__c;
                        acc.Email_parrainage_3__c = lnew.Email_parrainage_3__c;
                        acc.Email_parrainage_4__c = lnew.Email_parrainage_4__c;
                        acc.Email_parrainage_5__c = lnew.Email_parrainage_5__c;
                    }       
                                     
                    updatedAccount.add(acc);
                    deletedLead.add(lnew);
                }
              
            }
            for (Lead ld : leads)
            {
                if(ld.Id == lnew.Id)
                {
                    System.debug('Id egal'+ ' '+ld.Id+ ' '+lnew.Id);
                }
                else if(ld.Adresse_mail__c == lnew.Adresse_mail__c)
                {

                    System.debug('piste existante'+ ld.Adresse_mail__c+ ' '+ld.Id+ ' '+lnew.Id);
                    if(!existingAccount){                 
                        if(ld.Salutation == null){ld.Salutation = lnew.Salutation;}
                        if(ld.FirstName == null){ld.FirstName = lnew.FirstName;}
                        if(ld.LastName == null){ld.LastName = lnew.LastName;}
                        if(ld.Num_ro_piste__c == 0 || ld.Num_ro_piste__c ==null ){ld.Num_ro_piste__c = lnew.Num_ro_piste__c;}
                        if(ld.Adresse_mail__c == null){ld.Adresse_mail__c = lnew.Adresse_mail__c;}
                        //if(ld.LeadSource == null){ld.LeadSource = lnew.LeadSource;}
                        if(ld.Client__c == null){ld.Client__c = lnew.Client__c;}
                        
                         //information sur l'adresse. Si un est modifié, tous le sont
                        if(lnew.Street !=null || lnew.City !=null || lnew.PostalCode !=null || lnew.Country !=null || lnew.State !=null)
                        {
                            ld .Street = lnew.Street;
                            ld .City = lnew.City ;
                            ld .PostalCode = lnew.PostalCode;
                            ld .Country = lnew.Country;
                            ld .State = lnew.State ;
                        }
            
                        if(lnew.Phone !=null){ld .Phone = lnew.Phone ;}
                        if(lnew.Telephone_portable__c !=null){ld .Telephone_portable__c =  lnew.Telephone_portable__c ;}          
                        if(lnew.Rating !=null){ld .Rating = lnew.Rating ;}
                        if(lnew.Status !=null){ld .Status = lnew.Status ;} 
                        if(lnew.Company !=null){ld.Company = lnew.Company ;}

                        
                        if(lnew.Type_d_utilisation__c !=null){ld .Type_d_utilisation__c = lnew.Type_d_utilisation__c ;}
                        if(lnew.locataire__c !=null){ld.locataire__c = lnew.locataire__c;}
                        if(lnew.proprietaire__c !=null){ld.proprietaire__c = lnew.proprietaire__c;}
                        if(lnew.videosurveillance__c !=null){ld.videosurveillance__c = lnew.videosurveillance__c;}
                        if(lnew.pas_de_systeme__c !=null){ld.pas_de_systeme__c = lnew.pas_de_systeme__c;}
                        if(lnew.alarme__c !=null){ld.alarme__c = lnew.alarme__c;}
                        if(lnew.alarme_et_videosurveillance__c !=null){ld.alarme_et_videosurveillance__c = lnew.alarme_et_videosurveillance__c;}
                        if(lnew.demande__c !=null || lnew.Demande_contact__c !=null || lnew.Objet_demande__c !=null){
                            ld.demande__c = lnew.demande__c;
                            ld.Demande_contact__c = lnew.Demande_contact__c;
                            ld.Objet_demande__c = lnew.Objet_demande__c;
                            System.debug('ld '+ld.Id+' lnew '+lnew.Id);
                            demande = ld.Salutation+' '+ld.FirstName+' '+ ld.LastName +' a fait une nouvelle demande de contact ' + ld.Demande_contact__c+' :\n'+ld.Objet_demande__c+'\n'+ld.demande__c+'\n'+URL +ld.Id;
                
                         }
            
                        
                      
                        //champ relatif à la newsletter
                        if(lnew.Inscription_la_newsletter__c == true){
                            if(ld.Inscription_la_newsletter__c == false){
                                ld.Inscription_la_newsletter__c = true;
                                System.debug( ld.Inscription_la_newsletter__c);
                                ld.Date_d_inscription_la_newsletter__c = lnew.Date_d_inscription_la_newsletter__c;
                                ld.D_sinscription_la_newsletter__c = false;
                            }
                        }
                        else if(lnew.D_sinscription_la_newsletter__c == true){
                            if(ld.D_sinscription_la_newsletter__c == false){
                                ld.D_sinscription_la_newsletter__c = true;
                                ld.Date_de_d_sinscription_la_newsletter__c = lnew.Date_de_d_sinscription_la_newsletter__c;
                                ld.Raison_de_la_d_sinscription__c = lnew.Raison_de_la_d_sinscription__c;
                                ld.Inscription_la_newsletter__c = false;
                             }
                        }
                        
                        
                       // System.debug('mise à jour nl');
                        //champ relatif au jeu : pas de mise à jour possible
                        if(ld.accepte_jeux__c == false && lnew.accepte_jeux__c == true){
                            ld.accepte_jeux__c = lnew.accepte_jeux__c;
                            ld.Date_inscription_au_jeu__c = lnew.Date_inscription_au_jeu__c;                         
                            ld.Email_parr__c = lnew.Email_parr__c;
                            ld.Email_parrainage_2__c = lnew.Email_parrainage_2__c;
                            ld.Email_parrainage_3__c = lnew.Email_parrainage_3__c;
                            ld.Email_parrainage_4__c = lnew.Email_parrainage_4__c;
                            ld.Email_parrainage_5__c = lnew.Email_parrainage_5__c;
                        }
                        updatedLead.add(ld);
                        deletedLead.add(lnew);
                    }
                    else
                        deletedLead.add(ld); 
                }
            }
            
            if(lnew.demande__c !=null || lnew.Demande_contact__c !=null || lnew.Objet_demande__c !=null){
                System.debug(demande);
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                String[] toAddresses = new String[] {'h.broussin@kiwatch.com'}; 
                mail.setToAddresses(toAddresses); 
                mail.setSubject('Demande de contact');
                mail.setPlainTextBody(demande);
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            }
            
        }
        if(updatedLead.size()>0){
            System.debug('mise à jour');
            update updatedLead;
        }
        
        if(updatedAccount.size()>0){
            System.debug('mise à jour compte');
            update updatedAccount;
        }

        if(deletedLead.size()>0){
            for (Lead l : deletedLead){
                System.debug(l.Id);
                MyFutureClass.DeleteLead(l.Id);
            }

        }
    }

}

 

public class TestSearchDoubleAndFusion {
    static testMethod void TestInsertListLead(){
    System.debug('insert leads');
    List <Lead> ld = new List<Lead>();
    for(integer i = 0; i<2; i++){
    
        Lead lnew = new Lead(
            Salutation = '',
            FirstName ='',
            LastName = 'test',
            Adresse_mail__c = 'test@mail.com',
            LeadSource = 'Newsletter',
            Origine_connaissance_Kiwatch__c ='',
            Num_ro_piste__c = 0,
            Client__c = false,
            accepte_jeux__c = true,
            Email_parr__c = 'h.broussin@kiwatch.com',
            Email_parrainage_2__c = '',
            Email_parrainage_3__c = '',
            Email_parrainage_4__c = '',
            Email_parrainage_5__c = '',
            Street = '39, rue des étoiles',
            City = 'laval',
            State = 'pays de loire',
            PostalCode = '53000',
            Country = 'france',
            Phone = '0900000000',
            Telephone_portable__c = '070809101112',
            Rating = 'Froid',
            Status = 'En cours',
            Inscription_la_newsletter__c = true,
            D_sinscription_la_newsletter__c = false,
            Raison_de_la_d_sinscription__c = '',
            locataire__c = true,
            proprietaire__c = false,
            alarme__c = false,
            videosurveillance__c = false,
            alarme_et_videosurveillance__c = false,
            pas_de_systeme__c = true,
            Type_d_utilisation__c = 'Pro',
            demande__c = 'par tel',
            Demande_contact__c = 'gfsdf',
            Objet_demande__c = 'sdf');
            
            ld.add(lnew);
      }
      insert ld;
     System.debug('lead correctement insérer');

     Account a = new Account(
            Salutation = '',
            FirstName ='',
            LastName = 'test',
            Adresse_mail__c = 'test@mail.com',
            PersonLeadSource = 'Newsletter',
            Numero_client__c = 0,
            PersonMailingStreet = '39, rue des étoiles',
            PersonMailingCity = 'laval',
            PersonMailingState = 'pays de loire',
            PersonMailingPostalCode = '53000',
            PersonMailingCountry = 'france',
            PersonHomePhone = '0900000000',
            PersonMobilePhone = '070809101112',
            Rating = 'Froid',
            Inscription_la_newsletter__c = false,
            D_sinscription_la_newsletter__c = false,
            Raison_de_la_d_sinscription__c = '',
            Type_d_utilisation__c = 'Pro',
            demande__c = 'par mail',
            Demande_contact__c = 'sdkljfqsd',
            Objet_demande__c = 'sdfklsqfdkjs',           
            Raison_fin_de_contrat__c ='',
            Abonnement__c='Pro');
            
            test.startTest();
            System.debug('debut test');
            insert a;
             System.debug('compte insérer');
            test.stopTest();

    }
    
   static testMethod void TestInsertListAccount(){
      System.debug('insert accounts');
       List <Account> acc = new List<Account>();
       for(integer i = 0; i<2; i++){
       Account anew = new Account(
            Salutation = '',
            FirstName ='',
            LastName = 'test',
            Adresse_mail__c = 'test@mail.com',
            PersonLeadSource = 'Newsletter',
            Numero_client__c = 0,
            PersonMailingStreet = '39, rue des étoiles',
            PersonMailingCity = 'laval',
            PersonMailingState = 'pays de loire',
            PersonMailingPostalCode = '53000',
            PersonMailingCountry = 'france',
            PersonHomePhone = '0900000000',
            PersonMobilePhone = '070809101112',
            Rating = 'Froid',
            Inscription_la_newsletter__c = false,
            D_sinscription_la_newsletter__c = false,
            Raison_de_la_d_sinscription__c = '',
           Type_d_utilisation__c = 'Pro',
            demande__c = 'par mail',
            Demande_contact__c = 'sdkljfqsd',
            Objet_demande__c = 'sdfklsqfdkjs',
            Raison_fin_de_contrat__c ='',
            Abonnement__c='Pro');
            
            acc.add(anew);
        }            
        insert acc;  
        Lead l = new Lead(
            Salutation = '',
            FirstName ='',
            LastName = 'test',
            Adresse_mail__c = 'test@mail.com',
            LeadSource = 'Newsletter',
            Origine_connaissance_Kiwatch__c ='',
            Num_ro_piste__c = 0,
            Client__c = false,
            accepte_jeux__c = true,
           // Date_inscription_au_jeu__c = ,
            Email_parr__c = 'h.broussin@kiwatch.com',
            Email_parrainage_2__c = '',
            Email_parrainage_3__c = '',
            Email_parrainage_4__c = '',
            Email_parrainage_5__c = '',
            Street = '39, rue des étoiles',
            City = 'laval',
            State = 'pays de loire',
            PostalCode = '53000',
            Country = 'france',
            Phone = '0900000000',
            Telephone_portable__c = '070809101112',
            Rating = 'Froid',
            Status = 'En cours',
            Inscription_la_newsletter__c = true,
            D_sinscription_la_newsletter__c = false,
            Raison_de_la_d_sinscription__c = '',
            locataire__c = true,
            proprietaire__c = false,
            alarme__c = false,
            videosurveillance__c = false,
            alarme_et_videosurveillance__c = false,
            pas_de_systeme__c = true,
            Type_d_utilisation__c = 'Pro',
            demande__c = 'par tel',
            Demande_contact__c = 'gfsdf',
            Objet_demande__c = 'sdf');

             test.startTest();
            System.debug('debut test');
            insert l;
             System.debug('lead insérer');
            test.stopTest();
    }
}