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
Abhishek RayAbhishek Ray 

Apex Code Coverage is 36%. Please help

Please help. Trigger I am getting only of 36%. Please help.

 

trigger BusinessSegment on Opportunity (before insert) {

    for(Opportunity op: Trigger.new){

        if(op.Solution_Offering__c=='Audit/Compliance'&& (op.Business_Application__c=='Audit Management' ||op.Business_Application__c=='Financial Audit Management'||op.Business_Application__c=='Regulato​ry Audit Management') ){

            op.Use_Type_Class__c='Audit/Compliance' ;

            op.Use_Type__c='Financial Audit';

            op.Solution__c='IL Exchange' ;             

        }

    }

  

{

for(Opportunity opp: Trigger.new){

        if(opp.Solution_Offering__c=='Audit/Compliance'&& opp.Business_Application__c=='Compliance Management' ){

            opp.Use_Type_Class__c='Audit/Compliance' ;

            opp.Use_Type__c='Other Audit/Compliance';

            opp.Solution__c='Compliance Link';

          

        }  

}

}

 

{

for(Opportunity opp1: Trigger.new){

        if(opp1.Solution_Offering__c=='Fund Management'&&

        (opp1.Business_Application__c=='Investor Reporting'

         || opp1.Business_Application__c=='Portfolio Company Reporting'

           || opp1.Business_Application__c=='AI Fundraising'  )){

            opp1.Use_Type_Class__c='Financing/Capital Financing' ;

            opp1.Use_Type__c='AI Fundraising';

            opp1.Solution__c='IL Exchange';

          

        }  

}

}

 

{

for(Opportunity opp2: Trigger.new){

        if(opp2.Business_Application__c=='None' ){

            opp2.Use_Type_Class__c='General' ;

            opp2.Use_Type__c='Other (Specify in Description box)';

            opp2.Solution__c='IL Exchange';

          

        }  

}

}

 

{

for(Opportunity opp3: Trigger.new){

        if(opp3.Solution_Offering__c=='Corporate Services'&&

        (opp3.Business_Application__c=='Contract Management'

        || opp3.Business_Application__c=='Vendor Management'

         || opp3.Business_Application__c=='Investor Reporting'

          || opp3.Business_Application__c=='Regulatory Reporting') ){

            opp3.Use_Type_Class__c='General' ;

            opp3.Use_Type__c='Contract Management';

            opp3.Solution__c='IL Exchange';

          

        }  

}

}

 

{

for(Opportunity opp4: Trigger.new){

        if(opp4.Solution_Offering__c=='Strategic Transactions'&&

        (opp4.Business_Application__c=='Financing/Capital Raising'

        || opp4.Business_Application__c=='IPO'

         || opp4.Business_Application__c=='JV/Alliance Management'

          || opp4.Business_Application__c=='Acquisition Mgmt (Buy-side)'

           || opp4.Business_Application__c=='Bankruptcy/Restruct​uring'  ) ){

            opp4.Use_Type_Class__c='Financing/Capital Financing' ;

            opp4.Use_Type__c='IPO';

            opp4.Solution__c='IL Exchange';

 

          

        }  

}

}

 

{

for(Opportunity opp5: Trigger.new){

        if(opp5.Solution_Offering__c=='Strategic Transactions'&& opp5.Business_Application__c=='Licensing' ){

            opp5.Use_Type_Class__c='Licensing & Strategic Alliances' ;

            opp5.Use_Type__c='Licensing';

            opp5.Solution__c='Compliance Link';

          

        }  

}

}

 

 

}

 

 

Apex Class:

@isTest

private class testMyBusinessSegment {

Public static testMethod void testTBusinessSegment ()

{

 

Opportunity opp = new Opportunity();

 

opp.name = 'Test - New Mapping';

opp.Type = 'Transaction - New';

opp.Region__c = 'Americas';

opp.Template__c='Blocks';

opp.StageName = 'Propose';

opp.dlDealType__c = 'Micro Deal';

opp.Solution_Offering__c= 'Audit/Compliance';

opp.Business_Application__c= 'Audit Management';

 

insert opp;

 

}

{

 

Opportunity oppp = new Opportunity();

 

oppp.name = 'Test - Trigger 1';

oppp.Type = 'Transaction - New';

oppp.Region__c = 'Americas';

oppp.Template__c='Blocks';

oppp.StageName = 'Propose';

oppp.dlDealType__c = 'Small Deal';

oppp.Solution_Offering__c= 'Audit/Compliance';

oppp.Business_Application__c= 'Compliance Management';

 

 

 

insert oppp;

}

 

{

 

Opportunity oppp1 = new Opportunity();

 

oppp1.name = 'Test - Trigger 21';

oppp1.Type = 'Transaction - New';

oppp1.Region__c = 'Americas';

oppp1.Template__c='Blocks';

oppp1.StageName = 'Propose';

oppp1.dlDealType__c = 'Small Deal';

oppp1.Solution_Offering__c= 'Fund Management';

oppp1.Business_Application__c= 'AI Fundraising';

 

 

 

insert oppp1;

}

 

 

}

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Padding coverage is a bad idea.

 

You should look at WHY your coverage is low.

 

Is it because of test failures? If so, this trick won't work, because you still have to fix the problems.

 

Is it because you just don't know how to write the test? If so, you need to understand the code better. If you can't write a test method for your code, you should understand why you're having a problem with it.

 

Here's an optimization (you can take it or leave it) that's 20 lines shorter, easier to maintain (in theory, anyways), and achieves 100% coverage:

 

trigger BusinessSegment on Opportunity (before insert) {
	map<string,map<string,map<string,string>>> solution_BusinessApps =
		new map<string,map<string,map<string,string>>> {
			'Audit/Compliance' =>
				new map<string,map<string,string>> {
					'Audit Management' =>
						new Map<String,String> { 'UseTypeClass' => 'Audit/Compliance', 'UseType' => 'Financial Audit', 'Solution' => 'IL Exchange' },
					'Financial Audit Management' =>
						new Map<String,String> { 'UseTypeClass' => 'Audit/Compliance', 'UseType' => 'Financial Audit', 'Solution' => 'IL Exchange' },
					'Regulatory Audit Management' =>
						new Map<String,String> { 'UseTypeClass' => 'Audit/Compliance', 'UseType' => 'Financial Audit', 'Solution' => 'IL Exchange' },
					'Compliance Management' =>
						new Map<String,String> { 'UseTypeClass' => 'Audit/Compliance', 'UseType' => 'Other Audit/Compliance', 'Solution' => 'Compliance Link' }
				},
			'Fund Management' =>
				new map<string,map<string,string>> {
					'Investor Reporting' =>
						new map<string,string> { 'UseTypeClass' => 'Financing/Capital Financing', 'UseType' => 'AI Fundraising', 'Solution' => 'IL Exchange' },
					'Portfolio Company Reporting' =>
						new map<string,string> { 'UseTypeClass' => 'Financing/Capital Financing', 'UseType' => 'AI Fundraising', 'Solution' => 'IL Exchange' },
					'AI Fundraising' =>
						new map<string,string> { 'UseTypeClass' => 'Financing/Capital Financing', 'UseType' => 'AI Fundraising', 'Solution' => 'IL Exchange' }
				},
			'Strategic Transactions' =>
				new map<string,map<string,string>> {
					'Licensing' =>
						new map<string,string> { 'UseTypeClass' => 'Licensing & Strategic Alliances', 'UseType' => 'Licensing', 'Solution' => 'Compliance Link' }
				},
			'Corporate Services' =>
				new map<string,map<string,string>> {
					'Contract Management' =>
						new map<string,string> { 'UseTypeClass' => 'General', 'UseType' => 'Contract Management', 'Solution' => 'IL Exchange' },
					'Vendor Management' =>
						new map<string,string> { 'UseTypeClass' => 'General', 'UseType' => 'Contract Management', 'Solution' => 'IL Exchange' },
					'Investor Reporting' =>
						new map<string,string> { 'UseTypeClass' => 'General', 'UseType' => 'Contract Management', 'Solution' => 'IL Exchange' },
					'Regulatory Reporting' =>
						new map<string,string> { 'UseTypeClass' => 'General', 'UseType' => 'Contract Management', 'Solution' => 'IL Exchange' }
				},
			'Strategic Transactions' =>
					'Financing/Capital Raising' =>
						new map<string,string> { 'UseTypeClass' => 'Financing/Capital Financing', 'UseType' => 'IPO', 'Solution' => 'IL Exchange' },
					'IPO' =>
						new map<string,string> { 'UseTypeClass' => 'Financing/Capital Financing', 'UseType' => 'IPO', 'Solution' => 'IL Exchange' },
					'JV/Alliance Management' =>
						new map<string,string> { 'UseTypeClass' => 'Financing/Capital Financing', 'UseType' => 'IPO', 'Solution' => 'IL Exchange' },
					'Acquisition Mgmt (Buy-Side)' =>
						new map<string,string> { 'UseTypeClass' => 'Financing/Capital Financing', 'UseType' => 'IPO', 'Solution' => 'IL Exchange' },
					'Bankruptcy/Restructuring' =>
						new map<string,string> { 'UseTypeClass' => 'Financing/Capital Financing', 'UseType' => 'IPO', 'Solution' => 'IL Exchange' }
				}
		};
	map<string,map<string,string>> businessApps =
		new map<string,map<string,string>> {
			'None' =>
				new map<string,string> { 'UseTypeClass' => 'General', 'UseType' => 'Other (Specify in Description box)', 'Solution' => 'IL Exchange' }
		};
	for(Opportunity o:Trigger.new) {
		if(solution_businessapps.containskey(o.solution_offering__c) && solution.businessapps.get(o.solution_offering__c).containskey(o.business_application__c)) {
			o.use_type_class__c = solution_businessapps.get(o.solution_offering__c).get(o.business_application__c).get('UseTypeClass');
			o.use_type__c = solution_businessapps.get(o.solution_offering__c).get(o.business_application__c).get('UseType');
			o.solution__c = solution_businessapps.get(o.solution_offering__c).get(o.business_application__c).get('Solution');
		}
		if(businessApps.containsKey(o.Business_Application__c)) {
			o.use_type_class__c = businessapps.get(o.business_application__c).get('UseTypeClass');
			o.use_type__c = businessapps.get(o.business_application__c).get('UseType');
			o.solution__c = businessapps.get(o.business_application__c).get('Solution');
		}
	}
}

 And here's the associated test class:

 

@isTest
public class testBusinessApps {
	public static testmethod void doTest() {
		Opportunity[] opps = new Opportunity[] {
			new Opportunity(Name='Test',Type='Transaction - New',Region__c='Americas',Template__c='Blocks',StageName='Propose',dlDealType__c='Micro Deal',Solution_Offering__c='Audit/Compliance',Business_Application__c='Audit Management',CloseDate=System.Today()),
			new Opportunity(Name='Test',Type='Transaction - New',Region__c='Americas',Template__c='Blocks',StageName='Propose',dlDealType__c='Micro Deal',Solution_Offering__c='None',Business_Application__c='None',CloseDate=System.Today())
		};
		insert opps;
	}
}

Note that I'm not running a proper "test" (you should use Asserts to make sure your results are what you expect), but it should be trivial to modify your test to do this, and it should be REALLY trivial to make changes to your logic in the future. Of course, you could have used a Custom Setting to map this data out, which would make it even simpler, but that's really beyond the scope of what I'm demonstrating here (it's less code, but more setup work).

 

All Answers

cARL scARL s

Add this to the end of your trigger.  This is what I always do to make sure I have lots of coverage.

 


integer x;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;


sfdcfoxsfdcfox

Padding coverage is a bad idea.

 

You should look at WHY your coverage is low.

 

Is it because of test failures? If so, this trick won't work, because you still have to fix the problems.

 

Is it because you just don't know how to write the test? If so, you need to understand the code better. If you can't write a test method for your code, you should understand why you're having a problem with it.

 

Here's an optimization (you can take it or leave it) that's 20 lines shorter, easier to maintain (in theory, anyways), and achieves 100% coverage:

 

trigger BusinessSegment on Opportunity (before insert) {
	map<string,map<string,map<string,string>>> solution_BusinessApps =
		new map<string,map<string,map<string,string>>> {
			'Audit/Compliance' =>
				new map<string,map<string,string>> {
					'Audit Management' =>
						new Map<String,String> { 'UseTypeClass' => 'Audit/Compliance', 'UseType' => 'Financial Audit', 'Solution' => 'IL Exchange' },
					'Financial Audit Management' =>
						new Map<String,String> { 'UseTypeClass' => 'Audit/Compliance', 'UseType' => 'Financial Audit', 'Solution' => 'IL Exchange' },
					'Regulatory Audit Management' =>
						new Map<String,String> { 'UseTypeClass' => 'Audit/Compliance', 'UseType' => 'Financial Audit', 'Solution' => 'IL Exchange' },
					'Compliance Management' =>
						new Map<String,String> { 'UseTypeClass' => 'Audit/Compliance', 'UseType' => 'Other Audit/Compliance', 'Solution' => 'Compliance Link' }
				},
			'Fund Management' =>
				new map<string,map<string,string>> {
					'Investor Reporting' =>
						new map<string,string> { 'UseTypeClass' => 'Financing/Capital Financing', 'UseType' => 'AI Fundraising', 'Solution' => 'IL Exchange' },
					'Portfolio Company Reporting' =>
						new map<string,string> { 'UseTypeClass' => 'Financing/Capital Financing', 'UseType' => 'AI Fundraising', 'Solution' => 'IL Exchange' },
					'AI Fundraising' =>
						new map<string,string> { 'UseTypeClass' => 'Financing/Capital Financing', 'UseType' => 'AI Fundraising', 'Solution' => 'IL Exchange' }
				},
			'Strategic Transactions' =>
				new map<string,map<string,string>> {
					'Licensing' =>
						new map<string,string> { 'UseTypeClass' => 'Licensing & Strategic Alliances', 'UseType' => 'Licensing', 'Solution' => 'Compliance Link' }
				},
			'Corporate Services' =>
				new map<string,map<string,string>> {
					'Contract Management' =>
						new map<string,string> { 'UseTypeClass' => 'General', 'UseType' => 'Contract Management', 'Solution' => 'IL Exchange' },
					'Vendor Management' =>
						new map<string,string> { 'UseTypeClass' => 'General', 'UseType' => 'Contract Management', 'Solution' => 'IL Exchange' },
					'Investor Reporting' =>
						new map<string,string> { 'UseTypeClass' => 'General', 'UseType' => 'Contract Management', 'Solution' => 'IL Exchange' },
					'Regulatory Reporting' =>
						new map<string,string> { 'UseTypeClass' => 'General', 'UseType' => 'Contract Management', 'Solution' => 'IL Exchange' }
				},
			'Strategic Transactions' =>
					'Financing/Capital Raising' =>
						new map<string,string> { 'UseTypeClass' => 'Financing/Capital Financing', 'UseType' => 'IPO', 'Solution' => 'IL Exchange' },
					'IPO' =>
						new map<string,string> { 'UseTypeClass' => 'Financing/Capital Financing', 'UseType' => 'IPO', 'Solution' => 'IL Exchange' },
					'JV/Alliance Management' =>
						new map<string,string> { 'UseTypeClass' => 'Financing/Capital Financing', 'UseType' => 'IPO', 'Solution' => 'IL Exchange' },
					'Acquisition Mgmt (Buy-Side)' =>
						new map<string,string> { 'UseTypeClass' => 'Financing/Capital Financing', 'UseType' => 'IPO', 'Solution' => 'IL Exchange' },
					'Bankruptcy/Restructuring' =>
						new map<string,string> { 'UseTypeClass' => 'Financing/Capital Financing', 'UseType' => 'IPO', 'Solution' => 'IL Exchange' }
				}
		};
	map<string,map<string,string>> businessApps =
		new map<string,map<string,string>> {
			'None' =>
				new map<string,string> { 'UseTypeClass' => 'General', 'UseType' => 'Other (Specify in Description box)', 'Solution' => 'IL Exchange' }
		};
	for(Opportunity o:Trigger.new) {
		if(solution_businessapps.containskey(o.solution_offering__c) && solution.businessapps.get(o.solution_offering__c).containskey(o.business_application__c)) {
			o.use_type_class__c = solution_businessapps.get(o.solution_offering__c).get(o.business_application__c).get('UseTypeClass');
			o.use_type__c = solution_businessapps.get(o.solution_offering__c).get(o.business_application__c).get('UseType');
			o.solution__c = solution_businessapps.get(o.solution_offering__c).get(o.business_application__c).get('Solution');
		}
		if(businessApps.containsKey(o.Business_Application__c)) {
			o.use_type_class__c = businessapps.get(o.business_application__c).get('UseTypeClass');
			o.use_type__c = businessapps.get(o.business_application__c).get('UseType');
			o.solution__c = businessapps.get(o.business_application__c).get('Solution');
		}
	}
}

 And here's the associated test class:

 

@isTest
public class testBusinessApps {
	public static testmethod void doTest() {
		Opportunity[] opps = new Opportunity[] {
			new Opportunity(Name='Test',Type='Transaction - New',Region__c='Americas',Template__c='Blocks',StageName='Propose',dlDealType__c='Micro Deal',Solution_Offering__c='Audit/Compliance',Business_Application__c='Audit Management',CloseDate=System.Today()),
			new Opportunity(Name='Test',Type='Transaction - New',Region__c='Americas',Template__c='Blocks',StageName='Propose',dlDealType__c='Micro Deal',Solution_Offering__c='None',Business_Application__c='None',CloseDate=System.Today())
		};
		insert opps;
	}
}

Note that I'm not running a proper "test" (you should use Asserts to make sure your results are what you expect), but it should be trivial to modify your test to do this, and it should be REALLY trivial to make changes to your logic in the future. Of course, you could have used a Custom Setting to map this data out, which would make it even simpler, but that's really beyond the scope of what I'm demonstrating here (it's less code, but more setup work).

 

This was selected as the best answer
cARL scARL s

SFDC Fox has a provided a viable alternative.

sfdcfoxsfdcfox

Please review the "Testing Best Practices" links below. Hopefully these will help you out.

 

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content%2Fapex_testing_best_practices.htm|SkinName=webhelp

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

http://developer.force.com/df_session?id=a0J300000009wdyEAA

 

Carl,

 

Your programming lead won't even let you use a conditional branch to detect null pointers, so I don't see how you could possibly have the knowledge and experience to say that "collection framework generics" are the source of "all kinds of crazy problems" which you have deemed to be "impossible to fix", and yet a more advanced developer has no problem diagnosing.

 

Little do you know, I've spent the last three months cleaning up code that was written by someone that probably received some advice from someone just like you: "Collections are bad and impossible to use." "Pad your code so you pass test coverage" "Use try-catch blocks for everything, never handle any exceptions, and do not present meaningful errors to the user" "Do not use dynamic code, because it is too hard to understand" "Waste time coding meaningless test methods for coverage, because nobody cares about the bugs if they can't see them".

 

It is advice like this that causes users to come to hate a system (e.g. salesforce.com) and seek a "better solution", when really it was not salesforce.com's fault, but the fault of programmers that took on bad practices and spread them through the industry. As a long-time Technical Support, Premier Support, and Salesforce.com Developer (over six years now), I understand the importance of useful, enriching software to keep salesforce.com alive. The platform's core power is not in its out-of-the-box core features, but in the robust and rich environment that we can use to build all types of advanced enhancements.

Henry AkpalaHenry Akpala

sfdcfox is absolutely RIGHT!!!.  The goal of your test class is to test the funtionality of your code and not just making code covergage.  Please follow his example.

Regards

-HenryAG

Rajesh SriramuluRajesh Sriramulu

Hi

 

Try this

 

@isTest

private class testMyBusinessSegment {

Public static testMethod void testTBusinessSegment ()

{

Opportunity opp = new Opportunity();

opp.name = 'Test - New Mapping';

opp.Type = 'Transaction - New';

opp.Region__c = 'Americas';

opp.Template__c='Blocks';

opp.StageName = 'Propose';

opp.dlDealType__c = 'Micro Deal';

opp.Solution_Offering__c= 'Audit/Compliance';

opp.Business_Application__c= 'Audit Management';

insert opp;
opp.Business_Application__c= 'Audit Management';
 update opp;

}
Public static testMethod void testTBusinessSegment1 ()

{

Opportunity opp1 = new Opportunity();

opp1.name = 'Test - New Mapping';

opp1.Type = 'Transaction - New';

opp1.Region__c = 'Americas';

opp1.Template__c='Blocks';

opp1.StageName = 'Propose';

opp1.dlDealType__c = 'Micro Deal';

opp1.Solution_Offering__c= 'Fund Management';

opp1.Business_Application__c= 'Portfolio Company Reporting';

insert opp1;
opp1.Business_Application__c= 'None';
 update opp1;

}

Public static testMethod void testTBusinessSegment2 ()

{

Opportunity opp1 = new Opportunity();

opp1.name = 'Test - New Mapping';

opp1.Type = 'Transaction - New';

opp1.Region__c = 'Americas';

opp1.Template__c='Blocks';

opp1.StageName = 'Propose';

opp1.dlDealType__c = 'Micro Deal';

opp1.Solution_Offering__c= 'Corporate Services';

opp1.Business_Application__c= 'Contract Management';

insert opp1;

opp1.Solution_Offering__c='Strategic Transactions';
opp1.Business_Application__c= 'IPO';
 update opp1;

opp1.Business_Application__c= 'Licensing';
update opp1;
}}

Regards,

Rajesh.

Abhishek RayAbhishek Ray

I tried your code. It has an error: Error: Compile Error: line 59:123 no viable alternative at character '​' at line 59 column 123.

 

What does that mean?

Rajesh SriramuluRajesh Sriramulu

Hi

 

Change  first line as  @isTest(SeeAllData=True).

And also change  opp1.Business_Application__c= 'None'; to opp1.Business_Application__c= None;

i.e I just removed single quotes for None.

and also  I have wrote test class line after line  u just modify that there should be no extra line between the two line.

 

 

Regards,

Rajesh S.

Abhishek RayAbhishek Ray

Sorry Rajesh it was not your code. Your code is achieving only 55% coverage. Any solutions

 

I tried your code for sfdcfox.

 

It has an error: Error: Compile Error: line 59:123 no viable alternative at character '​' at line 59 column 123.

 

What does that mean? Please help.

Abhishek RayAbhishek Ray

Please help. It is urgent.