3

I have implemented workflow rule in maintaining unique name in the Account object everything is fine except that the error message that I'm getting I want to customize to give more meaningful error message then the below message, what do I need to do for that?

duplicate value found: Asset_Name_Dupe__c duplicates value on record with id: 0019000000CZC83

:EDIT:

I have created Workflow rules by following this:

At first let us create a new Field on the Account object.

  1. Go to Setup | Customize | Accounts | Fields.
  2. Scroll down to Custom Fields & Relationships Section.
  3. Click New Field.
  4. Select the Type as: Text
  5. Name it as: Account Name Dupe Check[Account_Name_Dupe_Check, 255].
  6. Check the Option: Unique and also select: 'Treat "ABC" and "abc" as different values (case sensitive)'.
  7. Make it Visible for the appropriate Profiles. While you make them Visible also check the Option: 'Read Only'.
  8. You need not put them on the Page Layouts.

Now, let us create a new Workflow Rule[Account Dupe Check].

  1. Go to Setup | Create | Workflows & Approvals | Workflow Rules.
  2. Click New Rule.
  3. Select the Object: Account
  4. Evaluation Criteria: created and every time it's edited.
  5. Rule Criteria: formula evaluates to true.

OR( ISNEW(), ISCHANGED(Name) )

  1. Save & Next.
  2. From under Immediate Actions, click Add Workflow Action to select 'Field Update'[Set the Account Name].
  3. Select the Field to Update: Account Name Dupe Check
  4. Select: Use a formula to set the new value
  5. In the formula box, type in 'Name'.
  6. Click Save.
  7. Click Done.
  8. Activate the WFR.

PS: I do not have apex code yet.

2
  • I am confused, you said this is a WFR but you have a tag of Apex. What are you using to manage duplicates? Can you share your Apex if that is what you are using? Or share the WFR configuration via adding snapshots to your original post.
    – cmmoutes13
    Commented Jul 15, 2016 at 18:20
  • I have updated my question and show you the steps I have taken to create WFR but I have no apex code yet, the reason I have added tag just in case :)
    – Nick
    Commented Jul 15, 2016 at 18:23

3 Answers 3

6

No, you can't custom the error message for field uniqueness constraints.

However, you can prevent accounts with duplicate names and provide custom error messages using duplicate rules. Watch the Managing Duplicates Records with Duplicate Rules for a good tutorial/overview. As duplicate rules are designed for this particular purpose they have lots of features that would be useful for you (alert vs. report, block vs. allow, error messages, fuzzy matching).

For a specific implementation, see @nick_kahn's answer for how he set up his duplicate rule.

7
  • I thought about this one but was too lazy to vet it. Will it actually override (run before) the duplicate validation that's already in place?
    – Adrian Larson
    Commented Jul 15, 2016 at 20:01
  • no, it runs after system validations, before triggers, and user validations, you'd need to remove the uniqueness constraint, see developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… Commented Jul 15, 2016 at 20:06
  • So it won't actually customize the error message then? Unless you make the field not actually "unique" at the database level. You should update your answer to reflect that constraint.
    – Adrian Larson
    Commented Jul 15, 2016 at 20:07
  • if the goal is to prevent duplicates and provide useful error message to the user, database constraints aren't the best method to accomplish this as they don't have the proper functionality, watch the youtube video, it's short and super useful Commented Jul 15, 2016 at 20:10
  • I agree it's not a good idea, I'm just saying it's the only way your solution will actually get him to his end goal. Much improved suggestion with the rewording.
    – Adrian Larson
    Commented Jul 15, 2016 at 20:11
4

Unfortunately you are unable to modify an error message that will come from using the unique function on a text field. One thing you might want to think about doing instead of a WFR is to do this check for duplicates with an apex trigger something like the following:

trigger manageAccountDuplicates on Account (before insert, before update){

    List<String> uniqueValues = new List<String>();
    for(Account record : Trigger.new){
        uniqueValueList.add(record.Unique_Value__c);
    }

    Map<String,Account> uniqueValueMap = new Map<String,Account>();
    for(Account record : [
        SELECT Unique_Value__c FROM Account
        WHERE Unique_Value__c IN :uniqueValueList
    ]){
        uniqueValueMap.put(record.Unique_Value__c, record);        
    }

    for(Account record : Trigger.new){
        if(uniqueValueMap.containsKey(record.Unique_Value__c)){
            if(trigger.isInsert || (trigger.isUpdate && record.id<>uniqueValueMap.get(record.Unique_Value__c).id)){
                record.addError('An account matching this criteria already exists');
            }           
        }
    }
}

Also you will be able to add more criteria to this just add a + and anymore fields you would like it to check on after the parts that have acc.name

5
  • I was typing up pretty much the same thing but you beat me to it. +1
    – Adrian Larson
    Commented Jul 15, 2016 at 18:37
  • You could optimize it a bit, but this approach is the right one.
    – Adrian Larson
    Commented Jul 15, 2016 at 18:38
  • Haha yeah I am still working on being better at optimizing my code. Still a lot to learn lol
    – cmmoutes13
    Commented Jul 15, 2016 at 18:38
  • arrggg, i was trying to away from trigger but i see not many options left for me
    – Nick
    Commented Jul 15, 2016 at 18:48
  • If you want to do something before validations occur, it's your only option.
    – Adrian Larson
    Commented Jul 15, 2016 at 19:01
1

Thanks @Ralph Callaway for showing us the new technique. after I go through the video and I manage to compile the steps involved in creating a duplicate rules

Enjoy!

Step 1: creating MATCHING RULES:

Setup--> Data.com Administration

-->Duplicate Management
-->Matching Rules (Click)
-->New
-->Select the object 
--> Rule Name: "Unique Name matching rule" (of whatever it suites your needs)
--> Field: Name
--> Matching Method: Exact
-->Save/Activate

Step 2: creating DUPLICATE RULES:

Setup--> Data.com Administration

-->Duplicate Management
-->Duplicate Rules (Click)
-->New Rule
-->Select the object 
-->Rule Name: "Unique Name" (of whatever it suites your needs)
-->Record-Level Security: Enforce sharing rules
-->Action on Create/Edit: Block
-->Compare Merchants With: <select your object>
-->Matching Rule: select the matching rules you have created in the STEP 1
-->Save/Activate
1
  • 1
    If @Ralph's solution worked for you, make sure you accept it!
    – Adrian Larson
    Commented Jul 18, 2016 at 15:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .