Help: getting error while running custom controller apex class

520 views
Skip to first unread message

mahendiran jayavarma

unread,
Sep 10, 2010, 3:21:43 AM9/10/10
to salesforce-p...@googlegroups.com
Hi,

I created following custom controller:

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

and I used following apex class for that controller:

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



While running I am getting error for "System.QueryException: List has no rows for assignment to SObject"

I guess i am not giving access permission for Sobject control.

But i dont know where i have to give grant access permission.

 So Please any one provide me the solution.

and tell me steps i have to follow.

Thanks,
Mahendiran.






rohit aggarwal

unread,
Sep 12, 2010, 7:03:41 AM9/12/10
to salesforce-p...@googlegroups.com
You are getting error because the account won't exist. this code only works for existing account records. you have to insert the account and have to use conditional if statement like this

public class MyController {

      private final Account account;          
 
       public list<Account> allaccount= new list<Account>(); 
 public MyController() { allaccount = [select id, name, site from Account where id = :ApexPages.currentPage().getParameters().get('id') limit 1];       
        if(!
allaccount.isEmpty() )
        account =
allaccount[0];
        else
         account = new account();
   
}


      public Account getAccount() {
            return account;
      }

      public PageReference save() 
{
        if(
!allaccount.isEmpty() )
         insert account;
         else
 
update account; return null; }
}

this will defiantly work fine for both new and existing accounts. Let me know if face any other problem

Rohit

mahendiran jayavarma

unread,
Sep 13, 2010, 12:43:07 AM9/13/10
to salesforce-p...@googlegroups.com
It wont work.. im getting follwoing error.

System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

Class.MyController.save: line 23, column 1 External entry point


Thanks,
Mahendiran.

rohit aggarwal

unread,
Sep 13, 2010, 2:25:03 AM9/13/10
to salesforce-p...@googlegroups.com
Are you passing the AccountId to vf page?

mahendiran jayavarma

unread,
Sep 13, 2010, 4:41:33 AM9/13/10
to salesforce-p...@googlegroups.com
no

rohit aggarwal

unread,
Sep 13, 2010, 10:52:02 AM9/13/10
to salesforce-p...@googlegroups.com
From which page are you calling this account vf page?
Reply all
Reply to author
Forward
0 new messages