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 

Need List Button on Custom Object

I'm trying to create a List Button on a custom object (Test_Object__c) that is a related list to the Account object.  I've created the VF page and Controller below, but I do not get the option of adding it to a custom list button.  I am also trying to pre-populate a look-up field on Test_Object__c with the Account name.  Can anyone tell me what I am doing wrong? 

 

VF Page:

 

<apex:page standardController="Account" tabStyle="Account" extensions="TestController3">
   <apex:form >
   <apex:pageBlock >
       <apex:pageBlockSection columns="2">
            <apex:inputField value="{!L1.test1__c}"/>
            <apex:inputField value="{!L1.test2__c}"/>
            <apex:inputField value="{!L1.test3__c}"/>
       </apex:pageBlockSection>
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!Save}"/>
            <apex:commandButton value="Save and New" action="{!News}"/>
            <apex:commandButton value="Cancel" action="{!cancel}"/>
        </apex:pageBlockButtons>
       </apex:pageBlock>
    </apex:form>
</apex:page>

 

 

Controller:

 

public class TestController3{

    public Account acct{get;set;}
    public Level_1__c L1{get;set;}

    public TestController3(ApexPages.StandardController controller) {
    
        This.L1 = (Level_1__c)controller.getRecord();
    }
    public PageReference Save(){
        L1.Company_Name__c = acct.Name;
        insert L1;
        PageReference ref = new PageReference('/'+acct.id);
        return ref;
    }
    public PageReference News(){
        return null;
    }
    public PageReference Cancel(){
        return null;
    }
}

Best Answer chosen by Admin (Salesforce Developers) 
S91084S91084

Hi,

 

While creating the custom button, select javascript and then in the source definethe script to open the visualforce page


window.open('/apex/<VisualforcePageName>','_parent');

 

You can also write condition in which case the page shld be displayed and add the button to the search layout.

All Answers

S91084S91084

Hi,

 

While creating the custom button, select javascript and then in the source definethe script to open the visualforce page


window.open('/apex/<VisualforcePageName>','_parent');

 

You can also write condition in which case the page shld be displayed and add the button to the search layout.

This was selected as the best answer
JN22JN22

Hi,

 

I added that javascript to the button and it allowed me to add it to the List Button on the Account, but now I am getting the error below:

 

Visualforce Error


System.TypeException: Invalid conversion from runtime type SOBJECT:Account to SOBJECT:Level_1__c
Class.TestController3.<init>: line 8, column 1

 

 

Any idea why?