0

I have a requirement in the Case object, we have a field called Delivery number if that value is duplicate the error message should be shown and the record should gets saved after that error message.

We have written a below Apex class and calling that class through trigger, but we are unable to save the record, since it is a standard object and using the standard save button I am unable to override the button.

Apex Class :

public class Dupclass{

private final Case c;

public void checkdups(List<Case> ca, Map<Id, Case> oldMap) {

    Map<String, Case> caseMap = new Map<String, Case>();

    for (Case csold : ca) {

       // if((csold.FrCCC_Delivery__c!= null)&& (csold.FrCCC_Delivery__c == oldMap.get(csold.id).FrCCC_Delivery__c)){
            if(caseMap.ContainsKey(csold.FrCCC_Delivery__c) && (csold.FrCCC_Delivery__c ==caseMap.get(csold.id).FrCCC_Delivery__c)){

                //System.debug('The case already has the same delivery number');
               csold.FrCCC_Delivery__c.addError('The case already has the same delivery number');
                 //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'The case already has the same delivery number')); 

            }else{
                caseMap.put(csold.FrCCC_Delivery__c, csold);
            }
      //  }

        for(Case csdup: [SELECT CaseNumber, FrCCC_Delivery__c FROM Case WHERE FrCCC_Delivery__c IN : caseMap.keySet()]){
            Case newCase= caseMap.get(csdup.FrCCC_Delivery__c);


            csold.FrCCC_Delivery__c.addError('The case ' + csdup.CaseNumber + ' already has the same delivery number');

        }

    } 
}

}

Trigger :

trigger Dupclass on Case(before insert,before update) 
{
  new Dupclass().checkdups(trigger.new, trigger.oldMap);
}
1
  • 1
    Please visit Trailhead to learn the basics of trigger. A trigger doesn't work that way, one of the way is to create a custom page and notify user with a duplicate record exist message and the perform necessary action.
    – Raul
    Jan 9, 2018 at 9:22

2 Answers 2

2

Yes, @RahulSharma is correct. The trigger won't work that way, however, you can try the following workaround for same:

  1. Without current screen interruption. Create a checkbox field on Case object as 'DeDupe Delivery number' and instead of throwing an error just check that boolean from your before trigger if its duplicate. It will keep the track of such duplicate Delivery number records and the record will save successfully.

  2. With current screen interruption. Create the same checkbox and by default its false. Add an additional condition to your trigger execution logic that Delivery number field is duplicate or 'DeDupe Delivery number' is false then display error. The error will say "Duplicate value for Delivery number found. If you still want to save the record then check DeDupe Delivery number field". If the user checks'DeDupe Delivery number' field it will save the record successfully.

  3. As @RahulSharma mentioned in his comment "Create a custom page and notify the user with a duplicate record exist message and perform necessary action."

0

Unfortunately there is no addWarning method that allows information to be communicated back to the UI yet also commit the transaction.

Its always seemed like a big piece of missing functionality to me. I can only assume that the lack of that relates to the actual internal implementation where e.g. errors are passed between processes via an exception and there is no way to communicate successful completion and a warning message together. But that is speculation.

1
  • Thank you all for your suggestions. @Abhishek we have already tried with the option by creating the new checkbox field if the duplicate found and if the user clicks the checkbox the field will save but the business is not ready to check the field manually they want the field should be hidden and the error should be shown after which they need to save the record.
    – Meena
    Jan 9, 2018 at 12:35

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