function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dfiredfire 

trigger to check for existing record

I am writing a trigger that, among other things, is supposed to check if a record exists for my custom object Statistic__c with the same date and level as the one trying to be inserted.

 

Simply I want to grab the date and level from the trigger do a SOQL to find existing records and return an error to the record trying to be inserted/updated if a match is found. No problem, right?

 

Well as I am trying to bulkify, this is getting much more complicated. The issue is once I have my list of existing records how I then sync up to the list of statistic__c to send errors to the proper one. So I thought to create a map where the key = date+level and the value is the sobject. So that would work fine, I think except what if in the bulk upload are 2 records that have the same date and level. When creating the map, the 2nd record will overwite the first, so the 2nd will be found to return an error, but not the first. So does this mean I need a map with a key of date+level and the value a LIST of Statistic__c? That is what I am thinking, but this seems very complicated.

 

Perhaps I am overlooking some tip or trick to more efficiently do this.

 

Here is the code I have so far:

 

trigger UpdateDateStatistics on Statistic__c (before insert, before update, after delete, after insert, after undelete, 
after update) {

if(Trigger.isBefore) {
List<Date> dateList = new List<Date>();
List<Id> levelList = new List<Id>();
Map<String,Statistic__c> smap = new Map<String,Statistic__c>();

for(Statistic__c s : Trigger.new) {
dateList.add(s.date__c);
levelList.add(s.level__c);
             smap.put(s.date__c.format()+s.level__c,s);
         }
        List<Statistic__c> existing = [select id, date__c, level__c from Statistic__c where date__c in :dateList and level__c in :levelList];
        for(Statistic__c e : existing) {
             Statistic__c se = smap.get(e.date__c.format()+e.level__c);
            se.addError('A Record already exists for this Level and Date. Please edit that record or choose an unused date and level combinations');
}
} else { // isAfter
 // do some other stuff not relavant here
}
}

 

 

Thanks.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
DCBoyDCBoy

We have used the unique field concept that Kritin has mentioned above with the only change being we used a workflow field update instead of a trigger to set the unique field.

All Answers

kritinkritin

On this object Statistic__c you need to crate a field like Unique of Text 50 size , and also set property while creating this field.

Do not allow duplicate values checkbox to true. And set this value through trigger while inserting/updating the records. It will automatically genaerates an error b'Coz of uniquness.

 Suppose you have created ine field like Unique__c on Statistic__c.

 

Then create a trigger

 

trigger UpdateUniqueStatistics on Statistic__c (before insert,before update){

 

for(Statistic__c s : Trigger.new) {
                        s.Unique__c=s.date__c.format()+s.level__c;
                }

}

 

 

 

DCBoyDCBoy

We have used the unique field concept that Kritin has mentioned above with the only change being we used a workflow field update instead of a trigger to set the unique field.

This was selected as the best answer
dfiredfire

great idea and simple. thanks!