You need to sign in to do that
Don't have an account?
alexj
Problem with test trigger
I have created a trigger with test a trigger after insert.
The problem is that the trigger run on the initialization of leads and accounts and don't run on insert which is between test.startTest et test.stopTest.
@isTest public class TestinsertLead { static testMethod void TestInsertListLead(){ //initialisation des variables List <Lead> ld = new List<Lead>(); List <Account> acc = new List<Account>(); for(integer i = 0; i<2; i++){ Lead lead = new Lead( Salutation = 'M.', FirstName ='Hélène', LastName = 'test', Adresse_mail__c = 'test'+i+'@mail.com',//test0@mail.com, test1@mail.com LeadSource = 'Newsletter', Origine_connaissance_Kiwatch__c ='', Num_ro_piste__c = 0, demande__c = 'par mail', Demande_contact__c = 'demande', Objet_demande__c = 'objet'); ld.add(lead); } for(integer i = 2; i<4; i++){ Account account= new Account( Salutation = '', FirstName ='', LastName = 'test', Adresse_mail__c = 'test'+i+'@mail.com',//test2@mail.com, test3@mail.com PersonLeadSource = 'Newsletter', Numero_client__c = 0, Demande_contact__c = 'sdkljfqsd', Objet_demande__c = 'sdfklsqfdkjs', Raison_fin_de_contrat__c ='', Abonnement__c='Pro'); acc.add(account); } insert ld;System.debug('insert of init leads ok'); insert acc;System.debug('insert of init accounts ok'); //début du test Test.startTest(); List <Lead> newLeads= new List<Lead>(); for(integer i = 1; i<3; i++){ Lead lead = new Lead( Salutation = 'M.', FirstName ='Hélène', LastName = 'test', Adresse_mail__c = 'test'+i+'@mail.com',//test1@mail.com, test2@mail.com LeadSource = 'Newsletter', Origine_connaissance_Kiwatch__c ='', Num_ro_piste__c = 0, demande__c = 'par mail', Demande_contact__c = 'demande', Objet_demande__c = 'objet'); newLeads.add(lead); } insert newLeads;System.debug('insert of test leads ok'); Test.stopTest() ; } }
When I display debug, I see :
|DEBUG|insert piste : test0@mail.com |DEBUG|insert piste : test1@mail.com |DEBUG|insert of init leads ok |DEBUG|insert of init accounts ok |DEBUG|insert of test leads ok
I thought have result my problem by activating one trigger (trigger after insert Lead and trigger after insert Account). It work on Sandbox to test the coverage.
The problem is that I can't deploy because when my two trigger is activating, the code isn't correctly covered.
How can I activate my two trigger and test one after the other ?
What exception are you getting when you're trying to deploy? Also this is a bit hard to test without seeing or explaining what your trigger is doing... I can give you guesses.
I have no exception. The problem is that the code isn't covered to 75 %.
My trigger enable to verify if there already is a lead (or an acocunt) with the same email and if it's the case, merge the way.
When I test myself the trigger by directly inserting account or lead on sandbox, the trigger run correctly.
Here a part of my trigger :
I already see a few problems with your trigger.
1) Issues once your org gets big enough you'll have to have limits on records you are pulling back
2) Executed Lines Script exception (once you pass 200k)
You can make this a lot simpler just by removing your multiple nested loops and using your set<String> which is your email address to query for exising leads IE:
This concept should be used on your accounts as well.
Once you're done getting back your records put them in a map if you'd like and use the key to hold emails.
Again use this concept to check out your accounts and do any mapping you need.
Also this should make you cover your test cases a lot easier.
Last thing, and this is just personal preference is add the annotation parameter @isTest(SeeAllData=false). Just because records could already exist that may screw up what you are testing.
OK,
What is the interrest to use a Map ?
I have modified my trigger but there always only 52% of code covered
And my test class is :
The reason for the map is later on your trigger could hit govenor limits due to the loops previously and amount records being pulled back. Thats why I suggested that. As far as your code, the only thing that looks like you are not hitting is your merge conditions. Is this correct?
if(ld.Salutation == null){ld.Salutation = lnew.Salutation;}
This tells me that it will only go inside and copy the same field content if the value is null. Because of this you're not getting inside your IF statements. If this isn't it. Can you please point out which lines of code you are not hitting?
Indeed, some field are unmodifiable (like Salutation) when there are already filled.
Other are modifiable :
I don't think that the problem is the trigger because when I test myself by inserting lead directly on sandbox, it's run correctly. I think that the probleme is the test class by I don't understand why the trigger don't run for the second insert.
up please
Anybody knows why my trigger don't run when I call it a second time ?