You need to sign in to do that
Don't have an account?

First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]
Hi All,
I have done a simple code however when I am runnung my test class I am getting error.
Apex code:
public class AccountSaveCont{
public Account acc{get;set;}
public AccountSaveCont(){
acc = new Account();
}
public void save1(){
insert acc;
}
}
Vf Page:
<apex:page controller="AccountSaveCont">
<apex:form >
<apex:pageBlock title="My Content" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!save1}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="My Content Section" columns="2">
<apex:inputField value="{!acc.name}"/>
<apex:inputField value="{!acc.site}"/>
<apex:inputField value="{!acc.type}"/>
<apex:inputField value="{!acc.accountNumber}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Test Class:
@isTest(seeAllData = true)
public class AccountSaveContTest{
public static testMethod void AccountInsert(){
Account a = new Account(Name='test', phone = '1234567890');
a.Name= 'DineshTesting1';
insert a;
PageReference pr= Page.AccountSave;
Test.setCurrentPage(pr);
AccountSaveCont m = new AccountSaveCont();
m.save1();
}
}
I have done a simple code however when I am runnung my test class I am getting error.
Apex code:
public class AccountSaveCont{
public Account acc{get;set;}
public AccountSaveCont(){
acc = new Account();
}
public void save1(){
insert acc;
}
}
Vf Page:
<apex:page controller="AccountSaveCont">
<apex:form >
<apex:pageBlock title="My Content" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!save1}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="My Content Section" columns="2">
<apex:inputField value="{!acc.name}"/>
<apex:inputField value="{!acc.site}"/>
<apex:inputField value="{!acc.type}"/>
<apex:inputField value="{!acc.accountNumber}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Test Class:
@isTest(seeAllData = true)
public class AccountSaveContTest{
public static testMethod void AccountInsert(){
Account a = new Account(Name='test', phone = '1234567890');
a.Name= 'DineshTesting1';
insert a;
PageReference pr= Page.AccountSave;
Test.setCurrentPage(pr);
AccountSaveCont m = new AccountSaveCont();
m.save1();
}
}
Your problem is because in the test class you didn't assign the account value back to the controller. So what my suggestion is
@isTest(seeAllData = true)
public class AccountSaveContTest{
public static testMethod void AccountInsert(){
Account a = new Account(Name='test', phone = '1234567890');
a.Name= 'DineshTesting1';
//insert a;
PageReference pr= Page.SaveAcc;
Test.setCurrentPage(pr);
AccountSaveCont m = new AccountSaveCont();
m.acc = a;
m.save1();
}
}
Please see the highlighted part. Another thing is it will be good to use 'seeAllData = false' during testing.
All Answers
Your problem is because in the test class you didn't assign the account value back to the controller. So what my suggestion is
@isTest(seeAllData = true)
public class AccountSaveContTest{
public static testMethod void AccountInsert(){
Account a = new Account(Name='test', phone = '1234567890');
a.Name= 'DineshTesting1';
//insert a;
PageReference pr= Page.SaveAcc;
Test.setCurrentPage(pr);
AccountSaveCont m = new AccountSaveCont();
m.acc = a;
m.save1();
}
}
Please see the highlighted part. Another thing is it will be good to use 'seeAllData = false' during testing.
Now I am writing for updating account . I have written test class , can you please help me to correct my code.
Apex code:
public class UpdateAccount{
public Account acc{get;set;}
public UpdateAccount(){
acc = [select id,name,site,type, Account Number from Account where id =: apexPages.currentPage().getParameter().get('id')];
}
public void save2(){
Update acc;
}
}
Vf Page:
<apex:page controller="UpdateAccount">
<apex:form >
<apex:pageBlock title="My Content" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!save2}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="My Content Section" columns="2">
<apex:inputField value="{!acc.name}"/>
<apex:inputField value="{!acc.site}"/>
<apex:inputField value="{!acc.type}"/>
<apex:inputField value="{!acc.accountNumber}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Test Class:
@isTest(seeAllData = false)
public class UpdateAccountTest{
public static testMethod void UpdateAccountTestM(){
Account Ua = new Account(Name='test', phone = '1234567890');
PageReference pr= page.UpdateAccount;
Test.setCurrentPage(pr);
UpdateAccount m = new UpdateAccount();
M.acc=Ua;
M.name= 'sample';
m.save2();
}
}
This is my understanding of your code:
Apex class - Some syntax error(underline)
Visualforce page - remain unchange
Apex Text class -
Hope my understand is correct to your code is correct.
Thanks for you reply. solution worked.
Now I am combining Save,Update and Delete action in once VF page. Update and delete is working fine but Save is throwing error as below
System.DmlException: Insert failed. First exception on row 0 with id 0012800001Wr1wcAAB; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Error is in expression '{!SaveAc}' in component <apex:commandButton> in page accsaveupdatdel: Class.AccountSaveUpdateDel.SaveAc: line 15, column 1
Class.AccountSaveUpdateDel.SaveAc: line 15, column 1
My code is here.
Can you please help me to fix this. Many thanks.
Regards,
Dj
This is what I'm thinking
but may I know how is the visualforce page being called? Is it from a button or from others apex code to pass in the account id into the url?
Sorry for late reply. Thanks for your reply. given code worked.
Many thanks.
Can you please help me writing code for EDIT BUTTON. my code is here..
Like if I click on EDIT Button record to be on edit mode.
Thanks in advance,
Dj
I'm not sure if I understand your requirement correctly, but here is my suggestion:
What I'm trying to do here is redirect you back to normal salesforce edit page based on the account id passed in.
I also found another issue on your apex code line 15, this will alway return you the new account cause you didn't have an else wrapper it this is what my suggestion: