You need to sign in to do that
Don't have an account?
Extension Testing Error in VF
Hi All,
I'm trying to write my first test class for a an extension to a controller.
The class is simple - update the account information listed on the account form that's related to the contract object. I'm having trouble figuring out how to pass an updated value from the form over into the account object within the test class. When I run the test class it says that my account name is being updated to null.
System.DmlException: Update failed. First exception on row 0 with id 001E000000PNqdrIAD; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]
Can someone help please?
public class updateAccountFromContract { public final Contract contract {get; set;} public updateAccountFromContract(ApexPages.StandardController controller) { this.contract = (Contract) controller.getRecord(); } public pageReference updateAccount() { Account account = new Account(Id = contract.AccountId); account.Name = Contract.Account.Name; update account; pageReference pageRef = new PageReference('/apex/FC_Contract_Account_Mini_Page_Display?id='+ contract.Id); pageRef.setRedirect(true); return pageRef; } //Test Class public static testMethod void testupdateAccountFromContract() { PageReference pageRef = Page.FC_Contract_Account_Mini_Page_Edit_Mode; Test.setCurrentPage(pageRef); Account account = new Account(name = 'test Account'); insert account; Contract contract = new Contract(AccountId = account.Id, Status = 'Draft', StartDate = system.today(), ContractTerm = 1); insert contract; //Contract.Account.Name = 'Updated'; ApexPages.currentPage().getParameters().put('id', contract.Id); ApexPages.StandardController stdController = new ApexPages.StandardController(contract); updateAccountFromContract controller = new updateAccountFromContract(stdController); controller.updateAccount(); String nextPage = controller.updateAccount().getURL(); System.assertEquals('/apex/FC_Contract_Account_Mini_Page_Display?id='+ contract.Id, nextPage); } }
Here's the actual visualforce if you're interested. It's housed as a service could console component (a side window view with the contract object as the primary tab).
<apex:page standardController="Contract" extensions="updateAccountFromContract">
<apex:form >
<apex:pageblock tabStyle="Account" id="pageblock" mode="inlineEdit" title="Account Edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!updateAccount}" id="Save" value="Save" />
<apex:commandButton action="{!Cancel}" id="cancelButton" value="Cancel"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Account Details" columns="1" ShowHeader="false" collapsible="false">
<apex:inputField style="text-align:left" value="{!Contract.AccountId}"/>
<apex:inputField style="text-align:left" value="{!Contract.Account.Name}" />
</apex:pageblocksection>
</apex:pageblock>
</apex:form>
</apex:page>
Here's the thing: when you execute this line of code:
All that happens is that you end up with a Contract SObject with a value in the ID field - nothing else. If you want to access any other fields on that Contract, much less other related fields, you are going to have to execute some SOQL, probably in your constructor.
So what's actually happening is that Contract.Account.Name is null.
Hope this helps,
All Answers
Is Name a required field on the contract?
Double check the logs and see what line number the error is happening on.
Jeff
Here's the thing: when you execute this line of code:
All that happens is that you end up with a Contract SObject with a value in the ID field - nothing else. If you want to access any other fields on that Contract, much less other related fields, you are going to have to execute some SOQL, probably in your constructor.
So what's actually happening is that Contract.Account.Name is null.
Hope this helps,
Should I go about declaring in my extension class all of the fields eligible for update?
Such as adding public String accountName and passing it through that way? Then in my test class pass in ApexPages.currentPage().getParameters().put('accountName', 'updated account name');
Or am I thinking about this wrong? Perhaps a quick example would help.
Jeff,
The name field is a required field on the Account record.
The environment that I'm working in is the service cloud console, where the primary page is the contract and you can have console components that point to separate visual force pages.
The visualforce page rendered in the side console component is run off of the controller located in the primary page, in this case, the contract. Every field is Contract.Account.fieldName like this:
But to make save any changes to the account information from this window - I need the extension to reroute the updates. The extension that I have works for saving changes, but I'm missing something in my test class to stimulate a change in the form field in order to provide an update.