+ Start a Discussion
Manjunath reddy 25Manjunath reddy 25 

trying avoid duplicates using workflow, getting system generated message ie duplicate value found: Unique_Key__c duplicates value on record with id: aPOP00000008OOD

I want to display a custom error message if my workflow rule fires and fails. Basically, I am trying to avoid duplicate dates. I have got my workflow running, but right now it displays the following message:
"duplicate value found: Unique_Key__c duplicates value on record with id: a04j0000002bTQs"

To achieve that,I am using the below ateps and code.

1) Write controller and override the Save method
2) In save method use try cache block and show whatever message I want to show
try{
    update myObj;
}
catch(Exception exc)
{
    if(exc.getMessage().contains('FIELD_FILTER_VALIDATION_EXCEPTION'))
    {
        Trigger.new[0].addError('Please select only active entities.');
    }
}
still I am not getting custom message.

My controller is as below
/**
* @author : HP 
* @date 30/08/2016 
* @description : GSDAccountteammembercontroller class is used to insert the records into 'HPE_Account_Team__c' object.
* $client$ [gsd|spt|common]: gsd 
//Class Header Starts
//Class Name:[GSDAccountteammembercontroller]
---------------------------------------------------------------------------------------------------------------------------------
//        DateTime[UTC]      |  Email_id                        |Stream     |   Release         |     Description
---------------------------------------------------------------------------------------------------------------------------------
//        30/08/2016[04:30]  |~:saran-kumar-satya.b-v@hpe.om    | CSC | GSDR9-SFDC      |   GSDAccountteammembercontroller class is used to insert the records into 'HPE_Account_Team__c' object
//Class Header Ends

**/

public  class GSDAccountteammembercontroller {


public list<HPE_Account_Team__c> listofrecords{get;set;}
public Boolean TabToBeClosed {get;set;}
    
public PageReference SaveRecords() 
{
    string parentrecordid = ApexPages.currentPage().getParameters().get('Account__c');
    list<HPE_Account_Team__c> insertlist = new list<HPE_Account_Team__c>();  
    for(HPE_Account_Team__c acctem :listofrecords)
    {
        system.debug('acctem.Name__c'+acctem.Name__c);
        system.debug('parentrecordid'+parentrecordid);
        system.debug('listofrecords'+listofrecords);
        if(acctem.Name__c!=null)
        {
          if(parentrecordid != null && parentrecordid != '')
          acctem.Account__c = parentrecordid ;
          insertlist.add(acctem);
        
        }              
       
    }
        try{
        insert insertlist;
        }
        catch(Exception ex){
         
         if(ex.getMessage().contains('FIELD_FILTER_VALIDATION_EXCEPTION')) {
             Trigger.new[0].addError('Please select only active entities.');
         }
         ApexPages.addMessages(ex);
         return null;
        }       
        if(listofrecords!=null){
        listofrecords.clear();
        }
        TabToBeClosed = true;
        return null;

}

public GSDAccountteammembercontroller(ApexPages.StandardController controller){
 HPE_Account_Team__c HPEAccountTeam=(HPE_Account_Team__c)controller.getrecord();
 system.debug('HPEAccountTeam'+HPEAccountTeam);
TabToBeClosed = false;

listofrecords = new list<HPE_Account_Team__c>();
    
    for(Integer i=0; i<5; i++){   
         HPE_Account_Team__c gsdaccteam = new HPE_Account_Team__c();
         gsdaccteam.Account__c=HPEAccountTeam.Account__c;        
         gsdaccteam.Name__c=null;
         gsdaccteam.Role_Scope_Description__c='';
         gsdaccteam.Role__c='';
                 
         listofrecords.add(gsdaccteam);
    
         }
         
        
     
} 
  
}

 
SandhyaSandhya (Salesforce Developers) 
Hi,
 
if(exc.getMessage().contains('FIELD_FILTER_VALIDATION_EXCEPTION'))
    {
        Trigger.new[0].addError('Please select only active entities.');
    }

Since your error message is in if condition please check if you are entering in if loop by just printing the value using system.debug('exc.getMessage()' +exc.getMessage()) it will help you to debug your code.

Hope this helps you!

Thanks and Regards
Sandhya