You need to sign in to do that
Don't have an account?
SFDC 2017
Test coverage for Trigger
I want Test Class For this Trigger for me it is covering 38 % i thinkk iam wron any one please help me .This is urgenyt issue.can anyone spend time on this
Here is my Trigger:
trigger UpdateNormalLeadwithWebLead on Lead (after insert){
set<String> setEmails = new set<String>();
List<Lead> updateExistingLead =New List<Lead>();
List<Lead> deleteNewDupLead =New List<Lead>();
map<String,Lead> mLeadsExisting = new map<String,Lead>();
map<String,Lead> Leadmap = new map<String,Lead>();
for(Lead LeadObj : trigger.new){
if(LeadObj.Email != null){
setEmails.add(LeadObj.Email);
Leadmap.put(LeadObj.Email,LeadObj);
}
}
System.debug('@@@Leadmap@@@'+Leadmap);
if(setEmails.size() > 0 ){
for(Lead LeadObj : [SELECT id,Email,LastName,Company,phone,isConverted FROM Lead where Email in : setEmails AND isConverted =False AND RecordType.Name='Registration']){
mLeadsExisting.put(LeadObj.Email,LeadObj);
}
}
System.debug('@@@mLeadsExisting@@@'+mLeadsExisting);
for(Lead LObj: [SELECT id,Email,LastName,FirstName,Company,phone FROM Lead WHERE Id NOT IN:Trigger.new]){
if(LObj.Email != null && Leadmap.ContainsKey(LObj.Email )){
//LObj.MobilePhone= Leadmap.get(LObj.Email).MobilePhone;
LObj.Phone = Leadmap.get(LObj.Email).Phone;
LObj.LastName = Leadmap.get(LObj.Email).LastName;
LObj.FirstName = Leadmap.get(LObj.Email).FirstName;
LObj.Company= Leadmap.get(LObj.Email).Individual_Company__c;
LObj.City= Leadmap.get(LObj.Email).City;
LObj.Street= Leadmap.get(LObj.Email).Street;
LObj.State= Leadmap.get(LObj.Email).State;
LObj.Registered_Online__c=Leadmap.get(LObj.Email).Registered_Online__c;
LObj.Title=Leadmap.get(LObj.Email).Title;
LObj.Lead_Trigger__c=false;
updateExistingLead.add(LObj);
}
}
System.debug('@@@updateExistingLead@@@'+updateExistingLead);
if(!updateExistingLead.isEmpty()){
try{
Update updateExistingLead;
System.debug('@@@updateExistingLead1@@@'+updateExistingLead);
}
catch(Exception e){
System.debug('@@@Insert Failed Due to@@@'+e);
}
for(Lead LeadObj : [SELECT id,Email,LastName,FirstName,Company,phone FROM Lead WHERE Id IN:Trigger.new]){
if(mLeadsExisting.containsKey(LeadObj.Email)){
deleteNewDupLead.add(LeadObj);
}
}
if(!deleteNewDupLead.isEmpty()){
delete deleteNewDupLead;
}
}
}
Here is my Trigger:
trigger UpdateNormalLeadwithWebLead on Lead (after insert){
set<String> setEmails = new set<String>();
List<Lead> updateExistingLead =New List<Lead>();
List<Lead> deleteNewDupLead =New List<Lead>();
map<String,Lead> mLeadsExisting = new map<String,Lead>();
map<String,Lead> Leadmap = new map<String,Lead>();
for(Lead LeadObj : trigger.new){
if(LeadObj.Email != null){
setEmails.add(LeadObj.Email);
Leadmap.put(LeadObj.Email,LeadObj);
}
}
System.debug('@@@Leadmap@@@'+Leadmap);
if(setEmails.size() > 0 ){
for(Lead LeadObj : [SELECT id,Email,LastName,Company,phone,isConverted FROM Lead where Email in : setEmails AND isConverted =False AND RecordType.Name='Registration']){
mLeadsExisting.put(LeadObj.Email,LeadObj);
}
}
System.debug('@@@mLeadsExisting@@@'+mLeadsExisting);
for(Lead LObj: [SELECT id,Email,LastName,FirstName,Company,phone FROM Lead WHERE Id NOT IN:Trigger.new]){
if(LObj.Email != null && Leadmap.ContainsKey(LObj.Email )){
//LObj.MobilePhone= Leadmap.get(LObj.Email).MobilePhone;
LObj.Phone = Leadmap.get(LObj.Email).Phone;
LObj.LastName = Leadmap.get(LObj.Email).LastName;
LObj.FirstName = Leadmap.get(LObj.Email).FirstName;
LObj.Company= Leadmap.get(LObj.Email).Individual_Company__c;
LObj.City= Leadmap.get(LObj.Email).City;
LObj.Street= Leadmap.get(LObj.Email).Street;
LObj.State= Leadmap.get(LObj.Email).State;
LObj.Registered_Online__c=Leadmap.get(LObj.Email).Registered_Online__c;
LObj.Title=Leadmap.get(LObj.Email).Title;
LObj.Lead_Trigger__c=false;
updateExistingLead.add(LObj);
}
}
System.debug('@@@updateExistingLead@@@'+updateExistingLead);
if(!updateExistingLead.isEmpty()){
try{
Update updateExistingLead;
System.debug('@@@updateExistingLead1@@@'+updateExistingLead);
}
catch(Exception e){
System.debug('@@@Insert Failed Due to@@@'+e);
}
for(Lead LeadObj : [SELECT id,Email,LastName,FirstName,Company,phone FROM Lead WHERE Id IN:Trigger.new]){
if(mLeadsExisting.containsKey(LeadObj.Email)){
deleteNewDupLead.add(LeadObj);
}
}
if(!deleteNewDupLead.isEmpty()){
delete deleteNewDupLead;
}
}
}
As @James Loghry said u have to insert lead first then create new lead to meet ur criteria.
Try below code and let me know if u have any issue
Thanks,
Pavanthetech
All Answers
@isTest
private class TestupdateLead{
static testMethod void LeadTriggeraction() {
Lead newTestLead = new Lead(
LastName = 'John Test',
Salutation='Mr.',
Company = 'Salesforce.com',
LeadSource = 'Anahat Website',
MobilePhone='9087654321',
Phone ='080345678',
City='Bangalore',
Street='123 Th Street',
State='Karnataka',
Title='Manager',
FirstName='Smith',
Email='abc@gmail.com',
Registered_Online__c=true
);
INSERT newTestLead;
newTestLead =
[SELECT id,Email,Registered_Online__c,LeadSource,LastName,Salutation,Company,MobilePhone,Phone,City,
Street,
State,Title ,FirstName FROM
Lead
WHERE
Email =:newTestLead.Email
];
System.assertEquals(true,newTestLead.Registered_Online__c);
System.assertEquals('Anahat Website',newTestLead.LeadSource);
System.assertEquals('John Test',newTestLead.LastName);
System.assertEquals('Mr.',newTestLead.Salutation);
System.assertEquals('Salesforce.com',newTestLead.Company);
System.assertEquals('080345678',newTestLead.Phone);
System.assertEquals('9087654321',newTestLead.MobilePhone);
System.assertEquals('Bangalore',newTestLead.City);
System.assertEquals('Karnataka',newTestLead.State);
System.assertEquals('Manager',newTestLead.Title);
}
}
As @James Loghry said u have to insert lead first then create new lead to meet ur criteria.
Try below code and let me know if u have any issue
Thanks,
Pavanthetech