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
MNaethlerMNaethler 

improve Code Coverage

Hey Guys,

 

here i am again.

 

so, well, i wanted to deploy my trigger to prevent duplicates, the trigger works definetely.

 

but i cant deploy it during Code coverage from 63% (21/33)

 

there u got a picture from the red lines in my code who isnt checked.

http://www7.pic-upload.de/30.09.13/wkjkc9yifc66.png

 

 

and here is the Error from Deployment try

http://www7.pic-upload.de/30.09.13/8kxhclrs27ol.png

 

 

So my qustion is, how can i fix it that i can deploy it from sandbox in my organisation? 

 

souvik9086souvik9086

Image is not opening. Please post the trigger here along with your test class and red lines.

 

Thanks

MNaethlerMNaethler
 // mnaethler 09/06/2013, 
// edited: 09/12/2013 added the confirmation from Street (which can contains several companies) dont have the same phone or mobilephone number from an existing lead &
// take a check for an empty adress, phone or mobilephone number and email adress too

trigger preventDuplicatedLeadEntries on Lead (before insert, before update) 
{
    Map<String, Lead> leadMap = new Map<String, Lead>();
    for (Lead lead : System.Trigger.new) 
    {
        // To make sure we don't treat an email address, phone or mobilephone number 
        // that isn't changing during an update as a duplicate.   
        if ((lead.Email != null) && (System.Trigger.isInsert || (lead.Email != System.Trigger.oldMap.get(lead.Id).Email)))
        {
            // To make sure another new lead isn't also a duplicate 
            if (leadMap.containsKey(lead.Email)) 
            {
                lead.Email.addError('Another new lead has the same email address.');
            } 
            else 
            {
                leadMap.put(lead.Email, lead);
            }
        }  
        if ((lead.Phone != null) && (System.Trigger.isInsert || (lead.Phone != System.Trigger.oldMap.get(lead.Id).Phone)))
        {
            if (leadMap.containsKey(lead.Phone)) 
            {
                lead.Phone.addError('Another new lead has the same Phone number.');
            } 
            else 
            {
                leadMap.put(lead.Phone, lead);
            }
        }
        if ((lead.MobilePhone != null) && (System.Trigger.isInsert || (lead.MobilePhone != System.Trigger.oldMap.get(lead.Id).MobilePhone)))
        {
            if (leadMap.containsKey(lead.MobilePhone)) 
            {
                lead.MobilePhone.addError('Another new lead has the same Mobilephone number.');
            } 
            else 
            {
                leadMap.put(lead.MobilePhone, lead);
            }
        }
        
        //To make sure that the Street (which can contains several companies) dont have the same phone or mobilephone number from an existing lead 
        if (((lead.Street != null) && (System.Trigger.isInsert || (lead.Street != System.Trigger.oldMap.get(lead.Id).Street)))
            &&
            ((lead.Email != null) && (System.Trigger.isInsert || (lead.Email != System.Trigger.oldMap.get(lead.Id).Email))
             &&
              ((lead.Phone != null) && (System.Trigger.isInsert || (lead.Phone != System.Trigger.oldMap.get(lead.Id).Phone))
                ||
                 ((lead.MobilePhone != null) && (System.Trigger.isInsert || (lead.MobilePhone != System.Trigger.oldMap.get(lead.Id).MobilePhone))))))
        {
            lead.addError('A lead already exists with this Informations, pls review the phone and mobilenumber and check the streetadress');
        }
                
        // To make sure that the customer has entered a mobilephone or telephon number or a address to contact him later
        if ((lead.MobilePhone == null) && (System.Trigger.isInsert || (lead.MobilePhone != System.Trigger.oldMap.get(lead.Id).MobilePhone)))
        {
            if ((lead.Phone == null) && (system.Trigger.isInsert || (lead.Phone != System.Trigger.oldMap.get(lead.Id).Phone)))
            {
                if ((lead.Street == null) && (system.Trigger.isInsert || (lead.Phone != System.Trigger.oldMap.get(lead.Id).Street)))
                {
                    if ((lead.Email == null) && (system.Trigger.isInsert || (lead.Phone != System.Trigger.oldMap.get(lead.Id).Email)))
                  {
                      lead.MobilePhone.addError('You need at least one contact option, a streetadress, a phone or a mobilephone number');
                    }
                }
            }
      }
    }
    // Using a single database query, find all the leads in the database that have the same 
    // email adress, phone number or mobilephone number as any of the leads being inserted or updated. 
    
    for (Lead lead : [SELECT Email FROM Lead WHERE Email IN :leadMap.KeySet()]) 
    {
        Lead newLead = leadMap.get(lead.Email);
        newLead.Email.addError('A lead with this email address already exists.');
    }
    for (Lead lead : [SELECT Phone FROM Lead WHERE Phone IN :leadMap.KeySet()]) 
    {
        Lead newLead = leadMap.get(lead.Phone);
        newLead.Phone.addError('A lead with this phone number already exists.');
    }
    for (Lead lead : [SELECT MobilePhone FROM Lead WHERE MobilePhone IN :leadMap.KeySet()]) 
    {
        Lead newLead = leadMap.get(lead.MobilePhone);
        newLead.MobilePhone.addError('A lead with this mobilephone number already exists.');
    }
    for (Lead lead : [SELECT Street FROM Lead WHERE Street IN :leadMap.KeySet()]) 
    {
        Lead newLead = leadMap.get(lead.Street);
        newLead.Street.addError('A lead with this address already exists.');
    }
}
MNaethlerMNaethler

And thats the Error from the deployment .

 

preventDuplicateLeadEntriesByEmailAndPhoneAndMobilePhone      

Testcoverage for listed Apex Trigger is 0 %; at least 1 % Testcoverage is required

 

 

Deploy Error      

The mean size of Testcoverage for all Apex-classes and -Trigger is 11 %; at least 75 % Testcoverage is required

souvik9086souvik9086

@isTest

public class MytestController {

 

public testmethod void myTestMethod() {

 

Lead lead = new Lead();

lead.lastname = 'Test';

lead.company = 'Test';

lead.MobilePhone = '9999999999';

lead.Phone = '03412222536';

lead.Email = 'test@gmail.com';

insert lead;

lead.MobilePhone = '9999999998';

lead.Phone = '03412222537';

lead.Email = 'test1@gmail.com';

update lead;

}

}

 

Add this to your testclass and let me know what coverage it does.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

MNaethlerMNaethler

it aint work with "public testmethod void myTestMethod()",so i changed it to static cus he said me that Only static top-level class methods can be test methods salesforce.

 

well anyway, there is n coverage then, he didnt even shows me 0%(NO coverage data), there is just an empty field

souvik9086souvik9086

I told you to add the code I gave to you on top of your code. This is not the complete code. I guess through your test class code the leadmap is populating while inserting the datas then you have to change the email,phone/mobile phone datas and have to use update to cover those red lines.

 

Thanks

MNaethlerMNaethler

Well,i think we got a missunderstanding here or i dont know what u mean where i have to paste your code. Cause you talk about a class but i wrote a trigger, where should i paste it there, it isnt a testclass

 

sry for the developed circumstances. but im relativly new to this

souvik9086souvik9086

You have a testclass right? Where you covered the other lines. There you can merge the things mentioned by me above.

MNaethlerMNaethler

if u mean where i test my trigger with a testinsert class : no,i have tested my trigger by manuelly insert a new lead and delete the lead, as long as my Trigger worked fully.

 

if u mean other several classes with Test in the name yes, from DupeCatcher, salesforce and force.com

souvik9086souvik9086

You have to write a test class to cover the your trigger code coverage.

MNaethlerMNaethler

HEy there.

i got a testclas now, and the testclass works. I tried it with email, telephon and mobile phone, but my trigger got now a coverage from 57% (19/33)   

 


trigger preventDuplicatedLeadEntries on Lead (before insert, before update) 
{
    Map<String, Lead> leadMap = new Map<String, Lead>();
    for (Lead lead : System.Trigger.new) 
    {
        // To make sure we don't treat an email address, phone or mobilephone number 
        // that isn't changing during an update as a duplicate.   
        if ((lead.Email != null) && (System.Trigger.isInsert || (lead.Email != System.Trigger.oldMap.get(lead.Id).Email)))
        {
            // To make sure another new lead isn't also a duplicate 
            if (leadMap.containsKey(lead.Email)) 
            {
                lead.Email.addError('Another new lead has the same email address.');
            } 
            else 
            {
                leadMap.put(lead.Email, lead);
            }
        }  
        if ((lead.Phone != null) && (System.Trigger.isInsert || (lead.Phone != System.Trigger.oldMap.get(lead.Id).Phone)))
        {
            if (leadMap.containsKey(lead.Phone)) 
            {
                lead.Phone.addError('Another new lead has the same Phone number.');
            } 
            else 
            {
                leadMap.put(lead.Phone, lead);
            }
        }
        if ((lead.MobilePhone != null) && (System.Trigger.isInsert || (lead.MobilePhone != System.Trigger.oldMap.get(lead.Id).MobilePhone)))
        {
            if (leadMap.containsKey(lead.MobilePhone)) 
            {
                lead.MobilePhone.addError('Another new lead has the same Mobilephone number.');
            } 
            else 
            {
                leadMap.put(lead.MobilePhone, lead);
            }
        }
        
        //To make sure that the Street (which can contains several companies) dont have the same phone or mobilephone number from an existing lead 
        if (((lead.Street != null) && (System.Trigger.isInsert || (lead.Street != System.Trigger.oldMap.get(lead.Id).Street)))
            &&
            ((lead.Email != null) && (System.Trigger.isInsert || (lead.Email != System.Trigger.oldMap.get(lead.Id).Email))
             &&
              ((lead.Phone != null) && (System.Trigger.isInsert || (lead.Phone != System.Trigger.oldMap.get(lead.Id).Phone))
                ||
                 ((lead.MobilePhone != null) && (System.Trigger.isInsert || (lead.MobilePhone != System.Trigger.oldMap.get(lead.Id).MobilePhone))))))
        {
            lead.addError('A lead already exists with this Informations, pls review the phone and mobilenumber and check the streetadress');
        }
                
        // To make sure that the customer has entered a mobilephone or telephon number or a address to contact him later
        if ((lead.MobilePhone == null) && (System.Trigger.isInsert || (lead.MobilePhone != System.Trigger.oldMap.get(lead.Id).MobilePhone)))
        {
            if ((lead.Phone == null) && (system.Trigger.isInsert || (lead.Phone != System.Trigger.oldMap.get(lead.Id).Phone)))
            {
                if ((lead.Street == null) && (system.Trigger.isInsert || (lead.Phone != System.Trigger.oldMap.get(lead.Id).Street)))
                {
                    if ((lead.Email == null) && (system.Trigger.isInsert || (lead.Phone != System.Trigger.oldMap.get(lead.Id).Email)))
                  {
                      lead.MobilePhone.addError('You need at least one contact option, a streetadress, a phone or a mobilephone number');
                    }
                }
            }
      }
    }
    // Using a single database query, find all the leads in the database that have the same 
    // email adress, phone number or mobilephone number as any of the leads being inserted or updated. 
    
    for (Lead lead : [SELECT Email FROM Lead WHERE Email IN :leadMap.KeySet()]) 
    {
        Lead newLead = leadMap.get(lead.Email);
        newLead.Email.addError('A lead with this email address already exists.');
    }
    for (Lead lead : [SELECT Phone FROM Lead WHERE Phone IN :leadMap.KeySet()]) 
    {
        Lead newLead = leadMap.get(lead.Phone);
        newLead.Phone.addError('A lead with this phone number already exists.');
    }
    for (Lead lead : [SELECT MobilePhone FROM Lead WHERE MobilePhone IN :leadMap.KeySet()]) 
    {
        Lead newLead = leadMap.get(lead.MobilePhone);
        newLead.MobilePhone.addError('A lead with this mobilephone number already exists.');
    }
    for (Lead lead : [SELECT Street FROM Lead WHERE Street IN :leadMap.KeySet()]) 
    {
        Lead newLead = leadMap.get(lead.Street);
        newLead.Street.addError('A lead with this address already exists.');
    }
}