0

my apex code:

public class ListExample {
 public list < account > myaccount {
  get;
  set;
 }
 public ListExample() {
  account a1 = new account(name = 'sam', type = 'prospect');
  account a2 = new account(name = 'ram', type = 'prospect');
  myaccount = new list < account > ();
  myaccount.add(a1);
  myaccount.add(a2);
 }
 public pagereference save() {
  update myaccount;
  return null;
 }
}

visualforce code:

<apex:page controller="ListExample">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!myaccount}" var="a">
<apex:column value="{!a.name}"/>
<apex:column value="{!a.type}"/>
</apex:pageBlockTable>
<apex:commandButton value="save" action="{!save}"/>
</apex:pageBlock>
</apex:form>
</apex:page>

when i clicked save it shows this error: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: [] Error is in expression '{!save}' in component in page listexamplevf: Class.ListExample.save: line 17, column 1 what is the problem can anyone help me

3

You are trying to use an update. Update only works if there is an ID already present in the database.

What you want is to use insert or upsert command.

Make sure , you are populating all the mandatory fields in Account record.

 public class ListExample {
 public list < account > myaccount {
  get;
  set;
 }
 public ListExample() {
  account a1 = new account(name = 'sam', type = 'prospect');
  account a2 = new account(name = 'ram', type = 'prospect');
  myaccount = new list < account > ();
  myaccount.add(a1);
  myaccount.add(a2);
 }
 public pagereference save() {
  upsert myaccount;
  return null;
 }
}

SRC: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_insert_update.htm

0

This error is pretty straightforward: you can't perform an update on a record that doesn't exist yet. Since you created the Accounts in myaccount, you have to do a DML insert, before you can update. Since this looks like a toy example, you may be intending to do this in your constructor. It's not a typical sequence of operations for real code.

Manipulate Records with DML might be helpful in clarifying this kind of issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.