I have written a trigger which prevent duplicate name. please refer following condition.

"Test" and "Te st" are same name (which mean don't need to consider any in between spaces.) "TEST" and "test" are same name (which mean ignore uppercases and lower cases.)

My Question :

If I have already one record inserted in system with name "Te st". and Now I am trying to insert a new record with name "test"(which is obvious not correct as per duplication).

How can i handle this exception?

Thank you in advance.

  • What have you tried so far? What characters should not be included in the matching or is it only spaces that should be ignored? – Girbot Sep 8 '14 at 9:47
  • only spaces and capital letter. I have tried by like query which is but obvious not giving me a proper result. – Dharmik Sep 8 '14 at 10:05

The generic approach to this problem is to add an extra field populated via a trigger where you store the normalised version of the name. So for "Te st" you would store "test". (By doing this via a trigger you can have an index on the normalised name.)

Then when new records are being inserted you can do this sort of query to detect duplicates:

select Id, NormalizedName__c
from SomeObject__c
where NormalizedName__c in :newNormalizedNames

so that the hard work is done by the database.

  • Yes, I think that will work. Thank you Keith. i will let you know if i need your help. – Dharmik Sep 8 '14 at 10:10

The easiest way is to add an error to the record (or a particular field) and then this cause an exceptiion which will bubble upto the user layer. eg.

Trigger.new[0].name__c.addError('duplicate name found.');

have a look here for some documentation http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject.htm#apex_System_SObject_field_addError

  • The Question is not about how to add error it's about how to prevent such kind of condition when "If I have already one record inserted in system with name "Te st". and Now I am trying to insert a new record with name "test"(which is obvious not correct as per duplication)." – Dharmik Sep 8 '14 at 9:35
  • You stated that the trigger was already written which is why I answered as I did. – Richard Durrant Sep 8 '14 at 10:09

Let's assume that inputString is your input and Custom_object__c is the object where you want to create a new record. Something like this should do it:

String inputString;
String modifiedString;

    modifiedString = inputString.replaceAll('\\s+','').toLowerCase();

List<Custom_object__c> cObjectList = new List<Custom_object__c>([SELECT Id, Name_field__c FROM Custom_object__c]);

for(Custom_object__c co: cObjectList){
            // Return error

Note that the select statement should be outside the trigger and this solution works only if you do not have a lot of data to select in Custom_object__c (max 50.000 in a query). Note that this is not the optimal solution.

Edit: I came up with another idea. Let's assume that inputString is your input string.

String inputString='Test';
String queryString='%';
String[] chars;
    chars = inputString.split('');
for(String s: chars){
List<Custom_object__c> cObjectList = Database.query('SELECT Id, Name_field__c FROM Custom_object__c WHERE Name_field__c LIKE '+queryString+'');
if(cObjectList!=null && cObjectList.size()>0){
    // Return error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.