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
Scott ParrishScott Parrish 

Time-based Workflow & After Update Trigger

I hope I explain this correctly: I have a time-based workflow rule that is supposed to kick off an hour after a new record is created. An automated process creates these records and more than one record may be created by the automated process.

When the time-based workflow rule executes it updates a single checkbox. When the checkbox is checked an After Update Trigger executes. The After Update Trigger checks to see if the newly insert record is a duplicate of an existing record. If it is a duplicate, the trigger updates the existing record with data from the duplicate record. A copy of the duplicate record is sent to another object to record that a duplicate did exist. The duplicate record is then deleted.

If there is only one newly inserted record everything works as it should. However, if there are two or more newly inserted records only the first record is processed as it should. The second record is also processed by the Time Based Workflow and the field update occurs; however, the After Update Trigger never executes against the second record. I have the debug log, but have no way of posting it here as it is too long.

Karan Khanna 6Karan Khanna 6
could you post your trigger here?
Scott ParrishScott Parrish
I cannot paste it here. It's more than 32k characters. The trigger executes fine against a single record though. Am I not understanding something correctly? Shouldn't it work something like this:
1. Time-based workflow rule updates checkbox on first record
2. Time-based workflow rule updates checkbox on second record
3. Time-based workflow rule updates checkbox on third record
4. After Update Trigger executes on first record and completes
5. After Update Trigger executes on second record and completes
6. After Update Trigger executes on third record and completes.
Karan Khanna 6Karan Khanna 6
could you confirm in your trigger you are not using Trigger.new[0], instead of Trigger.new?


Scott ParrishScott Parrish
Following is a portion of my trigger. As you can see there are times that I use the index. So how do I change this to get the desired results?

trigger BMCRF_GetDuplicate on BMCServiceDesk__BMC_BaseElement__c (after update) {

    BMCServiceDesk__BMC_BaseElement__c[] bEleOld = trigger.old;
    BMCServiceDesk__BMC_BaseElement__c[] bEleNew = trigger.new;
    string bEleSerial = bEleNew[0].BMCServiceDesk__SerialNumber__c;
    string bEleUniqueId = bEleNew[0].BMCServiceDesk__UniqueCISourceID__c;
    string bEleToken = bEleNew[0].BMCServiceDesk__TokenId__c;
    string bEleInstId = bEleNew[0].BMCServiceDesk__InstanceID__c;
    String tmpName = bEleNew[0].BMCServiceDesk__Name__c.substringBefore('-INST');
   
    System.Debug(tmpName);

        If (bEleOld[0].NN_DuplicateCheck__c == false && bEleNew[0].NN_DuplicateCheck__c == true && bEleNew[0].BMCServiceDesk__ClassName__c == 'BMC_ComputerSystem') {  
        integer i = [SELECT count() FROM BMCServiceDesk__BMC_BaseElement__c
                                    WHERE BMCServiceDesk__Name__c LIKE: tmpName + '%'
                                    AND BMCServiceDesk__InstanceID__c !=: bEleInstId];
        If (i == 1) {
              
        LIST<BMCServiceDesk__BMC_BaseElement__c> oldBEle = [SELECT Id, BMCServiceDesk__InstanceID__c
                                                            FROM BMCServiceDesk__BMC_BaseElement__c
                                                            WHERE BMCServiceDesk__Name__c LIKE: tmpName + '%'
                                                            AND BMCServiceDesk__InstanceID__c !=: bEleInstId LIMIT 1];
        If (bEleSerial == 'NONE' || bEleSerial == null) {                                                  
       BMCServiceDesk__BMC_BaseElement__c updoldBEle = [SELECT Id, BMCServiceDesk__InstanceID__c,
                                                            BMCServiceDesk__AssemblyId__c,
                                                            BMCServiceDesk__Model__c,
                                                            BMCServiceDesk__SerialNumber__c,
                                                            BMCServiceDesk__Version_Number__c,
                                                            BMCServiceDesk__ManufacturerName__c,
                                                            BMCServiceDesk__TokenId__c
                                                            FROM BMCServiceDesk__BMC_BaseElement__c                                                          
                                                            WHERE BMCServiceDesk__Name__c LIKE: tmpName + '%'
                                                            AND BMCServiceDesk__InstanceID__c !=: bEleInstId LIMIT 1];
        updoldBEle.BMCServiceDesk__AssemblyId__c = bEleNew[0].BMCServiceDesk__AssemblyId__c;
        updoldBEle.BMCServiceDesk__Model__c = bEleNew[0].BMCServiceDesk__Model__c;
        updoldBEle.BMCServiceDesk__Version_Number__c = bEleNew[0].BMCServiceDesk__Version_Number__c;
        updoldBEle.BMCServiceDesk__ManufacturerName__c = bEleNew[0].BMCServiceDesk__ManufacturerName__c;
        updoldBEle.BMCServiceDesk__TokenId__c = bEleUniqueId;
        update updoldBEle;                                                                                        
         }
Karan Khanna 6Karan Khanna 6
Hi Scott - it is clear that your code works only for one record at a time, what you need here is 'Bulkification', you need to iterate through all the records in trigger.new. Below articles can guide you with that:

https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code
http://www.sfdc99.com/2014/01/18/bulkifying-code/