You need to sign in to do that
Don't have an account?
Linda 98
URGENT!!! Display multiple error messages on VF page.
I am having 6 fields on a VF page.All those fields are required and cant be left blank if name field is filled.
I am using <apex:pagemessages/ > in my Vf page and below is my code.
Problem which i am not able to solve is,when i give name value and left other fields blank,i am getting only one error message.
Like Phone is required.
When i enter phone and save it,i get billing street is required.
But i want to display all the error messages at once.(fields which are nit filled ..error messages related to those fields)
Please help.
I am using <apex:pagemessages/ > in my Vf page and below is my code.
Problem which i am not able to solve is,when i give name value and left other fields blank,i am getting only one error message.
Like Phone is required.
When i enter phone and save it,i get billing street is required.
But i want to display all the error messages at once.(fields which are nit filled ..error messages related to those fields)
Please help.
Public pagereference save(){ if(accountname!= '' && accountname!= null){ account.name=accountname; if(phonenumber!= '' && phonenumber!=null) { account.phone=phonenumber; } else{ ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Phone is required')); return null; } if(Billingstreet != '' && Billingstreet !=null) { account.billingstreet=Billingstreet; } else{ ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing Street is required')); return null; } if(Billingcity != '' && Billingcity !=null) { account.billingcity=billingcity; } else{ ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing City is required')); return null; } if(billingzipcode != '' && billingzipcode !=null) { account.billingpostalcode=billingzipcode; } else{ ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing Zip is required')); return null; } upsert account; contact c=new contact(); c.accountid=account.id; if(contactname!= '' && contactname !=null){ c.lastName=contactname; }else{ ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Contact Name is required')); return null; } if(contactemail!= '' && contactemail !=null){ c.Email=contactemail; }else{ ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Contact Email is required')); return null; } insert c; } return new PageReference('/'+pr.Id+'/e?retURL=%2F'+pr.Id); }
Can you remove 'Return null' from every else loop.
And add below 'If' condition before 'insert c;'
Please let me know, if its still not working.
Please mark this answer as best if reply helpeed anyway.
If the value is given then check other conditions.
<apex:inputtext label="Contact Email" value="{!contactemail}" required="true"/>
I just made chnages to code as you suggested.However,all other fields displays error message except contact name.
When i fill account name,and leave contact name(which will be required to create a contact) i am getting standard error( Rather than error on VF page).
how can i overcome that?
If i fill lastname and click on save i get error on VF page saying i am missing other fields.
Why is it not working with lastname.(i understand lastname is required field to create a contact but still i need that to display error message on VF page).
When i fill all the fields and click on save,NOTHING IS happening.JUst screen sits there.No account or contact is being created .
No redirect to next page.
God!!I messed up something which i am not sure what is that.
try{
}catch(exception e){
ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, e.getmessage()));
}
public class displayserrors {
public string accountname{set;get;}
public string phonenumber{set;get;}
public string Billingstreet{set;get;}
public string billingcity {set;get;}
public string billingzipcode{set;get;}
public string contactname{set;get;}
public string contactemail{set;get;}
public displayserrors(apexpages.standardcontroller count){
account a=(account)count.getrecord();
}
Public void check(){
account a=new account();
a.Name=accountname;
a.Phone=phonenumber;
a.BillingState=Billingstreet;
a.BillingCity=billingcity;
a.BillingPostalCode=billingzipcode;
try{
if(accountname.isNumeric()){
ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Phone is required'));
}else{
insert a;
}
}catch(exception e){
apexpages.addMessages(e);
}
//apexpages.adderror((new apexpages.message(ApexPages.Severity.ERROR,String.valueOf(new testexception('invalid account number')));
// ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'account is required'));
if(phonenumber== '' || phonenumber==null)
{
ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Phone is required'));
}
if(Billingstreet == ''|| Billingstreet.equals(null))
{
ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing Street is required'));
}
if(Billingcity == '' || Billingcity.equals(null))
{
ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing City is required'));
}
if(billingzipcode == '' || billingzipcode.equals(null))
{
ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing Zip is required'));
}
contact c=new contact();
c.accountid=a.id;
c.LastName=contactname;
c.Email=contactemail;
try{
if(contactname.isNumeric()){
ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Contact Name is must be in string'));
}
else{
insert c;
}
}catch(exception e){
ApexPages.addMessages(e);
}
if(contactemail== '' ||contactemail ==null){
ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Contact Email is required'));
}
}
}
vf page
<apex:page standardController="account" extensions="displayserrors" >
<apex:form >
<apex:pageMessages />
<apex:pageBlock >
<apex:pageBlockSection title="account information">
<apex:outputLabel value="name"/>
<apex:inputText value="{!accountname}" />
<apex:outputLabel value="phone no"/>
<apex:inputText value="{!phonenumber}" />
<apex:outputLabel value="street"/>
<apex:inputText value="{!Billingstreet}" />
<apex:outputLabel value="city"/>
<apex:inputText value="{!billingcity}" />
<apex:outputLabel value="zipcode"/>
<apex:inputText value="{!billingzipcode}" />
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock >
<apex:pageBlockSection columns="2" title="contact information" >
<apex:outputLabel value="name"/>
<apex:inputText value="{!contactname}" />
<apex:outputLabel value="mail"/>
<apex:inputText value="{!contactemail}" />
</apex:pageBlockSection>
</apex:pageBlock>
<apex:commandButton value="check" action="{!check}" />
</apex:form>
</apex:page>
i hope it is help full
https://developer.salesforce.com/forums?state=id#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=OPENQUESTIONS&id=906F0000000kJybIAE
Please help!!