You need to sign in to do that
Don't have an account?
Pass Account Name to Custom Object
Hello,
I have a VF page with some basic inputs that populates values on a custom object that is a child of the Account object. Below is the VF page and custom controller. My problem is that the Company Name field on the custom object should pre-populate with the Account Name from the record the VF page is initiated. Everything seems to work fine, however, the Account Name is not coming through. Can anyone tell me what I'm doing wrong, or not doing? Thanks.
VF Page:
<apex:page standardController="Account" tabStyle="Account" extensions="TestController3">
<apex:form >
<apex:pageBlock title="Test" >
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}" rerender="error" />
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!test}" var="a" id="table">
<apex:column headerValue="Company Name">
{!a.Company_Name_Test__c}
</apex:column>
<apex:column headerValue="Input 1">
<apex:inputField value="{!a.Input_1__c}"/>
</apex:column>
<apex:column headerValue="Input 2">
<apex:inputField value="{!a.Input_2__c}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public class TestController3 {
public List<Test_Object__c> test {get; set;}
private final Account acct;
public TestController3(ApexPages.StandardController myController) {
acct=(Account)myController.getrecord();
test= new List<Test_Object__c>();
Test_Object__c test2 = new Test_Object__c();
test2.Company_Name_Test__c = acct.id;
test.add(test2);}
public PageReference save() {
insert test;
PageReference home = new PageReference('/home/home.jsp');
home.setRedirect(true);
return home; }
}
Thanks for all your help!! I actually got it to work by changing my custom button code to include the Account ID:
window.open('/apex/TestPage3?id={!Account.Id}','_Account');
My final VF page and Controller are below:
VF Page:
<apex:page standardController="Account" tabStyle="Account" extensions="TestController3">
<apex:form >
<apex:pageBlock title="Test" >
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}" rerender="error" />
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!test}" var="a" id="table">
<apex:column headerValue="Company Name">
<apex:outputField value="{!a.Company_Name_Test__c}" />
</apex:column>
<apex:column headerValue="Input 1">
<apex:inputField value="{!a.Input_1__c}"/>
</apex:column>
<apex:column headerValue="Input 2">
<apex:inputField value="{!a.Input_2__c}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public class TestController3{
public List<Test_Object__c> test {get; set;}
private final Account acct;
public TestController3(ApexPages.StandardController myController) {
acct=(Account)myController.getrecord();
test= new List<Test_Object__c>();
Test_Object__c test2 = new Test_Object__c();
test2.Company_Name_Test__c = acct.id;
test.add(test2);}
public PageReference save() {
insert test;
PageReference acctPage = new ApexPages.StandardController(acct).view();
acctPage.setRedirect(true);
return acctPage; }
}
All Answers
Hi JN22,
So from the looks of your Company_Name_Test__c field, this looks like a lookup field to Account from your custom object Test_Object__c. To reference any fields on the Account record, in your VF page you should use something like "{!a.Company_Name_Test__r.Name}" to grab fields off the Account through your relationship field.
Let me know if that is the solution!
Samantha
i suggest u to do something like a.Company_Name_Test__r.Name.. doing something like this or other will solve ur problem
Thanks for the suggestions, but changing line 9 of my VF page above to {!a.Company_Name_Test__r.Name} still does not pre-populate the Account Name. Is there something else I need to do to either the VF page or the custom Controller? Thanks.
Both cases show the same page. There are 3 columns: the first is Company Name which shows nothing under the header; the 2nd is Input 1 which has a picklist under the header; and the 3rd is picklist 2 which also has a picklist under the header. Changing the {!a.Company_Name_Test__c} to {!a.Company_Name_Test__r.Name} does not change how the page appears.
And yes, my Test object and the Company_Name_Test__c field have full visibility/Read/Write access. Thanks.
What if in your constructor you use acct=(Account)myController.getId(); instead of (Account)myController.getRecord();? You are only using the Id right now anyway... Then in the line test2.Company_Name_Test__c = acct.id; you could simply assign it to acct (rather than acct.id) since you directly set the variable to the ID value.
If this still doesn't work, use a debug statement or system.assert to tell me what the value is stored in the acct variable.
Making those changes gives me an error when I try to save the controller:
Error: TestController3 Compile Error: Incompatible types since an instance of String is never an instance of SOBJECT:Account at line 7 column 16
You would have to change the type... Here like this:
private final String acct;
public TestController3(ApexPages.StandardController myController) {
acct= myController.getId();
test= new List<Test_Object__c>();
Test_Object__c test2 = new Test_Object__c();
test2.Company_Name_Test__c = acct;
test.add(test2);}
ok?
Thanks for all your help!! I actually got it to work by changing my custom button code to include the Account ID:
window.open('/apex/TestPage3?id={!Account.Id}','_Account');
My final VF page and Controller are below:
VF Page:
<apex:page standardController="Account" tabStyle="Account" extensions="TestController3">
<apex:form >
<apex:pageBlock title="Test" >
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}" rerender="error" />
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!test}" var="a" id="table">
<apex:column headerValue="Company Name">
<apex:outputField value="{!a.Company_Name_Test__c}" />
</apex:column>
<apex:column headerValue="Input 1">
<apex:inputField value="{!a.Input_1__c}"/>
</apex:column>
<apex:column headerValue="Input 2">
<apex:inputField value="{!a.Input_2__c}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public class TestController3{
public List<Test_Object__c> test {get; set;}
private final Account acct;
public TestController3(ApexPages.StandardController myController) {
acct=(Account)myController.getrecord();
test= new List<Test_Object__c>();
Test_Object__c test2 = new Test_Object__c();
test2.Company_Name_Test__c = acct.id;
test.add(test2);}
public PageReference save() {
insert test;
PageReference acctPage = new ApexPages.StandardController(acct).view();
acctPage.setRedirect(true);
return acctPage; }
}