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
DaveStDaveSt 

Apex trigger update problem

Hi,
I am attempting to write a trigger on a custom object but it doesn't seem to being updating - my logic must be flawed in some way. The basic premise is that I have 2 checkbox fields that are updated depending on if a date field ( on the same object) falls within the bounds of either THIS_QUARTER or NEXT_QUARTER.  My code looks like this:
 
trigger SM_Ident_Quarter on ScheduleMirror__c (before insert, before update) {
   
   for(ScheduleMirror__c SchedMirr : Trigger.New){
     
     // if the schedule date is in This Quarter then update the this quarter flag
     for( ScheduleMirror__c s: [SELECT ID, Current_Quarter__c, Next_quarter__c, schedule_date__c  from schedulemirror__c where schedule_date__c = THIS_QUARTER]){
         if(s.id != null)
            s.Current_Quarter__c = TRUE;
     }
     // if the schedule date is in Next Quarter then update the next quarter flag
     for( ScheduleMirror__c sm: [SELECT ID, Current_Quarter__c, Next_quarter__c, schedule_date__c  from schedulemirror__c where schedule_date__c = NEXT_QUARTER]){
         if(sm.id != null)
            sm.Next_quarter__c = TRUE;    
     }
 }
}
I feel this should be quite simple but I think I am missing something fundamental.
Any help would be much appreciated,

Thanks
Dave
 
Best Answer chosen by DaveSt
EldonEldon
Hi Dave,

Try this code then.
 
trigger SM_Ident_Quarter on ScheduleMirror__c (after insert, after update) {
    
    
    
    list <ScheduleMirror__c> ScheduleMirrorList = new list<ScheduleMirror__c>();
    
    for( ScheduleMirror__c s: [SELECT ID, Current_Quarter__c, Next_quarter__c, 
                               schedule_date__c  from schedulemirror__c where schedule_date__c = THIS_QUARTER]){
                                   if(s.id != null){
                                       ScheduleMirror__c tempsched = s; 
                                       tempsched.Current_Quarter__c = TRUE;
                                       ScheduleMirrorList.add(tempsched);
                                   }
                               }
    
    
    for( ScheduleMirror__c sm: [SELECT ID, Current_Quarter__c, Next_quarter__c, 
                                schedule_date__c  from schedulemirror__c where schedule_date__c = NEXT_QUARTER]){
                                    if(sm.id != null){
                                        ScheduleMirror__c tempsched = sm; 
                                        tempsched.Next_quarter__c = TRUE;  
                                        ScheduleMirrorList.add(tempsched);
                                    }
                                }
    update ScheduleMirrorList;
    
}



Regards


 

All Answers

bob_buzzardbob_buzzard
One point is that when you insert a record it doesn't exist in the database, so your queries will only retrieve records inserted in the past. Thus if you insert record A, there will be nothing returned by the query. When you insert record B, the query will return record A and you can then update it.

However, these periods are dynamic so presumably by tomorrow all of your current quarter/next quarter flags may be incorrect anyway. It sounds like you want a schedueld job to process these records every night and update their status accordingly.
SFDC RJSFDC RJ
And i don't think you need if(sm.id != null) and if(s.id != null) in before update trigger , as id will not be generated before entering to the database.
bob_buzzardbob_buzzard
@SFDC RJ - they are SOQL date literals - surprised you haven't seen them before if you are advising people on Apex : 

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm
SFDC RJSFDC RJ
@bob 
I haven't noticed that :( sorry
EldonEldon
Hi Dave,

Try this code,
 
trigger SM_Ident_Quarter on ScheduleMirror__c (before insert, before update) {
    
    for(ScheduleMirror__c SchedMirr : Trigger.New){
        
        // if the schedule date is in This Quarter then update the this quarter flag
        for( ScheduleMirror__c s: Trigger.new){
            if(s.schedule_date__c == THIS_QUARTER){
                s.Current_Quarter__c = TRUE;
            }
        }
        // if the schedule date is in Next Quarter then update the next quarter flag
        for( ScheduleMirror__c sm: trigger.new){
            if(schedule_date__c == NEXT_QUARTER){
                sm.Next_quarter__c = TRUE;   
            }
        }
    }
}

Let me know if it solved your problem.

Regards
 
DaveStDaveSt
Hi Eldon,
Thanks for the suggestion but it complains about the date literal being used in this context. 

"Variable does not exist: THIS_QUARTER"

Do I have to define this as a formula upfront and reference the variable?

Thanks
Dave
EldonEldon
Hi Dave,

Try this code then.
 
trigger SM_Ident_Quarter on ScheduleMirror__c (after insert, after update) {
    
    
    
    list <ScheduleMirror__c> ScheduleMirrorList = new list<ScheduleMirror__c>();
    
    for( ScheduleMirror__c s: [SELECT ID, Current_Quarter__c, Next_quarter__c, 
                               schedule_date__c  from schedulemirror__c where schedule_date__c = THIS_QUARTER]){
                                   if(s.id != null){
                                       ScheduleMirror__c tempsched = s; 
                                       tempsched.Current_Quarter__c = TRUE;
                                       ScheduleMirrorList.add(tempsched);
                                   }
                               }
    
    
    for( ScheduleMirror__c sm: [SELECT ID, Current_Quarter__c, Next_quarter__c, 
                                schedule_date__c  from schedulemirror__c where schedule_date__c = NEXT_QUARTER]){
                                    if(sm.id != null){
                                        ScheduleMirror__c tempsched = sm; 
                                        tempsched.Next_quarter__c = TRUE;  
                                        ScheduleMirrorList.add(tempsched);
                                    }
                                }
    update ScheduleMirrorList;
    
}



Regards


 
This was selected as the best answer
DaveStDaveSt
HI - yes, the code worked but because of the interaction of  batch job updating the object the trigger fires on I was again reaching my limits.  I have incorporated the trigger logic into my batch process which works and is a more elegant solution, Thanks for your help though.