You need to sign in to do that
Don't have an account?
Rutvij Pathak 5
Apex field validation not working
Hi , I am trying to insert list of Contacts from visualforce page on a Button click. And after button click the page should automatically go back to account page.
I have Created a Custom Button on an Account page.
The list also has a Validation to check if the first name is updated or not.
Basically - 1 - Contact First name must not be blank.
2 - On Click of Save, Added Contacts should be inserted to the database.
3 - after insert Source account record should open
My below code saves the records and redirects correctly but does not fire validation on first name.
Please find controller code below :-
public class AddMultipleContacts {
public List<Contact> ContactList {get;set;}
public Integer rowNum{get;set;}
public string AcctID{get;set;}
sObject con = [Select ID ,FirstName , LastName from Contact LIMIT 1];
public AddMultipleContacts(ApexPages.StandardController controller ){
AcctID = ApexPages.currentPage().getParameters().get('Id');
system.debug('-AcctID--'+AcctID);
ContactList = new List<Contact>();
ContactList.add(new contact(Accountid = Acctid));
}
public PageReference save(){
for(Contact con :ContactList)
{
if(con.FirstName == '' || con.FirstName == null)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Contact First name'));
} else
{
insert ContactList ;
}
}
PageReference page1 = new PageReference ('https://ap4.salesforce.com/'+ AcctId );
Page1.setRedirect(true) ;
return page1;
}
public void insertRow(){
ContactList.add(new contact(Accountid = Acctid ));
}
public void delRow(){
rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
ContactList.remove(rowNum);
}
public PageReference Cancel(){
PageReference page2 = new PageReference ('https://ap4.salesforce.com/'+ AcctId ) ;
Page2.setRedirect(true) ;
return page2;
}
}
I have Created a Custom Button on an Account page.
The list also has a Validation to check if the first name is updated or not.
Basically - 1 - Contact First name must not be blank.
2 - On Click of Save, Added Contacts should be inserted to the database.
3 - after insert Source account record should open
My below code saves the records and redirects correctly but does not fire validation on first name.
Please find controller code below :-
public class AddMultipleContacts {
public List<Contact> ContactList {get;set;}
public Integer rowNum{get;set;}
public string AcctID{get;set;}
sObject con = [Select ID ,FirstName , LastName from Contact LIMIT 1];
public AddMultipleContacts(ApexPages.StandardController controller ){
AcctID = ApexPages.currentPage().getParameters().get('Id');
system.debug('-AcctID--'+AcctID);
ContactList = new List<Contact>();
ContactList.add(new contact(Accountid = Acctid));
}
public PageReference save(){
for(Contact con :ContactList)
{
if(con.FirstName == '' || con.FirstName == null)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Contact First name'));
} else
{
insert ContactList ;
}
}
PageReference page1 = new PageReference ('https://ap4.salesforce.com/'+ AcctId );
Page1.setRedirect(true) ;
return page1;
}
public void insertRow(){
ContactList.add(new contact(Accountid = Acctid ));
}
public void delRow(){
rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
ContactList.remove(rowNum);
}
public PageReference Cancel(){
PageReference page2 = new PageReference ('https://ap4.salesforce.com/'+ AcctId ) ;
Page2.setRedirect(true) ;
return page2;
}
}
You need return null when find the error.
The other error is the insert within a "for"
othe error is in a url put the partial URL not url completed
public PageReference save(){
for(Contact con :ContactList) {
if(con.FirstName == '' || con.FirstName == null){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Contact First name'));
return null;
}
}
insert ContactList ;
PageReference page1 = new PageReference ('/'+ AcctId );
Page1.setRedirect(true);
return page1;
}
Regards
Diego Arenas
All Answers
You can try below logic
integer apexflag=0;
if(con.FirstName == '' || con.FirstName == null)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Contact First name'));
apexflag=1;
} else
{
apexflag=0;
insert ContactList ;
}
if(apexflag==1)
{
return null;
}
You need return null when find the error.
The other error is the insert within a "for"
othe error is in a url put the partial URL not url completed
public PageReference save(){
for(Contact con :ContactList) {
if(con.FirstName == '' || con.FirstName == null){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Contact First name'));
return null;
}
}
insert ContactList ;
PageReference page1 = new PageReference ('/'+ AcctId );
Page1.setRedirect(true);
return page1;
}
Regards
Diego Arenas