You need to sign in to do that
Don't have an account?

Help needed with Controller Extension
I have a custom object which is a child to the Opportunity object. The custom object is viewed via visualforce page. Sometimes Users need to create payment records for the child object. This is achieved by calling a method on a controller extension. When pressed, the method will automatically create detail records using information in the custom object record.
I am trying to write a test class that will check the controller is working correctly and that the values copied over from the custom object are correct. I can't figure out how to call the method to create the payments. Can anyone help?
Here is the test class:
@isTest
public class sipExt_Test {
public static testMethod void testSIPExtController() {
// Create Opportunity from test utilities class
List<Opportunity> OppList = testUtilities.createTestOpps(1);
// Create the SIP
bonus_calculator__c bc1 = new bonus_calculator__c(opportunity__c = OppList[0].Id);
insert bc1;
PageReference ref = new PageReference('/apex/SIP2?id=' + bc1.Id);
Test.setCurrentPage(ref);
// Create SIP standard controller, pass it the SIP Record
ApexPages.StandardController controller = new ApexPages.StandardController(bc1);
// Pass the Controller to the Extension
sipExt stdController = new sipExt(controller);
stdController.createPayments();
// Query for Payments fields
List <Bonus_Payments__c> bPayList = [SELECT Id FROM Bonus_Payments__c WHERE Bonus_Record__c = :bc1.Id];
system.debug(bPayList[0].id);
}
}
but I think the problem here after looking a bit more into it is that you do not create any record because sa1_lookup__c is not set for the bonus you created in your test method and other saX_lookup__c fields as well
All Answers
can you share the controller extension code
Jerome
Hi Jerome, thanks for the reply, Sure thing:
// This class is used to initialise Payment record fields when created from SIP bonus records via the related list
public with sharing class sipExt {
private ApexPages.StandardController standardController;
public sipExt(ApexPages.StandardController standardController)
{
this.standardController = standardController;
}
public PageReference CreatePayments()
{
Id recordId = standardController.getId();
bonus_calculator__C bonusRecord = (bonus_calculator__C) standardController.getRecord();
list<Bonus_Payments__c> PAYList = new List <Bonus_Payments__c>();
integer paymentTotal = 0;
//Create Payment SA1 P1
Bonus_Payments__c PAY1 = new Bonus_Payments__c();
PAY1.Bonus_Record__c = bonusRecord.Id;
PAY1.Sales_Associate__c = bonusRecord.sa1_lookup__c;
PAY1.Date_Payment_Due__c = bonusRecord.Date_Payment_1_Due__c;
PAY1.amount__c = bonusRecord.SA1_P1_Amount__c;
PAY1.adjusted_amount__c = bonusRecord.SA1_P1_Adj__c;
PAY1.Division__c = bonusRecord.SA1_division__c;
PAY1.Region__c = bonusRecord.SA1_region__c;
PAY1.Payment__c = '1';
PAY1.Opportunity_Record__c = bonusRecord.opportunity__c;
PAY1.Type__c = 'SIP';
PAY1.currencyISOcode = 'EUR';
// Only create payment record if a sales associate exists
if (PAY1.Sales_Associate__c != null)
{
PAYList.add(PAY1);
paymentTotal = paymentTotal + 1;
}
//Create Payment SA2 P1
Bonus_Payments__c PAY2 = new Bonus_Payments__c();
PAY2.Bonus_Record__c = bonusRecord.Id;
PAY2.Sales_Associate__c = bonusRecord.sa2_lookup__c;
PAY2.Date_Payment_Due__c = bonusRecord.Date_Payment_1_Due__c;
PAY2.amount__c = bonusRecord.SA2_P1_Amount__c;
PAY2.adjusted_amount__c = bonusRecord.SA2_P1_Adj__c;
PAY2.Division__c = bonusRecord.SA2_division__c;
PAY2.Region__c = bonusRecord.SA2_region__c;
PAY2.Payment__c = '1';
PAY2.Opportunity_Record__c = bonusRecord.opportunity__c;
PAY2.Type__c = 'SIP';
PAY2.currencyISOcode = 'EUR';
// Only create payment record if a sales associate exists
if (PAY2.Sales_Associate__c != null)
{
PAYList.add(PAY2);
paymentTotal = paymentTotal + 1;
}
//Create Payment SA1 P2
Bonus_Payments__c PAY3 = new Bonus_Payments__c();
PAY3.Bonus_Record__c = bonusRecord.Id;
PAY3.Sales_Associate__c = bonusRecord.sa1_lookup__c;
PAY3.Date_Payment_Due__c = bonusRecord.Date_Payment_2_Due__c;
PAY3.amount__c = bonusRecord.SA1_P2_Amount__c;
PAY3.adjusted_amount__c = bonusRecord.SA1_P2_Adj__c;
PAY3.Division__c = bonusRecord.SA1_division__c;
PAY3.Region__c = bonusRecord.SA1_region__c;
PAY3.Payment__c = '2';
PAY3.Opportunity_Record__c = bonusRecord.opportunity__c;
PAY3.Type__c = 'SIP';
PAY3.currencyISOcode = 'EUR';
// Only create payment record if a sales associate exists
if (PAY3.Sales_Associate__c != null)
{
PAYList.add(PAY3);
paymentTotal = paymentTotal + 1;
}
//Create Payment SA2 P2
Bonus_Payments__c PAY4 = new Bonus_Payments__c();
PAY4.Bonus_Record__c = bonusRecord.Id;
PAY4.Sales_Associate__c = bonusRecord.sa2_lookup__c;
PAY4.Date_Payment_Due__c = bonusRecord.Date_Payment_2_Due__c;
PAY4.amount__c = bonusRecord.SA2_Amount_P2__c;
PAY4.adjusted_amount__c = bonusRecord.SA2_P2_Adj__c;
PAY4.Division__c = bonusRecord.SA2_division__c;
PAY4.Region__c = bonusRecord.SA2_region__c;
PAY4.Payment__c = '2';
PAY4.Opportunity_Record__c = bonusRecord.opportunity__c;
PAY4.Type__c = 'SIP';
PAY4.currencyISOcode = 'EUR';
// Only create payment record if a sales associate exists
if (PAY4.Sales_Associate__c != null)
{
PAYList.add(PAY4);
paymentTotal = paymentTotal + 1;
}
// Update Payments Created Checkbox if List has records
if (PAYList.size() >= 2 && bonusRecord.Payments_Created__c == false)
{
BonusRecord.Payments_Created__c = true;
BonusRecord.NoOfPayments__c = paymentTotal;
Update BonusRecord;
insert PAYList;
}
return null;
}
}
what you should do is :
put the following line out of your method (as a public variable) in your extension :
list<Bonus_Payments__c> PAYList = new List <Bonus_Payments__c>();
then in your test method, you check the values of that list instead of querying the records.
Jerome
but I think the problem here after looking a bit more into it is that you do not create any record because sa1_lookup__c is not set for the bonus you created in your test method and other saX_lookup__c fields as well
You were right!
I added a tonne of debug statements and then went through the User workflow via the UI. I noticed that the saX_lookup__c were populated as exptected. When I ran the test via the apex test they were null. So I explicitly added the sa1_lookup__c from the test utilities class and ran it again, worked straight away. I wonder if this has something to do with the fact that the fields are updated via formula field maybe.
Anyway thanks again. Have a great weekend.