You need to sign in to do that
Don't have an account?
KevinRussell
SOQL Query record count with Date field filter
I want to query the selected Contact's Interaction records with a Sub_type__c = 'Attend Event' AND Start_Date_Time__c within one year of the current date.
If I have a record count = 0 I know there has not been a record of attending an Event in the past year. I will set the Contact checkbox to FALSE.
If I have a record count of >= 1, they came to an event within the past year. I will set the Contact checkbox to TRUE.
Can anyone give me a hand for the counting records part of the code? Here is what I have so far:
Thanks
Kevin
// Update Contact field: "Attended_Event_Within_Past_Year__c" to TRUE when a new Interaction__c record is inserted or updated. trigger Contacts_Attended_Event_Within_Past_Year_Checkbox_test on Interaction__c (after insert, after update) { // Will store Contact record ID map< id, contact > contacts = new map< id, contact >(); integer recordcount = 0; // Create trigger for new or selected Interaction__c record for(Interaction__c record:trigger.new) //I want to query the selected Contact's Interaction records with a Sub_type__c = 'Attend Event' AND Start_Date_Time__c within one year of the current date. //If I have a record count = 0 I know there has not been a record of attending an Event in the past year. I will set the Contact checkbox to FALSE. //If I have a record count of >= 1, they came to an event within the past year. I will set the Contact checkbox to TRUE. if(recordcount >= 1) // Update checkbox field on the Contact record to TRUE contacts.put(record.contact__c, new contact(id=record.contact__c, Attended_Event_Within_Past_Year__c = TRUE)); Else { // Update checkbox field on the Contact record to TRUE contacts.put(record.contact__c, new contact(id=record.contact__c, Attended_Event_Within_Past_Year__c = FALSE)); } update contacts.values(); }
You want to avoid SOQL in the loop (the pseudo code you put seems like it would be querying inside the trigger contexts loop).
To query on records in the last year, you can use the LAST_N_DAYS date literal field expression. See here for more details on the date literals:
http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_select_dateformats.htm
Lastly, here's an attempt at what you're asking for... (note I didn't use the LAST_YEAR date literal as based off what I read, it's not what you want)