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
JN22JN22 

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; }

}

Best Answer chosen by Admin (Salesforce Developers) 
JN22JN22

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

SamReadySamReady

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

Vishal GuptaVishal Gupta
u must use relationships i.e. "__r.name" instead of __c. This will auto populate the acc name.

i suggest u to do something like a.Company_Name_Test__r.Name.. doing something like this or other will solve ur problem
JN22JN22

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.

SamReadySamReady

Can you please share what shows up on your VF page? (both for the original {!a.Company_Name_Test__c} case and {!a.Company_Name_Test__r.Name} case.

 

Also, does your test object have full visibility/Read/Write? If you created it with Schema Builder, even as an admin, it would be invisibile on a custom page.

JN22JN22

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.

SamReadySamReady

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.

JN22JN22

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

 

 

SamReadySamReady

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?

JN22JN22

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; }
}

This was selected as the best answer