we designed a vf page called competitor.Competitor has validation rule.This is running fine in the back end.But the validation error message has not been displayed on the page. Here the controller :
public with sharing class ctrlCompetitor {
public static Integer flag=0;
public Competitor__c cc{get;set;}
public String customerId{get;set;}
public Competitor__c cc1=new Competitor__c();
public List<Competitor__c> listComptr=new List<Competitor__c>();
public ctrlCompetitor(){}
//extension invoked to pre-populate the Name parameter
public ctrlCompetitor(ApexPages.StandardController stdController) {
try{
cc=new Competitor__c();
cc = (Competitor__c)stdController.getRecord();
if(ApexPages.currentPage().getParameters().get('Name')!=null)
cc.Name=ApexPages.currentPage().getParameters().get('Name');
}
catch(Exception e){System.debug(e.getMessage());}
}
//cache all the primary competitors for the particular opportunity
public void init(){
listComptr=[SELECT Id,Name,Primary_Competitor__c from Competitor__c where
Opportunity__r.Id =:ApexPages.currentPage().getParameters().get('retURL').substring(1,16) AND Primary_Competitor__c=true];
}
//method to uncheck the primary competitor for other competitor records
@RemoteAction
public static void checkPrimaryComp(String oppId){
Map<Id,Competitor__c> compMap=new Map<Id,Competitor__c>([SELECT Id,Name,Primary_Competitor__c from Competitor__c where Opportunity__r.Id =:
oppId.substring(1,16)]);
for(Competitor__c comp : compMap.values()){
comp.Primary_Competitor__c=false;}
update compMap.values();
}
//invoked to check for the first primary competitor
@RemoteAction
public static String checkFirstPrimaryComp(String oppId){
List<Competitor__c> listTemp=[SELECT Id,Name,Primary_Competitor__c from Competitor__c where
Opportunity__r.Id =:oppId.substring(1,16) AND Primary_Competitor__c=true];
if(listTemp.size()==0){return 'true';}
else{return 'false';}
}
//method invoked on update of Competitor records
public void onUpdatePrimaryComp(){
try{
if(Trigger.IsUpdate && !Trigger.IsInsert && flag==0){
flag++;
SET<Id> oppIds=new SET<Id>();
for(Sobject sobj: Trigger.new){
Competitor__c cc1=(Competitor__c)sobj;
oppIds.add(cc1.Opportunity__c);
}
List<Competitor__c> listTemp=[SELECT Id,Name,Primary_Competitor__c,Competitor_Machine__c,Competitor_Machine_Description__c,Opportunity__r.Id from Competitor__c where Opportunity__r.Id in : oppIds AND
Primary_Competitor__c=true] ;
if(listTemp.size()!=0){
for(Sobject sobj: Trigger.new){
Competitor__c cc1=(Competitor__c)sobj;
if(cc1.Primary_Competitor__c==true){
for(Integer i=0;i<listTemp.size();i++){
if(listTemp.get(i).Opportunity__r.Id == cc1.Opportunity__c){
listTemp.get(i).Primary_Competitor__c=false;}
}
}
}
update listTemp;}
}}
catch(Exception e){
System.debug('==Exception Message==='+e.getMessage());}
}
//method invoked to save and return to the original Opportunity record Page
public PageReference Save(){
try{
if(ApexPages.currentPage().getParameters().get('Name')!=null)
{
insert cc;
}
else
update cc;
}
catch(DMLException e){
ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.ERROR, e.getdmlMessage(0) );
ApexPages.addMessage(msg);
}
return new PageReference('/'+ApexPages.currentPage().getParameters().get('retURL').substring(1,16));
}
//method is invoked on click of CANCEL button
public PageReference Cancel(){
try{
if(ApexPages.currentPage().getParameters().get('Name')==null){
if(listComptr.get(0).primary_Competitor__c==false){
listComptr.get(0).primary_Competitor__c=true;
}
}
update listComptr;
}
catch(Exception e){
System.debug('==Exception Message==='+e.getMessage());
}
return new PageReference('/'+ApexPages.currentPage().getParameters().get('retURL').substring(1,16));
}
}
Visualforce Page:
function chngPrimComp(cmp,val){
var c;
var flag=0;
if(cmp.checked==false && flag!=0){
alert('{!$Label.msgCantUnchk}');
cmp.checked=true;
flag++;
}
if(cmp.checked==true && flag==0){
ctrlCompetitor.checkFirstPrimaryComp('{!$CurrentPage.parameters.retURL}',function(result, event){
if(event.status){
if(result=='false'){
c=confirm('{!$Label.msgPrmryCompCnfrm}');
if(c==true){
flag++;
ctrlCompetitor.checkPrimaryComp('{!$CurrentPage.parameters.retURL}',function(result, event){
if(event.status){}
if(event.exception){}
});
}
else if(c==false){
cmp.checked=false;
}
}
}
});
}
}
</script>
<apex:form >
<apex:pageblock mode="edit" title="{!$ObjectType.Competitor__c.label} Edit" >
<apex:PageMessages />
<apex:pageblockbuttons >
<apex:commandButton value="Save" action="{!Save}" />
<apex:commandbutton value="Cancel" action="{!Cancel}"/>
</apex:pageblockbuttons>
<apex:outputpanel >
<apex:pageblocksection title="Information" showheader="true" columns="2">
<apex:inputfield id="oppid" value="{!Competitor__c.Opportunity__c}" required="true"/>
<apex:inputfield value="{!Competitor__c.Amount__c}" required="false"/>
<apex:inputfield value="{!Competitor__c.Competitor_Name__c}" required="true"/>
<apex:inputfield id="chkbox" value="{!Competitor__c.Primary_Competitor__c}" required="false" onchange="chngPrimComp(this,this.value);" />
<apex:inputfield value="{!Competitor__c.Competitor_Machine__c}" required="false"/>
<apex:pageblocksectionitem />
<apex:inputfield value="{!Competitor__c.Competitor_Machine_Description__c}" required="false"/>
<apex:pageblocksectionitem />
</apex:pageblocksection>
<apex:pageblocksection title="Competitor Strengths / Weaknesses" showheader="true" columns="2">
<apex:inputfield value="{!Competitor__c.Competitors_Strength__c}" required="false"/>
<apex:inputfield value="{!Competitor__c.Competitors_Weakness__c}" required="false"/>
<apex:inputfield value="{!Competitor__c.Competitive_Situation__c}" required="false"/>
<apex:pageblocksectionitem />
</apex:pageblocksection>
<apex:pageblocksection title="System Information" showheader="true" columns="2">
<apex:inputfield value="{!Competitor__c.Name}" required="true"/>
</apex:pageblocksection>
</apex:outputpanel>
</apex:pageblock>
</apex:form>