I want an example how to compare a new record with old records in order to prevent duplicated records from being inserted.

For example: When I insert a new 'Time Off', I compare the new start date and end date if there is already a requested time off, then an error message will be displayed as:

This Start / End date is already asked. Please choose another start/end date

I've created a field named: WFError__c in my object as unique field (TEXT type), then I created a workflow on my custom object (Time_Off__c) there for I created a field update action on that WFError__c with this formula value TEXT( Start_date__c ) + TEXT( End_date__c ).

  • welcomes to salesforce.Stackexchange Kawtar. Have you already tried to write something in Apex yourself ? It would be meaningful to see that. Some more information on your datamodel and business concept may also help. You can edit the question to add more details. May 5, 2014 at 8:15
  • I want to use trigger for that matter, to display the error, but I'm not using a VisualForce page, I'm working with a standard page. I have in mind that oldMap.keySet() and newMap.keySet() obtain the diffetent ID's (old and new records) of an object, but I need an example to see how it works. I have a custom object: Time_Off__c custom fields: Start_date__c and End_date__c.
    – user8214
    May 5, 2014 at 9:07
  • There is an APEX trigger recipe - developer.force.com/cookbook/recipe/… worth checking.
    – cropredy
    May 5, 2014 at 17:07

1 Answer 1


If you don't want to do this with triggers, you could achieve it by creating a new text field as unique and use field update workflow to fill this field by the concatenation of your Start and End date. The error will be something standard like the following image : Image

  • Thank you for the idea! but I'm working on Date field here, not a Text. And trigger operate just in update and delete action. in my case it would be a before insert action
    – user8214
    May 5, 2014 at 10:06
  • You can concatenate the date or the date time in a workflow using the TEXT() function. and triggers can be fired on before insert event too May 5, 2014 at 10:11
  • I'm talking about the use of oldMap, it's working just on update and delete event. but I still can't get your answer. can you please explain it further more!
    – user8214
    May 5, 2014 at 10:31
  • @melmoussaoui It's tempting to use unique text fields but in that one I'd have to carefully test it. I predict some "fun" when you cast DateTime to string like that - will it respect timezones?
    – eyescream
    May 5, 2014 at 10:34
  • @KawtarBrazi OldMap in triggers you could use it only to compare records stats before and after your update @ eyescream that what I'm afraid from. May 5, 2014 at 10:39

Your Answer

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