want to show error message that if cityname is duplicate then show error message cityname already exists! whats i'm doing wrong anyone please tell
When saving record from cities tab and if dupicate city it showing error Kudos!!
when saving record from visualforce page it's not showing error message :(
class code:
public pageReference save(){
try{
upsert cityNames;
}catch(Exception e){
String error = e.getMessage();
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,error));
}
return new pageReference('/apex/CityControllerPage').setRedirect(true);
}
Trigger code
set<String> cityName = new set<String>();
for(City_Name__c ct : Trigger.New){
cityName.add(ct.name);
}
List<City_Name__c> cList = [SELECT id, Name FROM City_Name__c where name IN : cityName];
if(Trigger.isInsert){
for(City_Name__c ct : Trigger.New){
if(ct.name!=null && cList.size()>0){
ct.name.addError('City Name already Exits');
}
}
}
Use page messages on vfpage
<apex:page controller="Ciler" tabStyle="Contact">
<apex:pagemessages ></apex:pagemessages>
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons location="top" >
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="City Input">
<apex:inputField value="{!cityNames.name}"/>
<apex:inputField value="{!cityNames.Pin__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form> </apex:page>