You need to sign in to do that
Don't have an account?
Anup Kabra 2
duplicate error message
I have visualforce page with two few fileds out of which there are two account fileds. I want to throw an error message when user tries to edit an existing record and enter same account name in both the account fields. I am not able to achieve this. below is my code: (It works fine for NEW record).
Visualforce Page:
<apex:page standardController="GRP_Fund_Relationship__c" extensions="GRP_EditNew_FundRelationshipCon" lightningStylesheets="true">
<apex:form >
<div align="center" draggable="false">
<apex:commandbutton action="{!SaveAndRedirect}" Value="Save"/>
<apex:commandbutton action="{!CancelAndRedirect}" Value="Cancel" immediate="true" />
</div>
<apex:pageBlock mode="edit">
<apex:pageBlockSection title="Fund Relationship" columns="2" rendered="{!NOT(ISBLANK(GRP_Fund_Relationship__c.Id))}">
<apex:inputField value="{!GRP_Fund_Relationship__c.Name}"/>
<apex:inputField value="{!GRP_Fund_Relationship__c.GRP_Start_Date__c}"/>
<apex:inputField value="{!GRP_Fund_Relationship__c.GRP_Account1__c}"/>
<apex:inputField value="{!GRP_Fund_Relationship__c.GRP_End_Date__c}"/>
<apex:inputField value="{!GRP_Fund_Relationship__c.GRP_Account2__c}"/>
<apex:inputField value="{!GRP_Fund_Relationship__c.GRP_Type_of_Relationship__c}"/>
</apex:pageBlockSection>
<apex:pageMessages />
<apex:pageBlockSection title="New Fund Relationship" columns="2" rendered="{!(ISBLANK(GRP_Fund_Relationship__c.Id))}">
<apex:inputField value="{!NewFundRelationship.GRP_Account1__c}"/>
<apex:inputField value="{!NewFundRelationship.GRP_Start_Date__c}"/>
<apex:inputField value="{!NewFundRelationship.GRP_Account2__c}"/>
<apex:inputField value="{!NewFundRelationship.GRP_End_Date__c}"/>
<apex:inputField value="{!NewFundRelationship.GRP_Type_of_Relationship__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
Controller:
public class GRP_EditNew_FundRelationshipCon {
public account acc {set;get;}
public string FundRelationshipID {get;set;}
public string AccountParentId {get;set;}
private ApexPages.StandardController controller;
public GRP_Fund_Relationship__c NewFundRelationship {set;get;}
public list<GRP_Fund_Relationship__c> FundRelationship {set;get;}
public list<GRP_Fund_Relationship__c> CheckFundRelationship {set;get;}
public GRP_EditNew_FundRelationshipCon (ApexPages.StandardController controller) {
this.controller = controller;
try{
AccountParentId = ApexPages.currentPage().getParameters().get('pid');
FundRelationshipID = ApexPages.currentPage().getParameters().get('id');
FundRelationship = [Select Name, GRP_Account1__c, GRP_Account2__c, GRP_Type_of_Relationship__c, GRP_Start_Date__c, GRP_End_Date__c
from GRP_Fund_Relationship__c where id =:FundRelationshipID];
NewFundRelationship = new GRP_Fund_Relationship__c(GRP_Account1__c = AccountParentId );
}catch(Exception ex){
system.debug('The following exception has occured in GRP_EditNew_FundRelationshipCon.GRP_EditNew_FundRelationshipCon --> ' + ex.getMessage() + ' || Row Number --> ' + ex.getLineNumber());
}
}
public pagereference SaveAndRedirect(){
try{
if(FundRelationship.size() > 0){
controller.save();
CheckFundRelationship = [Select GRP_Account1__c, GRP_Account2__c from GRP_Fund_Relationship__c where id =:FundRelationshipID];
system.debug('checkaccount 1---->'+CheckFundRelationship[0].GRP_Account1__c);
system.debug('checkaccount 2---->'+CheckFundRelationship[0].GRP_Account2__c);
if(checkFundRelationship[0].GRP_Account1__c == checkFundRelationship[0].GRP_Account2__c){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Account fields cannot have same Account Name'));
}else
{
system.debug('account 1---->'+FundRelationship[0].GRP_Account1__c);
system.debug('account 2---->'+FundRelationship[0].GRP_Account2__c);
acc = [select id from account where id = :FundRelationship[0].GRP_Account1__c];
}
}else
{
if(NewFundRelationship.GRP_Account1__c == NewFundRelationship.GRP_Account2__c){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Account fields cannot have same Account Name'));
}else
{
Insert NewFundRelationship;
acc = [select id from account where id = :NewFundRelationship.GRP_Account1__c];
}
}
PageReference ToAcc = new PageReference('/'+acc.Id);
return ToAcc;
}catch(Exception ex){
system.debug('The following exception has occured in GRP_EditNew_FundRelationshipCon.SaveAndRedirect --> ' + ex.getMessage() + ' || Row Number --> ' + ex.getLineNumber());
return null;
}
}
public pagereference CancelAndRedirect(){
try{
if(FundRelationship.size() > 0){
controller.cancel();
acc = [select id from account where id = :FundRelationship[0].GRP_Account1__c];
PageReference ToAcc = new PageReference('/'+acc.Id);
return ToAcc;
}else
{
controller.cancel();
PageReference ToAcc = new PageReference('/'+AccountParentId);
return ToAcc;
}
}catch(Exception ex){
system.debug('The following exception has occured in GRP_EditNew_FundRelationshipCon.CancelAndRedirect--> ' + ex.getMessage() + ' || Row Number --> ' + ex.getLineNumber());
return null;
}
}
}
Visualforce Page:
<apex:page standardController="GRP_Fund_Relationship__c" extensions="GRP_EditNew_FundRelationshipCon" lightningStylesheets="true">
<apex:form >
<div align="center" draggable="false">
<apex:commandbutton action="{!SaveAndRedirect}" Value="Save"/>
<apex:commandbutton action="{!CancelAndRedirect}" Value="Cancel" immediate="true" />
</div>
<apex:pageBlock mode="edit">
<apex:pageBlockSection title="Fund Relationship" columns="2" rendered="{!NOT(ISBLANK(GRP_Fund_Relationship__c.Id))}">
<apex:inputField value="{!GRP_Fund_Relationship__c.Name}"/>
<apex:inputField value="{!GRP_Fund_Relationship__c.GRP_Start_Date__c}"/>
<apex:inputField value="{!GRP_Fund_Relationship__c.GRP_Account1__c}"/>
<apex:inputField value="{!GRP_Fund_Relationship__c.GRP_End_Date__c}"/>
<apex:inputField value="{!GRP_Fund_Relationship__c.GRP_Account2__c}"/>
<apex:inputField value="{!GRP_Fund_Relationship__c.GRP_Type_of_Relationship__c}"/>
</apex:pageBlockSection>
<apex:pageMessages />
<apex:pageBlockSection title="New Fund Relationship" columns="2" rendered="{!(ISBLANK(GRP_Fund_Relationship__c.Id))}">
<apex:inputField value="{!NewFundRelationship.GRP_Account1__c}"/>
<apex:inputField value="{!NewFundRelationship.GRP_Start_Date__c}"/>
<apex:inputField value="{!NewFundRelationship.GRP_Account2__c}"/>
<apex:inputField value="{!NewFundRelationship.GRP_End_Date__c}"/>
<apex:inputField value="{!NewFundRelationship.GRP_Type_of_Relationship__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
Controller:
public class GRP_EditNew_FundRelationshipCon {
public account acc {set;get;}
public string FundRelationshipID {get;set;}
public string AccountParentId {get;set;}
private ApexPages.StandardController controller;
public GRP_Fund_Relationship__c NewFundRelationship {set;get;}
public list<GRP_Fund_Relationship__c> FundRelationship {set;get;}
public list<GRP_Fund_Relationship__c> CheckFundRelationship {set;get;}
public GRP_EditNew_FundRelationshipCon (ApexPages.StandardController controller) {
this.controller = controller;
try{
AccountParentId = ApexPages.currentPage().getParameters().get('pid');
FundRelationshipID = ApexPages.currentPage().getParameters().get('id');
FundRelationship = [Select Name, GRP_Account1__c, GRP_Account2__c, GRP_Type_of_Relationship__c, GRP_Start_Date__c, GRP_End_Date__c
from GRP_Fund_Relationship__c where id =:FundRelationshipID];
NewFundRelationship = new GRP_Fund_Relationship__c(GRP_Account1__c = AccountParentId );
}catch(Exception ex){
system.debug('The following exception has occured in GRP_EditNew_FundRelationshipCon.GRP_EditNew_FundRelationshipCon --> ' + ex.getMessage() + ' || Row Number --> ' + ex.getLineNumber());
}
}
public pagereference SaveAndRedirect(){
try{
if(FundRelationship.size() > 0){
controller.save();
CheckFundRelationship = [Select GRP_Account1__c, GRP_Account2__c from GRP_Fund_Relationship__c where id =:FundRelationshipID];
system.debug('checkaccount 1---->'+CheckFundRelationship[0].GRP_Account1__c);
system.debug('checkaccount 2---->'+CheckFundRelationship[0].GRP_Account2__c);
if(checkFundRelationship[0].GRP_Account1__c == checkFundRelationship[0].GRP_Account2__c){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Account fields cannot have same Account Name'));
}else
{
system.debug('account 1---->'+FundRelationship[0].GRP_Account1__c);
system.debug('account 2---->'+FundRelationship[0].GRP_Account2__c);
acc = [select id from account where id = :FundRelationship[0].GRP_Account1__c];
}
}else
{
if(NewFundRelationship.GRP_Account1__c == NewFundRelationship.GRP_Account2__c){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Account fields cannot have same Account Name'));
}else
{
Insert NewFundRelationship;
acc = [select id from account where id = :NewFundRelationship.GRP_Account1__c];
}
}
PageReference ToAcc = new PageReference('/'+acc.Id);
return ToAcc;
}catch(Exception ex){
system.debug('The following exception has occured in GRP_EditNew_FundRelationshipCon.SaveAndRedirect --> ' + ex.getMessage() + ' || Row Number --> ' + ex.getLineNumber());
return null;
}
}
public pagereference CancelAndRedirect(){
try{
if(FundRelationship.size() > 0){
controller.cancel();
acc = [select id from account where id = :FundRelationship[0].GRP_Account1__c];
PageReference ToAcc = new PageReference('/'+acc.Id);
return ToAcc;
}else
{
controller.cancel();
PageReference ToAcc = new PageReference('/'+AccountParentId);
return ToAcc;
}
}catch(Exception ex){
system.debug('The following exception has occured in GRP_EditNew_FundRelationshipCon.CancelAndRedirect--> ' + ex.getMessage() + ' || Row Number --> ' + ex.getLineNumber());
return null;
}
}
}
As you are using standard controlller, you can display the same error on visualforce page.
Hope it helps!