2

Any idea how to write a Validation Rule, that prevents the user from creating more than one record a day.

I have one Object called (Stundenkonto__c) it shows a Month in Timetracking. I have a Object called (Zeiteintrag__c) it is the Master Detail of (Stundenkonto__c) So the User should only be able to create only one (Zeiteintrag__c) Record a day.

3
  • Just to clarify, do you want to prevent each User from preventing a new new Zeiteintrag__c each day, or do you want to prevent a more than one new Zeiteintrag__c related record from being created each day that's a child of Stundenkonto__c, regardless of who the user is? The solution to each problem is different.
    – crmprogdev
    Commented Jun 6, 2016 at 11:56
  • I want to prevent the User to create more than one a Day. Means on Any Stundenkonto__c (which is one Month) is only one Child Record per Business day. That is little Timetracking thing. Every user has his own Stundenkonto__c Commented Jun 8, 2016 at 6:52
  • Can users create records for just the current day, or can they choose the day that the record is for?
    – martin
    Commented Jun 8, 2016 at 13:00

3 Answers 3

1

You can do that with trigger. In your before insert trigger do something

trigger accTrigger on Zeiteintrag__c(before insert) {
   List<Zeiteintrag__c> zeiList = [SELECT Id FROM Account WHERE Zeiteintrag__c = TODAY]
  if(zeiList.size() > 0)
      trigger.new[0].addError('One record is already created');
}

With this small piece of code you can do this.

1

Now that you've clarified your requirements, it appears to me that you have several options available to you for doing this validation.

Personally, I'm not fond of the beforeInsert trigger solution. Since this is only on one object, you could add a field to the user object that keeps track of the LastCreatedDateZeiteintrag__c which could be used in a validation rule. Most fields on the user object can't be updated using a trigger, but they can be updated using workflow. Triggers also require periodic maintenance of code where workflow does not.

What I would recommend is comparing System.Now(), Today() or the TODAY value of the new CreatedDate (a date-time value) with the LastCreatedDateZeiteintrag__c on the User to create your validation rule. If the record passes, you'll then want to use workflow to update the LastCreatedDateZeiteintrag__c (either as a Date-time or Date, depending on your requirements) for that user with the new CreatedDate for the record. This method doesn't require you to maintain any code and equally important, gives you an error message that will appear where ever you want it to be located on your page.

Another option you'd have would be to create a controller extension that queries the user Zeiteintrag__c record along with user for the last record that user created and compares that with either Now() or Today(). If the number is less than one day, a page message error is generated, preventing the record from being saved.

6
  • by the way, what do you mean that the error message for a validation rule will appear where ever you want it to be located on your page
    – martin
    Commented Jun 8, 2016 at 13:01
  • @martin I may have been imprecise with that statement. It can appear either beside the field it applies to or at the top of the page. That is what I was referring to.
    – crmprogdev
    Commented Jun 8, 2016 at 13:05
  • Actually, the addError() method can be applied to a field as well.
    – martin
    Commented Jun 8, 2016 at 13:09
  • trigger Account on Account (after update) { Trigger.new[0].Name.addError('field error'); }
    – martin
    Commented Jun 8, 2016 at 13:10
  • In a trigger context, depending on how it's written, one record failing to insert/update because of it's validation test can cause ALL records in a bulk transaction to also fail. I don't view that as a desirable behavior. Again, as I mentioned above, triggers require maintenance. Any time a point & click solution can be used, I view that as preferable to using code.
    – crmprogdev
    Commented Jun 8, 2016 at 13:51
0

I found another solution.

  1. created a Tick box on Stundenkonto__c
  2. I created a Process builder, everytime a Zeiteintrag__c is created tick the box on Stundenkonto__c. After 13hours make the Tick box empty again.
  3. I created a Validation Rule Tickbox = TRUE && ISNEW()

So it is working in a very easy way

0

You must log in to answer this question.

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