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>

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

  • You should really check each individual city value for duplicates. – Adrian Larson Feb 25 '17 at 20:56
  • @AdrianLarson I have updated my ans – Santanu Boral Feb 25 '17 at 21:24
  • You're still only validating the first record. – Adrian Larson Feb 25 '17 at 21:25
  • Here, I am throwing all the cite names which are duplicates – Santanu Boral Feb 25 '17 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 – cloudZigZag Feb 26 '17 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.