Skip to main content
Bumped by Community user
screenshot added
Source Link
cloudZigZag
  • 1.4k
  • 3
  • 31
  • 81

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>

</apex:form> </apex:page>

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

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>

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>

</apex:form> </apex:page>

added 274 characters in body
Source Link
cloudZigZag
  • 1.4k
  • 3
  • 31
  • 81

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

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>

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

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

 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>

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

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>

Source Link
cloudZigZag
  • 1.4k
  • 3
  • 31
  • 81

error message show if cityname is already exist on vfpage

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

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

 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>