2

I have a button on Account layout that pulls up a VF page that is a sort of builder which allows the user to update contact info, account info, and make a new contract, with its sole purpose being a "contract renewal" form.

In an attempt to get and use account info from the current record from which the button was selected, I have this as a sort of constructor...

    public AccountRenewal(ApexPages.StandardController controller){

    // which fields are being pulled from current record
    controller.addFields(new String[] {
        'Id', 'Name', 'Phone', 'Fax', 'Website', 'Parent', 'ParentID', 'Name', 'Best_Day_Time__c','Site_Related_Message__c', 'ShippingStreet',
            'BillingState', 'BillingCity', 'BillingCountry', 'BillingStreet' 
            });
    // new account -- set to current record
    siteItem = new Account();
    siteItem = (Account)controller.getRecord();
    setupObjects();

}

I'm not entirely sure what that parameter is, how it is used, exactly, or how to work with it in making the Test class. How should I start? So far in my Test Class, I have account items, contacts, contact roles, contracts, etc...so I've got the basic components set that I'll need, it's just a matter of figuring out exactly how to deal with this controller setup.

  • Ah the one remaining valid use case for Test.isRunningTest. You cannot call addFields in a test. – Adrian Larson Aug 12 '16 at 14:34
  • What error. And please do not continue to add new questions to your question. For future reference when you have a new questions, ask a new one. When you put the error up I will respond. Its most likely the one about not being able to add fields during tests – Eric Aug 12 '16 at 14:35
  • Oops! Sorry, meant to paste in the exact error and got ahead of myself. System.SObjectException: You cannot call addFields when the data is being passed into the controller by the caller. – Natalie Spatharakis Aug 12 '16 at 14:36
  • Yeah, he resolved your initial question, you should ask a new one for that or search for it, there are many answers out there already. It does bear mention in the answer though, since your initial code snippet contains the addFields call. – Adrian Larson Aug 12 '16 at 14:37
  • Updated answer. Thanks for the detailed question, its nice to see. Just keep in mind this is not a discussion forum and to maintain the ability to find relevant answers question should not be ever expanding. – Eric Aug 12 '16 at 14:39
3

the public AccountRenewal(ApexPages.StandardController controller){ is the constructor for the class and the parameter is a standardController class provided by Salesforce for standard/custom object to provide standard save, update, etc functions.

So in your test you just need to instantiate it and pass it in.

Docs here: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_pages_standardcontroller.htm

You would do it like this:

Account acct = new Account(Name='test');
//Insert acct if needed
APexPages.StandardController con = New ApexPages.standardController ( acct );
[your class] myController = [your class].AccountRenewal(con);

Then you can continue with the rest of your test class

Since you are in a test context you will have issues around the .addFields method call.

You will have to wrap it in

if(!test.isRunningTest()){
controller.addFields(new String[] {
        'Id', 'Name', 'Phone', 'Fax', 'Website', 'Parent', 'ParentID', 'Name', 'Best_Day_Time__c','Site_Related_Message__c', 'ShippingStreet',
            'BillingState', 'BillingCity', 'BillingCountry', 'BillingStreet' 
            });
}

but then you need to contend with those fields not being available so you can either:

  1. Query for then and restructure your controller to store them.
  2. Ensure you have those fields in the record you use to construct the StandardController
  3. Add each of those fields in either an input hidden our an outputtext with rendered = false to the page and remove the addFields method completely (Still need to do #2)
| improve this answer | |
  • Better to just use real words like record for your variable names then. :) – Adrian Larson Aug 12 '16 at 14:33
  • @NatalieSpatharakis - See updated answer – Eric Aug 12 '16 at 14:42
  • option (3) is to put those fields on the VF page but referenced only in apex:variable or apex:inputHidden so they come into the controller implicitly with the getRecord() call – cropredy Aug 12 '16 at 19:41
  • @cropredy - I do not believe that will work during a test method which is why I did not add it. – Eric Aug 12 '16 at 19:43
  • 1
    right -- but OP can avoid the problem altogether by not using addFields() and just ensure mocked sobject in testmethod has the fields needed by the controller's getRecord() – cropredy Aug 12 '16 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.