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
nbeekmannbeekman 

Method must define a body

I have an issue and cannot find any good help or explanation of why I am getting this error ' below is the code calling another class to create Temp SObjects.    Or is there a better way to call a class method to create temp objects?  I am new to Apex programming and any comments appreciated.

 

Error message

 

"Method must define a body" on CommissionTestSuite.cls

 

 
Code calling the code at the end of this message.
 
/**
 * This class contains unit tests for validating the behavior of Commission feature
 * of Calculate Pricing trigger 
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
public with sharing class CommissionTestSuite {

// ********************** ERROR IS ON THE LINE BELOW ************************************
// ********************** METHOD MUST DEFINE A BODY ************************************

	Create_Objects_Helper.CreateBaseForTesting();

	static list<Opportunity> opp;
	static
	{
		opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
		if(opp.isEmpty())
			throw new MyException('No Opportunities found.');
	}
	
	
		
	// This static method calculates the Commission for the VP when the Payment model is Upfront
	static testMethod void Calculate_VP_Commission_Upfront() {
		
        //get the selected Opportunity
        opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
        Opportunity temp_opp = opp.get(0);
        temp_opp.SW_Pay_MD__c = 'Upfront';
        update temp_opp;
    }
    
    // This static method calculates the Commission for the VP when the Payment model is Monthly
	static testMethod void Calculate_VP_Commission_Monthly() {
		
		opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
        //get the selected Opportunity
        Opportunity temp_opp = opp.get(0);
        temp_opp.SW_Pay_MD__c = 'Monthly';
        update temp_opp;
    }
    
    // This static method calculates the Commission for the VP when the Payment model is Monthly
	static testMethod void Calculate_DIR_Commission_Monthly() {
		list<User> user = [select Id, Name from User where Name = 'Platform Integration' limit 1];
		User temp_user = user.get(0);
		opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
		temp_user.Title = 'Sales VP';
		update temp_user;
        //get the selected Opportunity
        Opportunity temp_opp = opp.get(0);
        temp_opp.OwnerId = user.get(0).Id;
        temp_opp.SW_Pay_MD__c = 'Monthly';
        Commision.Cal_Up_Commision_Dir('36 months', 544);
        Commision.Cal_MO_Commision_Dir(1000,'36 months',100);
        update temp_opp;
    }
    
    // This static method calculates the Commission for the VP when the Payment model is Monthly
	static testMethod void Calculate_DIR_Commission_Upfront() {
		list<User> user = [select Id, Name from User where Name = 'Platform Integration' limit 1];
		opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
		 User temp_user = user.get(0);
		 temp_user.Title = 'Sales VP';
		 update temp_user;
        //get the selected Opportunity
        Opportunity temp_opp = opp.get(0);
        temp_opp.SW_Pay_MD__c = 'Upfront';
        update temp_opp;
    }
    
    static testmethod void Calculate_Commission_12_Months()
    {
    	opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
    	//get the Opp and set the Term to 12 months
    	Opportunity temp_opp = opp.get(0);
    	temp_opp.Term_1__c = '12 months';
    	update temp_opp;
    }
    
    static testmethod void Calculate_Commission_24_Months()
    {
    	opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
    	//get the Opp and set the Term to 12 months
    	Opportunity temp_opp = opp.get(0);
    	temp_opp.Term_1__c = '24 months';
    	update temp_opp;
    }
    
    static testmethod void Calculate_Commission_60_Months()
    {
    	opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
    	//get the Opp and set the Term to 12 months
    	Opportunity temp_opp = opp.get(0);
    	temp_opp.Term_1__c = '60 months';
    	update temp_opp;
    }
    

}

 

 
I am trying to call this class.
 
//	Helper class to create Objects required for testing.
//
//		Account			'Envysion Test Account'
//		Opportunity 	'Envysion Test Opportunity'
//		Location	 	'Envysion Test Location' First Name = 'Envysion Test' Last Name = 'Location'
//		Bill To			'Envysion Test Bill To' First Name = 'Envysion Test' Last Name = 'Bill To'
//		Contact 		'Envysion Test Spokesperson' First Name = 'Envysion Test' Last Name = 'Spokesperson'
//		Site 			'Envysion Test Site'
//		Add Parts		'Test 25\' Cable'
//						'Test EnVR'
//						'Test Insight Software'
//						'Test Labor'
//
//	Created by: Norman Beekman 6/1/2013

public with sharing class Create_Objects_Helper {

	public void CreateBaseForTesting() {
        //	Delete any found Opportunity
		list<Opportunity> OpportunityList = new list<Opportunity>();
		OpportunityList = [Select Id, Sales_Status__c from Opportunity where Name = 'Envysion Test for Deployment'];
		
		if (!OpportunityList.isEmpty()) {
			system.debug('******************* Deleting any Opportunity - Envysion Test for Deployment *******************');
			//	Make sure we can delete the Opportunity
    		for (Opportunity op: OpportunityList) {
				if(op.Sales_Status__c != '3 - Committed'){
					op.Sales_Status__c = '3 - Committed';
				}
    		}
    		update OpportunityList;
			delete OpportunityList;
			OpportunityList.clear();
		}

        //	Delete any found Account
		list<Account> AccountList = new list<Account>();
		AccountList = [Select Id from Account where Name = 'Envysion, Inc Test'];
		
		if (!AccountList.isEmpty()) {
			system.debug('******************* Deleting any Account - Envysion, Inc *******************');
			Delete AccountList;
			AccountList.clear();
		}

        //	Delete any found Contact	Select r.Id, r.Name From RecordType r Where r.SobjectType = 'Contact'
		list<Contact> ContactList = new list<Contact>();
		ContactList = [Select Id from Contact where Name = 'Envysion Test Location' or Name = 'Envysion Test Bill To' 
			or Name = 'Envysion Test Spokesperson'];
		
		if (!ContactList.isEmpty()) {
			system.debug('******************* Deleting any Contact - Envysion, Inc *******************');
			delete ContactList;
			ContactList.clear();
		}

        //	Create a fresh Account
		if (AccountList.isEmpty()) {
			system.debug('******************* Creating Account - Envysion, Inc *******************');
			Account NewAccount = new Account(
        		Name = 'Envysion, Inc Test', BillingStreet  = '400 Centennial Pkwy, Suite 201', BillingCity = 'Louisville',
        		BillingState = 'Colorado', BillingPostalCode = '80027', BillingCountry = 'United States',
        		Phone = '303-590-2357'); 
			AccountList.add(NewAccount);
			if (!AccountList.isEmpty()) {
				insert AccountList;
			} 
		} 

        //	Create a fresh Contact
		system.debug('******************* CREATING Contacts - Envysion Test Bill To, Location, Spokesperson *******************');
		list<RecordType> RecordTypeList = new list<RecordType>();
		RecordTypeList = [Select r.Id, r.Name From RecordType r Where r.SobjectType = 'Contact'];
		Id BillToRecordID, SoldToRecordId, PersonRecordID;
    	for (RecordType rt: RecordTypeList) {
			if(rt.Name == 'Bill To'){
				BillToRecordID = rt.Id;
				system.debug('>>>>>>>>>>>>>>>>>>>> bill to ' + rt.Id);
			}
			if(rt.Name == 'Location (Sold To)'){
				SoldToRecordId = rt.Id;
				system.debug('>>>>>>>>>>>>>>>>>>>> Sold to ' + rt.Id);
			}
			if(rt.Name == 'Person'){
				PersonRecordID = rt.Id;
				system.debug('>>>>>>>>>>>>>>>>>>>> Person ' + rt.Id);
			}
    	}

		ContactList.add(new Contact(
			RecordTypeId = BillToRecordID, 
			FirstName = 'Envysion Test', LastName = 'Bill to',
			Account = AccountList.get(0), MailingStreet = '400 Centennial Pkwy Suite 201',
			MailingCity = 'Louisville', MailingState = 'Colorado', MailingPostalCode = '80027',
			MailingCountry = 'United States', Phone = '303-590-2357', Email = 'nbeekman@envysion.com')); 
		ContactList.add(new Contact(
			RecordTypeId =  SoldToRecordId, 
			FirstName = 'Envysion Test', LastName = 'Location',
			Account = AccountList.get(0), MailingStreet = '400 Centennial Pkwy Suite 201',
			MailingCity = 'Louisville', MailingState = 'Colorado', MailingPostalCode = '80027',
			MailingCountry = 'United States', Phone = '303-590-2357', Email = 'nbeekman@envysion.com')); 
		ContactList.add(new Contact(
			RecordTypeId = PersonRecordID, 
			FirstName = 'Envysion Test', LastName = 'Spokesperson',
			Account = AccountList.get(0), MailingStreet = '400 Centennial Pkwy Suite 201',
			MailingCity = 'Louisville', MailingState = 'Colorado', MailingPostalCode = '80027',
			MailingCountry = 'United States', Phone = '303-590-2357', Email = 'nbeekman@envysion.com')); 

		if (!ContactList.isEmpty()) {
			insert ContactList;
		}

        //	Create a fresh Opportunity and set its Properties
		//	NOTE: BillToID = ContactList.get(0), SoldToId= ContactList.get(1), PersonID= ContactList.get(2);
		system.debug('******************* CREATING Opportunity - Envysion Test for Deployment *******************');
		list<Pricebook2> PriceBooklist = [select Id, Name from Pricebook2 PB where PB.Name = 'Zuora Standard Pricebook march 2012'];
        Opportunity NewOpportunity = new Opportunity(
        	Name = 'Envysion Test for Deployment', Type = 'Initial Sale', AccountId = AccountList.get(0).Id, 
			CloseDate = System.today(), StageName = 'Closed Won', Sales_Status__c = '3 - Committed', 
			SW_Discount__c = 0.20, HW_Discount__c = 0.30, Inst_Discount__c = 0.20,
			Sold_To_Contact__c = ContactList.get(1).Id, Bill_To_Contact__c = ContactList.get(0).Id,
			BrandsonSite__c = 'Wendy', Multiplier__c = 1, of_EnVRs__c = 1, Sales_Path__c = 'Standard Order',
			Software_Cost__c = 0.0, Install_Cost__c = 0.0, Equipment_Cost__c = 0.0, Pricebook2Id = PriceBooklist.get(0).Id, 
			Extended_Warranty__c = 1, Term__c = 36, Term_1__c = '36 months', Lic_Fee_U__c = 0.0,
			HW_Pay_MD__c = 'Upfront', Inst_Pay_MD__c = 'Upfront', SW_Pay_MD__c = 'Monthly', TestOpp__c = false); 

		//Pbooklist.get(0).Id,
        
		OpportunityList.add(NewOpportunity);
			
		if (!OpportunityList.isEmpty() && !AccountList.isEmpty()) {
			insert OpportunityList;
			//	OpportunityList.get(0).Id)
		}
		
		//	Insert Product into new Opportunity
		list<OpportunityLineItem> OpportunityLineItemList = new list<OpportunityLineItem>();
		list<product2> Product2List = [Select p.Id From Product2 p Where p.Name = 'Test 25\' Cable' Limit 1];
		list<PricebookEntry> PricebookEntryList = [Select p.Id From PricebookEntry p 
			Where p.Pricebook2Id = :PriceBooklist.get(0).Id and p.Product2Id = :Product2List.get(0).Id];
		OpportunityLineItemList.add(new OpportunityLineItem(
			Unit__c = 'Each',
			Service_Type__c = 'Not Applicable',
			SalesDescript__c = 'Test 25\' Cable',
			HasEnVR__c = False,
			EnVRinSub__c = 'False',
			Component_Name__c = 'Test 25\' Cable',
			BOM__c = 'None',
			PricebookEntryId = PricebookEntryList.get(0).Id,
			OpportunityId = OpportunityList.get(0).Id,
			Quantity = 1,
			UnitPrice = 24.925,
			SKU__c = 'TEST-01'
			)); 
		Product2List = [Select p.Id From Product2 p Where p.Name = 'Test EnVR'];
		PricebookEntryList = [Select p.Id From PricebookEntry p 
			Where p.Pricebook2Id = :PriceBooklist.get(0).Id and p.Product2Id = :Product2List.get(0).Id];
		OpportunityLineItemList.add(new OpportunityLineItem(
			Unit__c = 'Each',
			Service_Type__c = 'Video',
			SalesDescript__c = 'Test EnVR',
			HasEnVR__c = true,
			EnVRinSub__c = 'False',
			Component_Name__c = 'Test EnVR',
			BOM__c = 'None',
			PricebookEntryId = PricebookEntryList.get(0).Id,
			OpportunityId = OpportunityList.get(0).Id,
			Quantity = 1,
			UnitPrice = 53.89,
			SKU__c = 'TEST-03'
			)); 
		Product2List = [Select p.Id From Product2 p Where p.Name = 'Test Insight Software'];
		PricebookEntryList = [Select p.Id From PricebookEntry p 
			Where p.Pricebook2Id = :PriceBooklist.get(0).Id and p.Product2Id = :Product2List.get(0).Id];
		OpportunityLineItemList.add(new OpportunityLineItem(
			Unit__c = 'Each',
			Service_Type__c = 'Insight',
			SalesDescript__c = 'Test Insight Software',
			HasEnVR__c = False,
			EnVRinSub__c = 'False',
			Component_Name__c = 'Test Insight Software',
			BOM__c = 'None',
			PricebookEntryId = PricebookEntryList.get(0).Id,
			OpportunityId = OpportunityList.get(0).Id,
			Quantity = 1,
			UnitPrice = 53.89,
			SKU__c = 'TEST-04'
			)); 
		Product2List = [Select p.Id From Product2 p Where p.Name = 'Test Labor'];
		PricebookEntryList = [Select p.Id From PricebookEntry p 
			Where p.Pricebook2Id = :PriceBooklist.get(0).Id and p.Product2Id = :Product2List.get(0).Id];
		OpportunityLineItemList.add(new OpportunityLineItem(
			Unit__c = 'Each',
			Service_Type__c = 'Not Applicable',
			SalesDescript__c = 'Test Labor',
			HasEnVR__c = False,
			EnVRinSub__c = 'False',
			Component_Name__c = 'Test Labor',
			BOM__c = 'None',
			PricebookEntryId = PricebookEntryList.get(0).Id,
			OpportunityId = OpportunityList.get(0).Id,
			Quantity = 10,
			UnitPrice = 124.75,
			SKU__c = 'TEST-02'
			)); 

		if (!OpportunityLineItemList.isEmpty()) {
			insert OpportunityLineItemList;
		}

		
	}	// End CreateBaseForTesting

}	// End Class

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Change the following:

 

	Create_Objects_Helper.CreateBaseForTesting();

	static list<Opportunity> opp;
	static
	{
		opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
		if(opp.isEmpty())
			throw new MyException('No Opportunities found.');
	}

 As follows:

 

	static list<Opportunity> opp;
	static
	{
		Create_Objects_Helper.CreateBaseForTesting();
		opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
		if(opp.isEmpty())
			throw new MyException('No Opportunities found.');
	}

Rationale: You can't call code (except for variable initializers, such as integer x = 5;) outside of a code block. static { } is a code block that you can use to initialize your data as soon as the class is loaded.

 

 Next, change:

 

public with sharing class Create_Objects_Helper {

	public void CreateBaseForTesting() {

 As follows:

 

public with sharing class Create_Objects_Helper {

	public static void CreateBaseForTesting() {

Rationale: Your code appears to assume it was meant to be called statically anyways, so marking the function as static removes the need to instantiate the class (the coh variable, as mentioned above).

All Answers

sfdcfoxsfdcfox
You should probably just copy and paste your code. Your images are 404, so we can't see what you're talking about.
nbeekmannbeekman

I updated the message to include the code.

Avidev9Avidev9

You have to call a method from inside a function or constructor.

Create_Objects_Helper.CreateBaseForTesting();

You just placed the above line the class body. Try to call the same from constructor or from another function

 

 

 

nbeekmannbeekman

Please explain how I can do it there.  Is the constructor like this:

 

Create_Objects_Helper coh = new Create_Objects_Helper();

coh.CreateBaseForTesting();

 

If so, I get the same error on coh.CreateBaseForTesting();

 

Thanks

Avidev9Avidev9

Define a constructor inside you class "CommissionTEstSuite".

 

public CommissionTestSuite() {
Create_Objects_Helper coh = new Create_Objects_Helper();
coh.CreateBaseForTesting();
}

 

 

nbeekmannbeekman

I did that and it still has the same error on coh.CreateBaseForTesting();

 

See code insert.

 

/**
 * This class contains unit tests for validating the behavior of Commission feature
 * of Calculate Pricing trigger 
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
public with sharing class CommissionTestSuite {

// ********************** ERROR IS ON THE LINE BELOW ************************************
// ********************** METHOD MUST DEFINE A BODY ************************************

	Create_Objects_Helper coh = new Create_Objects_Helper();
	coh.CreateBaseForTesting();
	
	static list<Opportunity> opp;
	static
	{
		opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
		if(opp.isEmpty())
			throw new MyException('No Opportunities found.');
	}
	
	
		
	// This static method calculates the Commission for the VP when the Payment model is Upfront
	static testMethod void Calculate_VP_Commission_Upfront() {
		
        //get the selected Opportunity
        opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
        Opportunity temp_opp = opp.get(0);
        temp_opp.SW_Pay_MD__c = 'Upfront';
        update temp_opp;
    }
    
    // This static method calculates the Commission for the VP when the Payment model is Monthly
	static testMethod void Calculate_VP_Commission_Monthly() {
		
		opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
        //get the selected Opportunity
        Opportunity temp_opp = opp.get(0);
        temp_opp.SW_Pay_MD__c = 'Monthly';
        update temp_opp;
    }
    
    // This static method calculates the Commission for the VP when the Payment model is Monthly
	static testMethod void Calculate_DIR_Commission_Monthly() {
		list<User> user = [select Id, Name from User where Name = 'Platform Integration' limit 1];
		User temp_user = user.get(0);
		opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
		temp_user.Title = 'Sales VP';
		update temp_user;
        //get the selected Opportunity
        Opportunity temp_opp = opp.get(0);
        temp_opp.OwnerId = user.get(0).Id;
        temp_opp.SW_Pay_MD__c = 'Monthly';
        Commision.Cal_Up_Commision_Dir('36 months', 544);
        Commision.Cal_MO_Commision_Dir(1000,'36 months',100);
        update temp_opp;
    }
    
    // This static method calculates the Commission for the VP when the Payment model is Monthly
	static testMethod void Calculate_DIR_Commission_Upfront() {
		list<User> user = [select Id, Name from User where Name = 'Platform Integration' limit 1];
		opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
		 User temp_user = user.get(0);
		 temp_user.Title = 'Sales VP';
		 update temp_user;
        //get the selected Opportunity
        Opportunity temp_opp = opp.get(0);
        temp_opp.SW_Pay_MD__c = 'Upfront';
        update temp_opp;
    }
    
    static testmethod void Calculate_Commission_12_Months()
    {
    	opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
    	//get the Opp and set the Term to 12 months
    	Opportunity temp_opp = opp.get(0);
    	temp_opp.Term_1__c = '12 months';
    	update temp_opp;
    }
    
    static testmethod void Calculate_Commission_24_Months()
    {
    	opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
    	//get the Opp and set the Term to 12 months
    	Opportunity temp_opp = opp.get(0);
    	temp_opp.Term_1__c = '24 months';
    	update temp_opp;
    }
    
    static testmethod void Calculate_Commission_60_Months()
    {
    	opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
    	//get the Opp and set the Term to 12 months
    	Opportunity temp_opp = opp.get(0);
    	temp_opp.Term_1__c = '60 months';
    	update temp_opp;
    }
    

}

 

Avidev9Avidev9

I dont see a change in your code!!!!!

 

public with sharing class CommissionTestSuite {

    // ********************** ERROR IS ON THE LINE BELOW ************************************
    // ********************** METHOD MUST DEFINE A BODY ************************************

    //Create_Objects_Helper coh = new Create_Objects_Helper(); <=======Comment this
    //coh.CreateBaseForTesting(); <==========Comment this 
    
    //added this
    public CommissionTestSuite() {
        Create_Objects_Helper coh = new Create_Objects_Helper();
        coh.CreateBaseForTesting();
        opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
		if(opp.isEmpty())
			throw new MyException('No Opportunities found.');

    }

    static list < Opportunity > opp;
    



    // This static method calculates the Commission for the VP when the Payment model is Upfront
    static testMethod void Calculate_VP_Commission_Upfront() {

        //get the selected Opportunity
        opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity'
            limit 1
        ];
        Opportunity temp_opp = opp.get(0);
        temp_opp.SW_Pay_MD__c = 'Upfront';
        update temp_opp;
    }

    // This static method calculates the Commission for the VP when the Payment model is Monthly
    static testMethod void Calculate_VP_Commission_Monthly() {

        opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity'
            limit 1
        ];
        //get the selected Opportunity
        Opportunity temp_opp = opp.get(0);
        temp_opp.SW_Pay_MD__c = 'Monthly';
        update temp_opp;
    }

    // This static method calculates the Commission for the VP when the Payment model is Monthly
    static testMethod void Calculate_DIR_Commission_Monthly() {
        list < User > user = [select Id, Name from User where Name = 'Platform Integration'
            limit 1
        ];
        User temp_user = user.get(0);
        opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity'
            limit 1
        ];
        temp_user.Title = 'Sales VP';
        update temp_user;
        //get the selected Opportunity
        Opportunity temp_opp = opp.get(0);
        temp_opp.OwnerId = user.get(0).Id;
        temp_opp.SW_Pay_MD__c = 'Monthly';
        Commision.Cal_Up_Commision_Dir('36 months', 544);
        Commision.Cal_MO_Commision_Dir(1000, '36 months', 100);
        update temp_opp;
    }

    // This static method calculates the Commission for the VP when the Payment model is Monthly
    static testMethod void Calculate_DIR_Commission_Upfront() {
        list < User > user = [select Id, Name from User where Name = 'Platform Integration'
            limit 1
        ];
        opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity'
            limit 1
        ];
        User temp_user = user.get(0);
        temp_user.Title = 'Sales VP';
        update temp_user;
        //get the selected Opportunity
        Opportunity temp_opp = opp.get(0);
        temp_opp.SW_Pay_MD__c = 'Upfront';
        update temp_opp;
    }

    static testmethod void Calculate_Commission_12_Months() {
        opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity'
            limit 1
        ];
        //get the Opp and set the Term to 12 months
        Opportunity temp_opp = opp.get(0);
        temp_opp.Term_1__c = '12 months';
        update temp_opp;
    }

    static testmethod void Calculate_Commission_24_Months() {
        opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity'
            limit 1
        ];
        //get the Opp and set the Term to 12 months
        Opportunity temp_opp = opp.get(0);
        temp_opp.Term_1__c = '24 months';
        update temp_opp;
    }

    static testmethod void Calculate_Commission_60_Months() {
        opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity'
            limit 1
        ];
        //get the Opp and set the Term to 12 months
        Opportunity temp_opp = opp.get(0);
        temp_opp.Term_1__c = '60 months';
        update temp_opp;
    }


}

 

nbeekmannbeekman

Ok, I will try that and post the results.  Thanks so much for your patience.

 

:)

sfdcfoxsfdcfox

Change the following:

 

	Create_Objects_Helper.CreateBaseForTesting();

	static list<Opportunity> opp;
	static
	{
		opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
		if(opp.isEmpty())
			throw new MyException('No Opportunities found.');
	}

 As follows:

 

	static list<Opportunity> opp;
	static
	{
		Create_Objects_Helper.CreateBaseForTesting();
		opp = [select Name, Id, SW_Pay_MD__c from Opportunity where Name = 'Envysion Test Opportunity' limit 1];
		if(opp.isEmpty())
			throw new MyException('No Opportunities found.');
	}

Rationale: You can't call code (except for variable initializers, such as integer x = 5;) outside of a code block. static { } is a code block that you can use to initialize your data as soon as the class is loaded.

 

 Next, change:

 

public with sharing class Create_Objects_Helper {

	public void CreateBaseForTesting() {

 As follows:

 

public with sharing class Create_Objects_Helper {

	public static void CreateBaseForTesting() {

Rationale: Your code appears to assume it was meant to be called statically anyways, so marking the function as static removes the need to instantiate the class (the coh variable, as mentioned above).

This was selected as the best answer
nbeekmannbeekman

nbeekmannbeekman

Worked perfect and didnt tneed to instantiate the class (the coh variable).

 

Thanks for you help.