0

On case object we have a field Delivery Number(Text) whenever user enters that value if that number is duplicate. We need to show pop-up/error message (basically inform user) that it is already exists and should get saved in salesforce.

Validation rules,Triggers,Unique field checked are all prevent the record saving in salesforce.In this case i want to save it in salesforce.

So i have tried a trigger and apex class.

Trigger :

trigger trgr_Case_DuplicateCheck on Case(before Insert, before Update) {
    new DuplicateCase().checkDuplicates(trigger.new);
}

Apex Class:

public class DuplicateCase{
    private Apexpages.Standardcontroller controller;

    public void checkDuplicates(List<Case> lstCase){


        Map<String, Id> mapCase= new Map<String, Id>();

        Set<String> setCase = new Set<String>();
        for(Case ca: lstCase)
            setCase .add(ca.Delivery__C);

         for(Case c : lstCase)
            setCase .add(c.CaseNumber);


        for(
            Case ca:
            [
                SELECT Id, Delivery__c FROM Case WHERE  Delivery__C IN : setCase 
            ]
        )
            mapCase.put(ca.Delivery__c, ca.Id);

            for(
            Case c:
            [
                SELECT Id, CaseNumber FROM Case WHERE  Delivery__C IN : setCase 
            ]
        )

            mapCase.put(c.CaseNumber, c.Id);


        for(Case ca: lstCase)
            if(
                mapCase.containsKey(ca.Delivery__c) &&
                mapCase.get(ca.Delivery__c) != ca.Id
            )
                ca.addError(
                    'The case '+ca.CaseNumber+' has already same Delivery number'   ,
                    true
                                      );   
    }


}
0

I would suggest you move to the declarative functionality Salesforce provide for duplicate detection. You can use Duplicate Management to create Duplicate Rules. For each Duplicate Rule, you can have many Matching Rules.

Duplicate Rule: Can block users from saving records that have been identified as possible duplicates, or simply alert users that they may be creating a duplicate, but allow them to save the record anyway.

Matching Rule: Can specify that if the Email and Phone values of two records match exactly, the records might be duplicates.

The following is an example of a Duplicate Rule where I am preventing the Save action from executing by notifying the User of a duplicate and giving them an option to continue with Save or to use the existing record. This works well with fields that are key identifiers of a record such as SSN or Employee ID. enter image description here

The Matching Rule that is used in the Duplicate Rule is as follows.

enter image description here

And the warning message that is displayed when a duplicate is detected looks like:

enter image description here

Don't forget to activate both your Duplicate Rule and Matching Rule before attempting to test, can catch you out!

  • 1
    Feature is not available for Cases. – NITHESH K Dec 6 '17 at 13:22
1

There is a native Salesforce feature called Duplicate Management. It's a no-code solution for your problem. Check this Trailhead Module for mor information.

It also states:

Users can click Save (ignore alert) on the comparison page when manually cleaning a record to ignore duplicate alerts and save the record.

So it would perfectly fit your needs.

EDIT:

Unfortunately it is not available for Cases (yet) but there is already an idea: Duplicate Management for Cases

  • 2
    Is matching Rule is available for case also ? I am not pretty sure about this so. – NITHESH K Dec 6 '17 at 12:52
  • @NITHESHK thanks for your comment, I double checked an it is not :( but there is an idea, I updated my answer. – Basti Dec 6 '17 at 13:19
0

You can't display an alert from a trigger -- a trigger doesn't have any direct access to the user interface. You could instead write a logic in that trigger itself to insert a "case comment" which says "Duplicate Number".

Just after you save the record in your trigger, you can use the following code to insert a case comment on the respective case.

CaseComment newComment = new CaseComment();
newComment.CommentBody = "Duplicate Number";
newComment.IsPublished = TRUE;
newComment.ParentId = case.id;
insert newComment;

Also go through the link which has similar discussion in stackoverflow

How to display a message when the trigger finishes?

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.