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
Itay Shechter 10Itay Shechter 10 

Unknown constructor error in visualforce page

Hi, I'm trying to use a standardcontroller in the Visualforce page in order to use it in a custom button. But I'm having this error:
Unknown constructor 'CarController.CarController(ApexPages.StandardController controller)'
I believe that I need to add something to the controller, but I could not find a solution that worked for me(I tried all the solutions that I found online)...

So, this is the code:
<apex:page  standardController="Case" extensions="CarController" lightningStylesheets="true" title="test">
    <apex:form>
        <apex:pageMessages/>
        <apex:pageBlock >    
            <apex:pageBlockSection columns="2">
                <apex:inputField value="{!abc.Name__c}"/> 
                <apex:inputField value="{!abc.License_Number__c}"/>
                <apex:inputField value="{!abc.Manufacturer__c}"/>
            </apex:pageBlockSection>
            
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Save" action="{!save}" styleClass="slds-button slds-button_brand"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class CarController{

    public Car__c abc {Get;Set;}

    public CarController() {     
        abc = new Car__c();
    }
    
    public PageReference save(){
    	try{
        	INSERT abc;
        }
        catch (exception e){
            
        }
        return null;
    }
    
}
P.S. I'm not a developer, just try to learn in order to get there one day... so I'll be happy to understand what is missing.

Thanks!



 
Best Answer chosen by Itay Shechter 10
Maharajan CMaharajan C
Hi Itay,

Please use the below updated class:

You have to change the constructor like below.
 
public class CarController {

    public Car__c abc {get;set;}

    public CarController(ApexPages.StandardController controller) {
        abc = new Car__c ();
    }

    public PageReference save(){
        try{
            INSERT abc;
        }
        catch (exception e){
            
        }
        return null;
    }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Itay,

Please use the below updated class:

You have to change the constructor like below.
 
public class CarController {

    public Car__c abc {get;set;}

    public CarController(ApexPages.StandardController controller) {
        abc = new Car__c ();
    }

    public PageReference save(){
        try{
            INSERT abc;
        }
        catch (exception e){
            
        }
        return null;
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
Itay Shechter 10Itay Shechter 10
Thanks a lot Maharajan!