-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
4Questions
-
1Replies
Need help with test class suffering from deficit code coverage
My test class achieved 73% code coverage. Please help me to get the required code coverage.
Class :
Class :
public class QuoteFieldHelpers
{
public static boolean isExecuting=false;
public static void updateValues( Quote q)
{
if(QuoteFieldHelpers.isExecuting)
{
return;
}
QuoteFieldHelpers.isExecuting=true;
Quote[] quotes=new Quote[]{};
Quote qu=new Quote(Id =q.Id);
QuoteLineItem qle;
try
{
qle=[select GRAND_TOTAL__c from QuoteLineItem where QuoteId =:q.id Limit 1];
}
catch(Exception e)
{
System.debug('Error at query result'+e.getMessage());
}
if(qle != null)
{
try
{
System.debug('grant total from QLI : '+qle.GRAND_TOTAL__c);
qu.TOTAL_AOS_Including_Taxes__c=qle.GRAND_TOTAL__c;
quotes.add(qu);
}
catch(Exception e)
{
System.debug('Error during Grand total fetch'+e.getMessage());
}
}
try
{
update quotes;
}
catch(Exception e)
{
System.debug('Error during field update quote : '+e.getMessage());
}
}
}
Test Class :
@isTest
public class QuoteControlTestClass
{
public static Quote qu;
public static Opportunity op;
public static QuoteLineItem qle;
public static testMethod void method1()
{
List<User> salesExec=[select Id from user];
Opportunity opp=new Opportunity();
opp.Name='Cheky Voola';
opp.Account=new Account(id='0010l00000EhKdL');
opp.CurrencyIsoCode='INR';
opp.CloseDate=Date.newInstance(2018, 1, 10);
opp.StageName='Qualification';
opp.Rating__c='Hot';
opp.Preferred_Project__c='Test Project';
opp.Sales_Executive__c=salesExec.get(1).Id;
opp.Amenities__c='Pool and Badminton';
opp.Certifications__c='No certifications';
opp.Brand__c='Under Armour';
opp.Preferred_Location__c='Kavali';
opp.Budget__c='70 Lakhs - 75 Lakhs';
insert opp;
Id pricebookId = Test.getStandardPricebookId();
Opportunity o=[select Id,Name from Opportunity where id=:opp.Id];
Product2 pr=new Product2();
pr.Name='Test Product';
pr.IsActive=True;
pr.CurrencyIsoCode='INR';
pr.Floor__c='9';
pr.Total_Charges_including_Amenities_del__c=12345;
pr.GST_Service_Tax__c=6;
insert pr;
Product2 prod=[select Name,IsActive,CurrencyIsoCode,Floor__c,Total_Amount_with_Agreement_of_Sale__c from Product2 where Id=:pr.Id];
PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = prod.Id,UnitPrice = 10000, IsActive = true);
insert standardPrice;
Quote q=new Quote(Name='Trent',OpportunityId=o.Id,Pricebook2Id=pricebookId);
insert q;
qle=new QuoteLineItem();
qle.QuoteId=q.Id;
qle.Product2Id=prod.Id;
qle.UnitPrice=12345;
qle.Quantity=1;
qle.PricebookEntryId=standardPrice.Id;
insert qle;
q.TOTAL_AOS_Including_Taxes__c=qle.GRAND_TOTAL__c;
upsert q;
QuoteLineItem qle2=[select QuoteId,GRAND_TOTAL__c from QuoteLineItem where product2Id=:prod.Id ];
Quote quot=[select TOTAL_AOS_Including_Taxes__c from quote where id=:qle2.QuoteId ];
System.debug('TOTAL_AOS_Including_Taxes__c from quote : '+quot.TOTAL_AOS_Including_Taxes__c);
System.debug('TOTAL_AOS_Including_Taxes__c : '+quot.TOTAL_AOS_Including_Taxes__c);
//System.debug('Total_Amount_with_Agreement_of_Sale__c : '+prod.Total_Amount_with_Agreement_of_Sale__c);
//op=[select Name from Opportunity limit 1];
//qle=[select QuoteId,GRAND_TOTAL__c from QuoteLineItem where product2Id=:prod.Id limit 1];
//Quote quot=[select TOTAL_AOS_Including_Taxes__c from quote where id=:qle.QuoteId limit 1];
//System.debug('TOTAL_AOS_Including_Taxes__c from quote : '+quot.TOTAL_AOS_Including_Taxes__c);
//QuoteFieldHelpers.updateValues(q);
}
}
-
- John Carton 6
- January 16, 2018
- Like
- 0
Overcome Null Pointer Exception
String sv1;
String sv2;
String sv3;
String sv4;
for(Opportunity o:[Select CampaignId,Site_Visit_1__c,Site_Visit_2__c,Site_Visit_3__c,Site_Visit_4__c From Opportunity Where CampaignId=:keysForOpp])
{
sv1=o.Site_Visit_1__c;
sv2=o.Site_Visit_2__c;
sv3=o.Site_Visit_3__c;
sv4=o.Site_Visit_4__c;
}
If the custom fields are not filled in the records and I try to access them then I am encountering null pointer exception. Is there a way to validate the fields before accessing them? I tried placing those fields inside try-catch block but it gets clumpsy since I need to write try-catch for every individual field. Is there an alternate way?-
- John Carton 6
- January 15, 2018
- Like
- 0
code optimization needed
Hello Folks. I have four custom pick up fields in Opportunity. Every opportunity has a look up relationship with Campaign. My trigger will loop through all the opportunity records and if any of the custom pick lists is saved with a value 'Completed ' it updates the count in Campaign's custom field. I coded a trigger an it is working perfect. I need help in optimizing this code cause I feel like I have used too many try-catch blocks and also SOQL statements inside for loops. Thanks in Advance.
trigger Site_visit_update_in_campaign on Opportunity (after insert,after
update,after delete)
{
public Boolean s1NullCheck=false;
public Boolean s2NullCheck=false;
public Boolean s3NullCheck=false;
Public Boolean s4NullCheck=false;
List <Campaign> campaigns=[select id from campaign];
Set<Id> keysForOpp=new Set<Id>();
Map<Id,List<Opportunity>> oppList=new Map<Id,List<Opportunity>>();
List<Id> filteredIds=new List<Id>();
Campaign[] campaign=new Campaign[]{};
Map<Id,Campaign> campaignToUpdate=new Map<Id,Campaign>([Select Id,Completed_Site_Visit_1__c,Completed_Site_Visit_2__c,Completed_Site_Visit_3__c,Completed_Site_Visit_4__c From campaign]);
for(Opportunity oppor:Trigger.new)
{
for(Campaign c:campaigns)
{
keysForOpp.add(c.Id);
}
for(Opportunity o:[Select
CampaignId,Site_Visit_1__c,Site_Visit_2__c,Site_Visit_3__c,Site_Visit_4__c From Opportunity Where CampaignId=:keysForOpp])
{
try
{
List<Opportunity> temp=oppList.get(o.CampaignId);
if(temp==null)
{
oppList.put(o.CampaignId, new List<Opportunity>{o});
filteredIds.add(o.CampaignId);
}
else
{
temp.add(o);
}
}
catch(NullPointerException e)
{
System.debug('Exception at custom Map setting :
'+e.getMessage());
}
}
System.debug('opportunity debug'+opplist);
for(Id i:filteredIds)
{
Integer siteVisitOneCounter=0;
Integer siteVisitTwoCounter=0;
Integer siteVisitThreeCounter=0;
Integer siteVisitFourCounter=0;
for(Opportunity o:oppList.get(i))
{
System.debug('Opportunity after calling key : '+o);
try
{
if(o.Site_Visit_1__c !=null)
{
s1NullCheck=true;
System.debug('S1check : '+s1NullCheck);
}
}
catch(NullPointerException e)
{
System.debug('Exeeption at siteVisitOne : '+e.getMessage());
System.debug('S1check : '+s1NullCheck);
}
try
{
if(o.Site_Visit_2__c !=null)
{
s2NullCheck=true;
System.debug('S2check : '+s2NullCheck);
}
}
catch(NullPointerException e)
{
System.debug('Exeeption at siteVisitOne : '+e.getMessage());
System.debug('S2check : '+s2NullCheck);
}
try
{
if(o.Site_Visit_3__c !=null)
{
s3NullCheck=true;
System.debug('S3check : '+s3NullCheck);
}
}
catch(NullPointerException e)
{
System.debug('Exeeption at siteVisitOne : '+e.getMessage());
System.debug('S13heck : '+s3NullCheck);
}
try
{
if(o.Site_Visit_4__c !=null)
{
s4NullCheck=true;
System.debug('S4check : '+s4NullCheck);
}
}
catch(NullPointerException e)
{
System.debug('Exeeption at siteVisitOne : '+e.getMessage());
System.debug('S4check : '+s4NullCheck);
}
if(s1NullCheck==true)
{
if(o.Site_Visit_1__c=='Completed')
{
System.debug('Opportunity : '+o.Site_Visit_1__c);
siteVisitOneCounter=siteVisitOneCounter+1;
}
s1NullCheck=false;
}
if(s2NullCheck==true)
{
if(o.Site_Visit_2__c=='Completed')
{
System.debug('Opportunity : '+o.Site_Visit_2__c);
siteVisitTwoCounter=siteVisitTwoCounter+1;
}
s2NullCheck=false;
}
if(s3NullCheck==true)
{
if(o.Site_Visit_3__c=='Completed')
{
System.debug('Opportunity : '+o.Site_Visit_3__c);
siteVisitThreeCounter=siteVisitThreeCounter+1;
}
s3NullCheck=false;
}
if(s4NullCheck==true)
{
if(o.Site_Visit_4__c=='Completed')
{
System.debug('Opportunity : '+o.Site_Visit_4__c);
siteVisitFourCounter=siteVisitFourCounter+1;
}
s4NullCheck=false;
}
}
System.debug('Site Visit One Total : '+siteVisitOneCounter);
System.debug('Site Visit two Total : '+siteVisitTwoCounter);
System.debug('Site Visit three Total : '+siteVisitThreeCounter);
System.debug('Site Visit four Total : '+siteVisitFourCounter);
Campaign cam=campaignToUpdate.get(i);
cam.Completed_Site_Visit_1__c=siteVisitOneCounter;
cam.Completed_Site_Visit_2__c=siteVisitTwoCounter;
cam.Completed_Site_Visit_3__c=siteVisitThreeCounter;
cam.Completed_Site_Visit_4__c=siteVisitFourCounter;
campaign.add(cam);
try
{
update campaign;
System.debug('Campaign update Success');
}
catch(Exception e)
{
System.debug('Failed to update campaig : '+e.getMessage());
}
}
}
}
-
- John Carton 6
- January 15, 2018
- Like
- 0
Help with Trigger Test Class
I am unable to get the required code coverage for this trigger.Please Help me out.
Here is my Test class
trigger Site_visit_update_in_campaign on Opportunity (after insert,after
update,after delete)
{
public Boolean s1NullCheck=false;
public Boolean s2NullCheck=false;
public Boolean s3NullCheck=false;
Public Boolean s4NullCheck=false;
List <Campaign> campaigns=[select id from campaign];
Set<Id> keysForOpp=new Set<Id>();
Map<Id,List<Opportunity>> oppList=new Map<Id,List<Opportunity>>();
List<Id> filteredIds=new List<Id>();
Campaign[] campaign=new Campaign[]{};
Map<Id,Campaign> campaignToUpdate=new Map<Id,Campaign>([Select Id,Completed_Site_Visit_1__c,Completed_Site_Visit_2__c,Completed_Site_Visit_3__c,Completed_Site_Visit_4__c From campaign]);
for(Opportunity oppor:Trigger.new)
{
for(Campaign c:campaigns)
{
keysForOpp.add(c.Id);
}
for(Opportunity o:[Select
CampaignId,Site_Visit_1__c,Site_Visit_2__c,Site_Visit_3__c,Site_Visit_4__c From Opportunity Where CampaignId=:keysForOpp])
{
try
{
List<Opportunity> temp=oppList.get(o.CampaignId);
if(temp==null)
{
oppList.put(o.CampaignId, new List<Opportunity>{o});
filteredIds.add(o.CampaignId);
}
else
{
temp.add(o);
}
}
catch(NullPointerException e)
{
System.debug('Exception at custom Map setting :
'+e.getMessage());
}
}
System.debug('opportunity debug'+opplist);
for(Id i:filteredIds)
{
Integer siteVisitOneCounter=0;
Integer siteVisitTwoCounter=0;
Integer siteVisitThreeCounter=0;
Integer siteVisitFourCounter=0;
for(Opportunity o:oppList.get(i))
{
System.debug('Opportunity after calling key : '+o);
try
{
if(o.Site_Visit_1__c !=null)
{
s1NullCheck=true;
System.debug('S1check : '+s1NullCheck);
}
}
catch(NullPointerException e)
{
System.debug('Exeeption at siteVisitOne : '+e.getMessage());
System.debug('S1check : '+s1NullCheck);
}
try
{
if(o.Site_Visit_2__c !=null)
{
s2NullCheck=true;
System.debug('S2check : '+s2NullCheck);
}
}
catch(NullPointerException e)
{
System.debug('Exeeption at siteVisitOne : '+e.getMessage());
System.debug('S2check : '+s2NullCheck);
}
try
{
if(o.Site_Visit_3__c !=null)
{
s3NullCheck=true;
System.debug('S3check : '+s3NullCheck);
}
}
catch(NullPointerException e)
{
System.debug('Exeeption at siteVisitOne : '+e.getMessage());
System.debug('S13heck : '+s3NullCheck);
}
try
{
if(o.Site_Visit_4__c !=null)
{
s4NullCheck=true;
System.debug('S4check : '+s4NullCheck);
}
}
catch(NullPointerException e)
{
System.debug('Exeeption at siteVisitOne : '+e.getMessage());
System.debug('S4check : '+s4NullCheck);
}
if(s1NullCheck==true)
{
if(o.Site_Visit_1__c=='Completed')
{
System.debug('Opportunity : '+o.Site_Visit_1__c);
siteVisitOneCounter=siteVisitOneCounter+1;
}
s1NullCheck=false;
}
if(s2NullCheck==true)
{
if(o.Site_Visit_2__c=='Completed')
{
System.debug('Opportunity : '+o.Site_Visit_2__c);
siteVisitTwoCounter=siteVisitTwoCounter+1;
}
s2NullCheck=false;
}
if(s3NullCheck==true)
{
if(o.Site_Visit_3__c=='Completed')
{
System.debug('Opportunity : '+o.Site_Visit_3__c);
siteVisitThreeCounter=siteVisitThreeCounter+1;
}
s3NullCheck=false;
}
if(s4NullCheck==true)
{
if(o.Site_Visit_4__c=='Completed')
{
System.debug('Opportunity : '+o.Site_Visit_4__c);
siteVisitFourCounter=siteVisitFourCounter+1;
}
s4NullCheck=false;
}
}
System.debug('Site Visit One Total : '+siteVisitOneCounter);
System.debug('Site Visit two Total : '+siteVisitTwoCounter);
System.debug('Site Visit three Total : '+siteVisitThreeCounter);
System.debug('Site Visit four Total : '+siteVisitFourCounter);
Campaign cam=campaignToUpdate.get(i);
cam.Completed_Site_Visit_1__c=siteVisitOneCounter;
cam.Completed_Site_Visit_2__c=siteVisitTwoCounter;
cam.Completed_Site_Visit_3__c=siteVisitThreeCounter;
cam.Completed_Site_Visit_4__c=siteVisitFourCounter;
campaign.add(cam);
try
{
update campaign;
System.debug('Campaign update Success');
}
catch(Exception e)
{
System.debug('Failed to update campaig : '+e.getMessage());
}
}
}
}
Here is my Test class
@isTest
public class OpportunityTriggerTest
{
public static testMethod void campaignUpdate()
{
List<User> salesExec=[select Id from user];
Campaign campaign=new Campaign(Name='chekxx campaign',CurrencyIsoCode='INR');
Opportunity opp=new Opportunity();
opp.Name='Cheky Voola';
opp.Account=new Account(id='0010l00000EhKdL');
opp.CurrencyIsoCode='INR';
opp.CloseDate=Date.newInstance(2018, 1, 10);
opp.StageName='Qualification';
opp.Rating__c='Hot';
opp.Preferred_Project__c='Test Project';
opp.Sales_Executive__c=salesExec.get(1).Id;
opp.Amenities__c='Pool and Badminton';
opp.Certifications__c='No certifications';
opp.Brand__c='Under Armour';
opp.Preferred_Location__c='Kavali';
opp.Budget__c='70 Lakhs - 75 Lakhs';
opp.Site_Visit_1__c='Completed';
opp.CampaignId='7010l0000009iV8';
System.debug('Campaigns in method 2 : '+campaign);
try
{
insert opp;
}
catch(Exception e)
{
System.debug('Exception during insert in method 2 : '+e.getMessage());
}
opp.Site_visit_2__c='Completed';
try
{
upsert opp;
}
catch(Exception e)
{
System.debug('Error during upsert in method 2 : '+e.getMessage());
}
}
}
-
- John Carton 6
- January 12, 2018
- Like
- 0
Help with Trigger Test Class
I am unable to get the required code coverage for this trigger.Please Help me out.
Here is my Test class
trigger Site_visit_update_in_campaign on Opportunity (after insert,after
update,after delete)
{
public Boolean s1NullCheck=false;
public Boolean s2NullCheck=false;
public Boolean s3NullCheck=false;
Public Boolean s4NullCheck=false;
List <Campaign> campaigns=[select id from campaign];
Set<Id> keysForOpp=new Set<Id>();
Map<Id,List<Opportunity>> oppList=new Map<Id,List<Opportunity>>();
List<Id> filteredIds=new List<Id>();
Campaign[] campaign=new Campaign[]{};
Map<Id,Campaign> campaignToUpdate=new Map<Id,Campaign>([Select Id,Completed_Site_Visit_1__c,Completed_Site_Visit_2__c,Completed_Site_Visit_3__c,Completed_Site_Visit_4__c From campaign]);
for(Opportunity oppor:Trigger.new)
{
for(Campaign c:campaigns)
{
keysForOpp.add(c.Id);
}
for(Opportunity o:[Select
CampaignId,Site_Visit_1__c,Site_Visit_2__c,Site_Visit_3__c,Site_Visit_4__c From Opportunity Where CampaignId=:keysForOpp])
{
try
{
List<Opportunity> temp=oppList.get(o.CampaignId);
if(temp==null)
{
oppList.put(o.CampaignId, new List<Opportunity>{o});
filteredIds.add(o.CampaignId);
}
else
{
temp.add(o);
}
}
catch(NullPointerException e)
{
System.debug('Exception at custom Map setting :
'+e.getMessage());
}
}
System.debug('opportunity debug'+opplist);
for(Id i:filteredIds)
{
Integer siteVisitOneCounter=0;
Integer siteVisitTwoCounter=0;
Integer siteVisitThreeCounter=0;
Integer siteVisitFourCounter=0;
for(Opportunity o:oppList.get(i))
{
System.debug('Opportunity after calling key : '+o);
try
{
if(o.Site_Visit_1__c !=null)
{
s1NullCheck=true;
System.debug('S1check : '+s1NullCheck);
}
}
catch(NullPointerException e)
{
System.debug('Exeeption at siteVisitOne : '+e.getMessage());
System.debug('S1check : '+s1NullCheck);
}
try
{
if(o.Site_Visit_2__c !=null)
{
s2NullCheck=true;
System.debug('S2check : '+s2NullCheck);
}
}
catch(NullPointerException e)
{
System.debug('Exeeption at siteVisitOne : '+e.getMessage());
System.debug('S2check : '+s2NullCheck);
}
try
{
if(o.Site_Visit_3__c !=null)
{
s3NullCheck=true;
System.debug('S3check : '+s3NullCheck);
}
}
catch(NullPointerException e)
{
System.debug('Exeeption at siteVisitOne : '+e.getMessage());
System.debug('S13heck : '+s3NullCheck);
}
try
{
if(o.Site_Visit_4__c !=null)
{
s4NullCheck=true;
System.debug('S4check : '+s4NullCheck);
}
}
catch(NullPointerException e)
{
System.debug('Exeeption at siteVisitOne : '+e.getMessage());
System.debug('S4check : '+s4NullCheck);
}
if(s1NullCheck==true)
{
if(o.Site_Visit_1__c=='Completed')
{
System.debug('Opportunity : '+o.Site_Visit_1__c);
siteVisitOneCounter=siteVisitOneCounter+1;
}
s1NullCheck=false;
}
if(s2NullCheck==true)
{
if(o.Site_Visit_2__c=='Completed')
{
System.debug('Opportunity : '+o.Site_Visit_2__c);
siteVisitTwoCounter=siteVisitTwoCounter+1;
}
s2NullCheck=false;
}
if(s3NullCheck==true)
{
if(o.Site_Visit_3__c=='Completed')
{
System.debug('Opportunity : '+o.Site_Visit_3__c);
siteVisitThreeCounter=siteVisitThreeCounter+1;
}
s3NullCheck=false;
}
if(s4NullCheck==true)
{
if(o.Site_Visit_4__c=='Completed')
{
System.debug('Opportunity : '+o.Site_Visit_4__c);
siteVisitFourCounter=siteVisitFourCounter+1;
}
s4NullCheck=false;
}
}
System.debug('Site Visit One Total : '+siteVisitOneCounter);
System.debug('Site Visit two Total : '+siteVisitTwoCounter);
System.debug('Site Visit three Total : '+siteVisitThreeCounter);
System.debug('Site Visit four Total : '+siteVisitFourCounter);
Campaign cam=campaignToUpdate.get(i);
cam.Completed_Site_Visit_1__c=siteVisitOneCounter;
cam.Completed_Site_Visit_2__c=siteVisitTwoCounter;
cam.Completed_Site_Visit_3__c=siteVisitThreeCounter;
cam.Completed_Site_Visit_4__c=siteVisitFourCounter;
campaign.add(cam);
try
{
update campaign;
System.debug('Campaign update Success');
}
catch(Exception e)
{
System.debug('Failed to update campaig : '+e.getMessage());
}
}
}
}
Here is my Test class
@isTest
public class OpportunityTriggerTest
{
public static testMethod void campaignUpdate()
{
List<User> salesExec=[select Id from user];
Campaign campaign=new Campaign(Name='chekxx campaign',CurrencyIsoCode='INR');
Opportunity opp=new Opportunity();
opp.Name='Cheky Voola';
opp.Account=new Account(id='0010l00000EhKdL');
opp.CurrencyIsoCode='INR';
opp.CloseDate=Date.newInstance(2018, 1, 10);
opp.StageName='Qualification';
opp.Rating__c='Hot';
opp.Preferred_Project__c='Test Project';
opp.Sales_Executive__c=salesExec.get(1).Id;
opp.Amenities__c='Pool and Badminton';
opp.Certifications__c='No certifications';
opp.Brand__c='Under Armour';
opp.Preferred_Location__c='Kavali';
opp.Budget__c='70 Lakhs - 75 Lakhs';
opp.Site_Visit_1__c='Completed';
opp.CampaignId='7010l0000009iV8';
System.debug('Campaigns in method 2 : '+campaign);
try
{
insert opp;
}
catch(Exception e)
{
System.debug('Exception during insert in method 2 : '+e.getMessage());
}
opp.Site_visit_2__c='Completed';
try
{
upsert opp;
}
catch(Exception e)
{
System.debug('Error during upsert in method 2 : '+e.getMessage());
}
}
}

- John Carton 6
- January 12, 2018
- Like
- 0