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
Don WanigasundaraDon Wanigasundara 

Help with writing a trigger on a custom object that detects duplicates and resets the trigger every 24 hours.

I am trying to write a trigger on a custom object that detects duplicates and resets the trigger every 24 hours.  Custom object has a lookup field that looks up accounts, I want to make sure users dont select the same account and apdate the same account within a 24 hour period as I use this object as data collection tool.  
Best Answer chosen by Don Wanigasundara
pconpcon
This is a quick and dirty trigger that might do what you are asking.  I'm still not 100% clear on your request
 
trigger dupeChecker on Boiler_Percentage_Report__c (before insert) {
    Set<Id> facilityIds = new Set<Id>();

    for (Boiler_Percentage_Report__c bpr: Trigger.new) {
        facilityIds.add(bpr.Facility__c);
    }

    facilityIds.remove(null);

    if (!facilityIds.isEmpty()) {
        Map<Id, Boiler_Percentage_Report__c> bprMap = new Map<Id, Boiler_Percentage_Report__c>();

        for (Boiler_Percentage_Report__c bpr: [
            select CreatedDate,
                Facility__c
            from Boiler_Percentage_Report__c
            where BPR_Date__c = TODAY and
                Facility__c in :facilityIds
        ]) {
            bprMap.put(bpr.Facility__c, bpr);
        }

        for (Boiler_Percentage_Report__c bpr: Trigger.new) {
            if (bprMap.containsKey(bpr.Facility__c)) {
                bpr.addError('No duplicates');
            }
        }
    }
}

You can replace line 24 with whatever code you want to do

All Answers

pconpcon
What you are trying to do is a little fuzzy to me, so I'll try my best to guess at what you are looking to do and how I would solve it.  What I would do is in a before trigger, I would build up a set of account Ids, and then query your custom object for a CreatedDate (or LastModifiedDate) that equals TODAY [1].  Or if you want a rolling 24 hour period, you'll have to do some DateTime manipulation with .addDays(-1) and query for that.  Then if you get a result back do whatever you need to if that is throw and error or modify your incoming custom objects.

If this doesn't help you, please include an example workflow with your object (and it's field names).  And hopefully with that information we can get a clearer picture of what you are trying to accomplish.

[1] http://www.salesforce.com/us/developer/docs/officetoolkit/Content/sforce_api_calls_soql_select_dateformats.htm
Don WanigasundaraDon Wanigasundara
Thanks for the quick reply, I have the query I came up with below I wanna make sure useres dont select the same facility again by mistake within a day as this form(object) is updated daily for each facility.

SELECT CreatedDate,Facility__c FROM Boiler_Percentage_Report__c
WHERE BPR_Date__c = today
pconpcon
This is a quick and dirty trigger that might do what you are asking.  I'm still not 100% clear on your request
 
trigger dupeChecker on Boiler_Percentage_Report__c (before insert) {
    Set<Id> facilityIds = new Set<Id>();

    for (Boiler_Percentage_Report__c bpr: Trigger.new) {
        facilityIds.add(bpr.Facility__c);
    }

    facilityIds.remove(null);

    if (!facilityIds.isEmpty()) {
        Map<Id, Boiler_Percentage_Report__c> bprMap = new Map<Id, Boiler_Percentage_Report__c>();

        for (Boiler_Percentage_Report__c bpr: [
            select CreatedDate,
                Facility__c
            from Boiler_Percentage_Report__c
            where BPR_Date__c = TODAY and
                Facility__c in :facilityIds
        ]) {
            bprMap.put(bpr.Facility__c, bpr);
        }

        for (Boiler_Percentage_Report__c bpr: Trigger.new) {
            if (bprMap.containsKey(bpr.Facility__c)) {
                bpr.addError('No duplicates');
            }
        }
    }
}

You can replace line 24 with whatever code you want to do
This was selected as the best answer
Don WanigasundaraDon Wanigasundara
It works like a charm Thanks a lot but I seem to have trouble testing it any suggetions. 

@isTest
public class DupeCheckerTest   {
    static testMethod void BPRCreator() {
        Boiler_Percentage_Report__c acc = new Boiler_Percentage_Report__c();
        acc.Facility__c    = 'test3';
        acc.Number_Boilers_that_are_down__c = 1;
        insert acc;
    }
}
pconpcon
This should be your most basic test.  I would also do a test for a Boiler_Percentage_Report__c and a Boiler_Percentage_Report__c that has a BPR_Date__c of today().addDays(-1).  I did not know what you needed to create the Facility record, so you'll probably need to add some additional fields.
 
@isTest
public class DupeCheckerTest {
    static testMethod void noDupeTest() {
        Facility__c testFacility = new Facility__c(
            Name = 'Test Facility'
        );
        insert testFacility;

        Boiler_Percentage_Report__c testBPR1 = new Boiler_Percentage_Report__c(
            Facility__c = testFacility.Id,
            Number_Boilers_That_Are_Down__c = 1,
            BPR_Date__c = Date.today()
        );
        insert testBPR1;

        Boiler_Percentage_Report__c testBPR1 = new Boiler_Percentage_Report__c(
            Facility__c = testFacility.Id,
            Number_Boilers_That_Are_Down__c = 1
        );

        Test.startTest();

        try {
            insert testBPR2;
        } catch (System.DmlException e) {
            System.assert(
                e.getMessage().contains('No duplicates'),
                'Did not get the right message back ' + e.getMessage()
            );
        }

        Test.stopTest();
    }
}

 
Don WanigasundaraDon Wanigasundara
Deployed and working I initially got a error because i didnt include my test class in the change set, thanks a lot... 
pconpcon
Great!  Glad to hear it's working for you.  If you don't mind, please choose the best answer so that this question can be marked as solved.  This will remove it from the unanswered queue as well as make it easier for people to find the answer in the future.