function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
daniel.duartedaniel.duarte 

Customizing Duplicate Value Error Message

Hello,

I want an unique combination of fields, so I have a hidden text field that concatenates the values of three fields and this text field is set as unique.The link below explains it better:

http://www.forcetree.com/2010/07/unique-field-combination-in-salesforce.html

When the user tries to create a record with an existing combination of fields, he gets an error message. But I need this message to be intuitive, I need to customize it. If I can't change the displayed error message, this solution won't work for me.

Is there a way of customizing the Salesforce standard error messages?

If not (I'm guessing there's no way), would anyone think of another way of achieving this unique field combination?

Thanks.

kyle.tkyle.t

You may want to look into writing a trigger for this.  the trigger would query the database for that value and if it exists already you would throw an error, which can be any thing you want.  I have not tested this at all (just wrote it up on in a text editor, but this is the basic idea

 

 

trigger PreventAccountDuplicate on Account (before insert, before update){
	////////////
	//Get all of the unique values (I took a guess that you were using Name, State and Country
	///////////
	List<String> uniqueValueList = new List<String>();
	for(Account a : Trigger.new){
		uniqueValueList.add(a.Name + a.State + a.Country);
	}
	
	///////////
	//find any acccounts that contain the unique value and add them to a Map
	///////////
	List<Account> acctList = [select id, name, Unique_Value__c where Unique_Value__c IN :uniqueValueList];
	
	Map<String,Account> uniqueValueMap = new Map<String,Account>();
	for(Account a : acctList){
		uniqueValueMap.put(a.Unique_Value__c,a);		
	}
	
	///////////
	//loop through the accounts in the trigger and see if our unique value was found
	//if so, check whether we are inserting or updating and if updating that the unique value is from another record
	//Thorw error if duplicate
	///////////
	for(Account a : Trigger.new){
		if(uniqueValueMap.containsKey(a.Name + a.State + a.Country)){
			//the second condition here is for the update... allows you to update the record, but not ANOTHER record
			if(trigger.isInsert() || (trigger.isUpdate() && a.id<>uniqueValueMap.get(a.Name + a.State + a.Country).id)){
				a.addError('An account matching this criteria already exists');
			}			
		}
	}
}

 

 

 

 

daniel.duartedaniel.duarte

Thanks for your help. It didn't work, though.

When I create a record, before the insert action, the fields are empty. So, this part of the code:

 

List<String> uniqueValueList = new List<String>();
	for(Account a : Trigger.new){
		uniqueValueList.add(a.Name + a.State + a.Country);
	}

adds to the String List nullnullnull. I realized that using the System Log and some system.Debug() calls.

How can I get the values of the fields in a "before insert" Trigger??

Thanks

 

kyle.tkyle.t

Hi,

  I tested this in a dev enviornment and should work... I created a formula field called Uniquie Value which concatenated Name + BillingCity + BillingCountry (obviously you can change this to meet your busines rulles).

 

Here is the code.

 

trigger PreventAccountDuplicate on Account (before insert, before update){
    ////////////
    //Get all of the unique values (I took a guess that you were using Name, State and Country
    ///////////
    List<String> uniqueValueList = new List<String>();
    for(Account a : Trigger.new){
        System.Debug('==='+a.Name + a.BillingState + a.BillingCountry+'===');
        uniqueValueList.add(a.Name + a.BillingState + a.BillingCountry);
    }
    
    ///////////
    //find any acccounts that contain the unique value and add them to a Map
    ///////////
    List<Account> acctList = [select id, name, Unique_Value__c from Account where Unique_Value__c IN :uniqueValueList];
    
    Map<String,Account> uniqueValueMap = new Map<String,Account>();
    for(Account a : acctList){
        uniqueValueMap.put(a.Unique_Value__c,a);        
    }
    
    ///////////
    //loop through the accounts in the trigger and see if our unique value was found
    //if so, check whether we are inserting or updating and if updating that the unique value is from another record
    //Thorw error if duplicate
    ///////////
    for(Account a : Trigger.new){
        if(uniqueValueMap.containsKey(a.Name + a.BillingState + a.BillingCountry)){
            //the second condition here is for the update... allows you to update the record, but not ANOTHER record
            if(trigger.isInsert || (trigger.isUpdate && a.id<>uniqueValueMap.get(a.Name + a.BillingState + a.BillingCountry).id)){
                a.addError('An account matching this criteria already exists');
            }           
        }
    }
}

 

 

MC34MC34
Hi Kyle, can you suggest me wheer is the message part in your code? VF page embedded? Thanks Mit.