+ Start a Discussion
subramanyam Rsubramanyam R 

Error Error: myController Compile Error: Illegal assignment from List<Account> to account at line 6 column 9

**********controller page*******

public class MyController {

    private final Account account;

    public MyController() {
        account = [SELECT Id, Name, Site FROM Account 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }

    public Account getAccount() {
        return account;
    }

    public PageReference save() {
        update account;
        return null;
    }
}

******visualpage****************************


<apex:page controller="myController" tabStyle="Account">
    <apex:form>
        <apex:pageBlock title="Congratulations {!$User.FirstName}">
            You belong to Account Name: <apex:inputField value="{!account.name}"/>
            <apex:commandButton action="{!save}" value="save"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>


*****************************************
throughing errorError    Error: myController Compile Error: Illegal assignment from List<Account> to account at line 6 column 9    
surasura
make sure you parameter Id is returning the correct Id , becuase if id is wrong and no account is found with given id Salesforce try to assign an empty Account  list to your Account variable 


below code is modified to handle that scenario
private final Account account;

    public MyController() {
       List<account>  accounts = [SELECT Id, Name, Site FROM Account 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
       if (Accounts.size() > 0)
      {
         account = account[0];

     }
     else
    {
       account  = new Account();
    }
    

    }

    public Account getAccount() {
        return account;
    }

    public PageReference save() {
        update account;
        return null;
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
Please try below code 
<apex:page controller="myController" tabStyle="Account">
    <apex:form>
        <apex:pageBlock title="Congratulations {!$User.FirstName}">
            You belong to Account Name: <apex:inputField value="{!account.name}"/>
            <apex:commandButton action="{!save}" value="save"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class MyController 
{
    Public Account account {get;set;}
    public MyController() 
	{
		List<Account> lstAccount =[ SELECT Id, Name, Site FROM Account WHERE Id = :ApexPages.currentPage().getParameters().get('id') ] ;
		if(lstAccount.size() > 0 )
		{
			account = lstAccount[0];
		}
		else
		{
			account = new Account();
		}
    }

    public PageReference save() 
	{
        update account;
        return null;
    }
}

Please let us know if this will help you