-1

Attaching a piece of code below which checks the creation of one record per day. I want to make this code run for checking the creation of one record for a week. No two records can be created in a week.

trigger RecordDuplicateTrigger on Time_In__c (before insert) {
      for(Time_In__c a:Trigger.new)
       {
        string uid = userinfo.getName();
         List<Time_In__c> acc=[select Id, CreatedDate, Owner.Name, Start_Date_del__c from Time_In__c where (Owner.Name = :uid AND CreatedDate = Today)];
             if(acc.size()>0)
             {
               a.adderror('One record is already created for the Day For ' + uid);
             }
       }
   }
1
  • 1
    just change the date filter to check in last 7 days, Are you facing any challenge? Apr 27, 2020 at 11:26

3 Answers 3

0
trigger RecordDuplicateTrigger on Time_In__c (before insert) {
  for(Time_In__c a:Trigger.new)
   {
    string uid = userinfo.getName();
     List<Time_In__c> acc=[select Id, CreatedDate, Owner.Name, Start_Date_del__c from Time_In__c where (Owner.Name = :uid AND CreatedDate = THIS_WEEK)];
         if(acc.size()>0)
         {
           a.adderror('One record is already created for the Day For ' + uid);
         }
   }

}

1
  • what chnages could be made to the code such that it allows more than one record in a week but not with the similar Start Date? (more than one records with different start dates) Apr 30, 2020 at 13:02
0

You can put the soql query outside for loop. Try something like below

trigger RecordDuplicateTrigger on Time_In__c (before insert) {
string uid = userinfo.getName();
integer Max_Records = 1;
List<Time_In__c> acc=[select Id, CreatedDate, Owner.Name, Start_Date_del__c from Time_In__c where (Owner.Name = :uid AND CreatedDate = LAST_N_DAYS:7)];
total = acc.size();
  for(Time_In__c a:Trigger.new)
   {
         if(total == Max_Records)
         {
           a.adderror('One record is already created for the Day For ' + uid);
           break;
         }
         else {
             total = total+1;
         }

   } }

It's good to Max_records = 1 in the custom setting / custom metadata type and make this as configurable.

0

A few things to get out of the way first...

First, use the user Id, not the user's name. This may cause problems down the road.

Second, do not place a query in a loop, as that can cause large data loads to fail.

Third, you should clarify if you meant the last seven days, or this week, which it sounds like what you'd actually want to do.

Given those, here's how we can fix this:

trigger RecordDuplicateTrigger on Time_In__c (before insert) {
  Set<Id> ownersThisWeek = new Set<Id>();
  Map<Id, Time_In__c> matches = new Map<Id, Time_In__c>();
  // Prevent duplicates in the same trigger //
  for(Time_In__c record: Trigger.new) {
    if(record.OwnerId == null) {
      record.OwnerId = UserInfo.getUserId();
    }
    if(!ownersThisWeek.add(record.OwnerId)) {
      record.addError('You may not have more than one card in a week.');
    } else {
      matches.put(record.OwnerId, record);
    }
  }
  // Check for duplicates in the database
  for(Time_In__c record: [SELECT OwnerId FROM Time_In__c WHERE CreatedDate = THIS_WEEK AND OwnerId = :matches.keySet() FOR UPDATE]) {
    Time_In__c newRecord = matches.get(OwnerId);
    if(newRecord != null) {
      newRecord.addError('You may not have more than one card in a week.');
    }
  }
}

This change prevents duplicates that may sneak in because of bulk data loads, as well as duplicates from the existing week. Change THIS_WEEK to LAST_N_DAYS:7 if you wish to change the behavior from limiting one record between Sun-Sat to the last seven days.

1
  • what chnages could be made to the code such that it allows more than one record in a week but not with the similar Start Date? (more than one records with different start dates) Apr 30, 2020 at 13:01

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