You need to sign in to do that
Don't have an account?
Trigger with helper class
Hi,
I have created a Trigger and used Helper class in the trigger and created the Test Class which is covering 96% of helper class but when I am trying to deploy the code then it is showing 20% code coverage bcoz of which I am unable to deploy the code. Can anyone help?
Trigger
trigger lead_agent_trigger on Lead (before insert,before Update)
{
Lead_Agent_Trigger_Helper.Agent(trigger.new);
}
Helper Class:-
public class Lead_Agent_Trigger_Helper {
public void Agent(List<Lead> Leads)
{
for(Lead loopVar : Leads)
{
try
{
string country = loopVar.Country;
//Agents__c agent = [select Id,Agent_Name__c,agent_email__c from Agents__c where Country__c like : country and Active__c = True and Edition__c =: loopVar.Edition__c limit 1];
if(loopVar.Sent_to_Agent__c==true)
{
if(loopVar.Sector_show__c == null && loopVar.Sectors__c == null)
{
Agents__c agent = [select Id,Agent_Name__c,agent_email__c,Sector_show__c,Sectors__c from Agents__c where
Country__c like : country and
Active__c = True and
Edition__c =: loopVar.Edition__c and
Sector_show__c = null and
Sectors__c = null limit 1];
loopVar.Agent__c = agent.Agent_Name__c;
loopVar.agent_email__c= agent.agent_email__c;
Contact contact = [select AccountId,Email from Contact where Id =: agent.Agent_Name__c limit 1];
loopVar.Agent_Account__c = contact.AccountId;
loopVar.Agents__c = agent.Id;
loopVar.Date_sent_to_Agent__c = date.Today();
}
else if(loopVar.Sector_show__c != null && loopVar.Sectors__c != null)
{
String sector = loopVar.Sectors__c;
Agents__c agent1 = [select Id,Agent_Name__c,agent_email__c,Sector_show__c,Sectors__c from Agents__c
where Country__c like : country
and Active__c = True and Edition__c =: loopVar.Edition__c
and Sector_show__c =: loopVar.Sector_show__c
and Sectors__c Includes (:sector) limit 1];
loopVar.Agent__c = agent1.Agent_Name__c;
loopVar.agent_email__c= agent1.agent_email__c;
Contact contact1 = [select AccountId,Email from Contact where Id =: agent1.Agent_Name__c limit 1];
loopVar.Agent_Account__c = contact1.AccountId;
loopVar.Agents__c = agent1.Id;
loopVar.Date_sent_to_Agent__c = date.Today();
}
}
}
catch(Exception E){}
}
}
}
Test Class:-
public class Lead_Agent_Trigger_Helper {
public void Agent(List<Lead> Leads)
{
for(Lead loopVar : Leads)
{
try
{
string country = loopVar.Country;
//Agents__c agent = [select Id,Agent_Name__c,agent_email__c from Agents__c where Country__c like : country and Active__c = True and Edition__c =: loopVar.Edition__c limit 1];
if(loopVar.Sent_to_Agent__c==true)
{
if(loopVar.Sector_show__c == null && loopVar.Sectors__c == null)
{
Agents__c agent = [select Id,Agent_Name__c,agent_email__c,Sector_show__c,Sectors__c from Agents__c where
Country__c like : country and
Active__c = True and
Edition__c =: loopVar.Edition__c and
Sector_show__c = null and
Sectors__c = null limit 1];
loopVar.Agent__c = agent.Agent_Name__c;
loopVar.agent_email__c= agent.agent_email__c;
Contact contact = [select AccountId,Email from Contact where Id =: agent.Agent_Name__c limit 1];
loopVar.Agent_Account__c = contact.AccountId;
loopVar.Agents__c = agent.Id;
loopVar.Date_sent_to_Agent__c = date.Today();
}
else if(loopVar.Sector_show__c != null && loopVar.Sectors__c != null)
{
String sector = loopVar.Sectors__c;
Agents__c agent1 = [select Id,Agent_Name__c,agent_email__c,Sector_show__c,Sectors__c from Agents__c
where Country__c like : country
and Active__c = True and Edition__c =: loopVar.Edition__c
and Sector_show__c =: loopVar.Sector_show__c
and Sectors__c Includes (:sector) limit 1];
loopVar.Agent__c = agent1.Agent_Name__c;
loopVar.agent_email__c= agent1.agent_email__c;
Contact contact1 = [select AccountId,Email from Contact where Id =: agent1.Agent_Name__c limit 1];
loopVar.Agent_Account__c = contact1.AccountId;
loopVar.Agents__c = agent1.Id;
loopVar.Date_sent_to_Agent__c = date.Today();
}
}
}
catch(Exception E){}
}
}
Error:-
you have pasted your apex class as a test class, put correct test class.
The reason is your test class is fails to run successfully in production. Now Investigate what is different Process in production compare to sandbox that cause test class which fails to run.
Example: If you are inserting data in test class, so you unable to insert data in due to valditiaon executed to true or loookup filter. Because that process are available in production and not available in sandbox. In this case test class will fails in production.
All Answers
you have pasted your apex class as a test class, put correct test class.
The reason is your test class is fails to run successfully in production. Now Investigate what is different Process in production compare to sandbox that cause test class which fails to run.
Example: If you are inserting data in test class, so you unable to insert data in due to valditiaon executed to true or loookup filter. Because that process are available in production and not available in sandbox. In this case test class will fails in production.
Test Class
@isTest
public class lead_agent_trigger_test{
static testMethod void Agent_test_class() {
list<account> ListofAccount=new list<account>();
account a=new account(name='taha');
ListofAccount.add(a);
insert ListofAccount;
Contact con = new Contact( FirstName = 'Taha', LastName = 'Test', Email = 'abc@xyz.com', AccountId=ListofAccount[0].id);
insert con;
Show__c sh = new Show__c ( Name = 'Business', Business_Line__c = 'exibition' );
insert sh;
Edition__c edt = new Edition__c( Name = 'Business 2017', Show__c = sh.id);
insert edt;
Agents__c agent = new Agents__c( Agent_Name__c = con.id, Edition__c = edt.id, Country__c = 'Brazil', Active__c = true);
insert agent;
Test.startTest();
System.debug('>>>>>>>>> Starting Test');
Lead ld = new Lead( LastName = 'Agent', Company = 'TestCompany', Country = 'Brazil', Edition__c = edt.id,Sector_show__c = null,
Sectors__c = null,Date_sent_to_Agent__c = date.Today(),LeadSource='website');
//ListofLeads.add(ld);
insert ld;
agent = [select Id,Agent_Name__c,Agent_Name__r.Email from Agents__c where Country__c like : ld.Country and Active__c = True and Edition__c =: ld.Edition__c limit 1];
//System.assertEquals(agent.Agent_Name__c,ld.Agent__c);
//System.assertEquals(agent.Agent_Name__r.Email,ld.agent_email__c);
con = [select AccountId,Email from Contact where Id =: agent.Agent_Name__c limit 1];
//System.assertEquals(con.AccountId,ld.Agent_Account__c);
//System.assertEquals(agent.Id,ld.Agents__c);
Test.stopTest();
}
static testMethod void Agent_test_class1() {
list<account> ListofAccount=new list<account>();
account a=new account(name='taha');
ListofAccount.add(a);
insert ListofAccount;
Contact con = new Contact( FirstName = 'Test', LastName = 'Test', Email = 'abc@xyz.com', AccountId=ListofAccount[0].id);
insert con;
Show__c sh = new Show__c ( Name = 'Business', Business_Line__c = 'exibition' );
insert sh;
Edition__c edt = new Edition__c( Name = 'Business 2017', Show__c = sh.id);
insert edt;
Agents__c agent = new Agents__c( Agent_Name__c = con.id, Edition__c = edt.id, Country__c = 'Brazil', Active__c = true,Sector_Show__c = 'INDEX', Sectors__c = 'Eco');
insert agent;
Test.startTest();
System.debug('>>>>>>>>> Starting Test');
Lead ld = new Lead( LastName = 'Agent', Company = 'TestCompany', Country = 'Brazil', Edition__c = edt.id,Sector_show__c = 'INDEX',
Sectors__c = 'Eco',Date_sent_to_Agent__c = date.Today(),LeadSource='website');
insert ld;
agent = [select Id,Agent_Name__c,Agent_Name__r.Email from Agents__c where Country__c like : ld.Country and Active__c = True and Edition__c =: ld.Edition__c limit 1];
//System.assertEquals(agent.Agent_Name__c,ld.Agent__c);
//System.assertEquals(agent.Agent_Name__r.Email,ld.agent_email__c);
con = [select AccountId,Email from Contact where Id =: agent.Agent_Name__c limit 1];
//System.assertEquals(con.AccountId,ld.Agent_Account__c);
//System.assertEquals(agent.Id,ld.Agents__c);
Test.stopTest();
}
}
Thanks Amol for your reply
I'll check and get back to you if still facing any issue.
Thanks, Amol
One of the workflows was inactive.