0

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!!

enter image description here

when saving record from visualforce page it's not showing error message :( enter image description here

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>

4
  • how cList is being populated in trigger? Commented Feb 25, 2017 at 20:43
  • 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]; Commented Feb 25, 2017 at 20:46
  • Edit your question and add this Commented Feb 25, 2017 at 20:46
  • is the problem that no error message is appearing? If so, your catch block needs to return null
    – cropredy
    Commented Feb 26, 2017 at 0:10

1 Answer 1

0

Since you are creating city names upon looping through the Trigger.New, so you can execute the query and check the size of list.

 set<String> cityName = new set<String>();

 for(City_Name__c  ct : Trigger.New)
 {
       if(Trigger.isInsert)
       cityName.add(ct.name);    
 }
 List<AggregateResult> lst = 
                    [SELECT Name, COUNT(Id) 
                    FROM City_Name__c
                    WHERE name IN : cityName
                    GROUP By Name
                    HAVING COUNT(Id)>0];

   if(cList.size()>0){
       Trigger.new[0].addError('City Name already Exist');
    }

The above error is applicable for any matching found.

If you want to throw errors showing which specific city name matching with the result, then you need to loop through the list and show the error.

List<AggregateResult> lst = 
                    [SELECT Name, COUNT(Id) 
                    FROM City_Name__c
                    WHERE name IN : cityName
                    GROUP By Name
                    HAVING COUNT(Id)>0];

List<String> lstStr = new List<String>();                   
for (AggregateResult ar : lst)  {
    lstStr.add((String) ar.get('Name'));
}
Trigger.new[0].addError(String.join(lstStr,',') + ' already exist');

To return error message, use return null in catch block in controller method.

8
  • You should really check each individual city value for duplicates.
    – Adrian Larson
    Commented Feb 25, 2017 at 20:56
  • @AdrianLarson I have updated my ans Commented Feb 25, 2017 at 21:24
  • You're still only validating the first record.
    – Adrian Larson
    Commented Feb 25, 2017 at 21:25
  • Here, I am throwing all the cite names which are duplicates Commented Feb 25, 2017 at 21:33
  • @SantanuBoral through tab(cities) when record saving if duplicate city it's showing error and its working fine but the thing is when i'm showing preview of visualforce page then duplicate city exit not showing any error message. i have added screenshot Commented Feb 26, 2017 at 4:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .