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
Greg RohmanGreg Rohman 

Seemingly simple get/set problem

Hello.

 

On a page, I have an inputtext field called "giftItemCost". In the custom controller, I'm referencing that value directly in a SOQL query.

 

When I use the following, it works fine:

 

 

public decimal giftItemCost {get; set;}

 

However, in order to do unit testing (and thus assign a value to the inputtext during testing), I need an explicit getter and setter. So, when I switch it to the following, it doesn't work:

 

 

public decimal giftItemCost;
public decimal getGiftItemCost() { return giftItemCost; }
public void setGiftItemCost(Decimal d) { giftItemCost = d; }

Not only does it not work, but the button on the VF page stops working entirely. Oddly, my VF page has a series of other inputtext fields, and all of them are working properly using the latter explicit get/set version of the code.

 

Any explanation would be very appreciated. Thank you.

 

-Greg

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
osamanosaman

You don't have to have explicit getter setter. You can still call the get/set method by using

 

ClassName.VariableName = AssignedValue

 

Can you send your test method  here?

All Answers

osamanosaman

You don't have to have explicit getter setter. You can still call the get/set method by using

 

ClassName.VariableName = AssignedValue

 

Can you send your test method  here?

This was selected as the best answer
Greg RohmanGreg Rohman

Hi Osama, and thanks for the reply.

 

Thanks for the tip regarding assigning values without explicitly calling get/set. I've modified my test method, and everything works perfectly. I have included the complete test method below, as requested, and I'm still not sure why writing it as an explicit get/set didn't work.

 

 

    public static testMethod void testBatch() {
Account testAccount = new Account(name='testAccount');
insert testAccount;
Contact testContact = new Contact(FirstName='First', LastName='Last');
insert testContact;
Lead testLead = new Lead(Company='Test Account', FirstName='John', LastName='Last', Status='Open', Phone='555-123-4567');
insert testLead;
Campaign testCampaign = new Campaign(Name='Test Campaign',isActive=true);
insert testCampaign;

CampaignMember cm1 = new CampaignMember(CampaignId=testCampaign.Id,ContactId=testContact.Id,Status='Sent');
CampaignMember cm2 = new CampaignMember(CampaignId=testCampaign.Id,LeadId=testLead.Id,Status='Sent');
insert cm1;
insert cm2;

PageReference acctPage = new PageReference('/apex/ConvertCampaignToGift');
Test.setCurrentPage(acctPage);

CtrlConvertCampaignToGift Ctrl = new CtrlConvertCampaignToGift();

// Set testing values
Ctrl.selectedCampaignId = string.valueof(testCampaign.Id);
Ctrl.selectedGiftType = 'Sent';
Ctrl.giftName = 'Test Gift';
Ctrl.giftDate = '2010-01-10';
Ctrl.giftItemCost = 20;
Ctrl.giftQuantity = 1;

// Execute all methods
Ctrl.doCampaignLookup();
Ctrl.getCampaigns();
Ctrl.getCampaignMemberList();
Ctrl.getCampaignStatuses();
Ctrl.doAssignGifts();
Ctrl.getGiftTypes();
Ctrl.getSelectedCampaignName();
Ctrl.getCampaignMemberStatusList();
Ctrl.getGiftCount();
Ctrl.getGiftsAdded();
Ctrl.getMemberCount();
Ctrl.getAddedGifts();
}

 -Greg