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
KevinRussellKevinRussell 

How to reference current Contact record to update custom field.

I want to set a checkbox on the current contact record.

My trigger queries a related Interaction object to perform a record count.

After counting records,I set the check box either True or False.

I can't figure how to properly reference the current Contact record to update the checkbox field,

 

I'm getting this error:

Compile Error: Invalid field contact for SObject Contact at line 19 column 31

 

On this line:

contacts.put(record.contact, new contact(id = record.id, Attended_Event_Within_Past_Year__c = TRUE));                    

 

The problem reference: "record.contact"

 

Thanks for any help.

 

Kevin

 

 

The entire trigger.

trigger ContactsAttendedEventPastYear on Contact (after update) {
    try {
    // Your code here
    
        date todaysDate = date.today();
        date CutoffDate = todaysDate.addDays(-365);
        
        // Store Contact record ID
        map< id, contact > contacts = new map< id, contact >();
           
        //look through the data
        For (Contact record : trigger.new)
        {
        
            Integer recordcount = [select count() from Interaction__c where Contact__c = :record.id  AND Start_Date_Time__c > :CutoffDate and Selected_Sub_type__c = 'Attend Event' AND (Interaction_Type__c = 'Alumni Interaction' OR Interaction_Type__c = 'Advancement Interaction' )];
         
            if(recordcount >= 1)
            
                 contacts.put(record.contact, new contact(id = record.id, Attended_Event_Within_Past_Year__c = TRUE));                     
            Else {
                 //contacts.put(record.contact__c, new contact(id=record.contact__c, Attended_Event_Within_Past_Year__c = FALSE));    
            
            }               
            
        //update contacts.values(); 
        
        }
        
    } catch (Exception e) {
    // Generic exception handling code here

    }
}

 

Rahul_sgRahul_sg

you need to use Record.ID not record.contact

s_k_as_k_a

Hi Kevin

 

Here is the code for you requoirement. 

 

trigger ContactsAttendedEventPastYear on Contact (before update) {
       
	   // Contact map
        map< id, contact > contacts = new map< id, contact >();
           
        //look through the data
        For (Contact con : trigger.new)
        {
			contacts.put(con.id,con)
		}

		Map<Id , Integer> conToIntaCount = new Map<Id , Integer>();
		for(aggregateResult ar : [select count(Id) reccount , contact__c  from Interaction__c where Contact__c in :contacts.keySet() AND Selected_Sub_type__c = 'Attend Event' AND Start_Date_Time__c != LAST_N_DAYS:365 AND (Interaction_Type__c = 'Alumni Interaction' OR Interaction_Type__c = 'Advancement Interaction')])
		{
			conToIntaCount.put((id)ar.get('contact__c'), (integer)ar.get('reccount'));
		}

		for(Id contId : contacts.keySet())
		{
			if(conToIntaCount.containsKey(contId)) 
			{
				if(conToIntaCount.get(contId) >= 1)
				    contacts.get(contId).Attended_Event_Within_Past_Year__c = true;
				else
					contacts.get(contId).Attended_Event_Within_Past_Year__c = false;
			}
		}
	}

 

KevinRussellKevinRussell
Thanks s_k_a, I tried the code and I'm getting this error: Compile Error: Field must be grouped or aggregated: Contact__c at line 13 column 34. Not exactly sure how to resolve it.
s_k_as_k_a

Kevin,

 

Replace soql query like this

 

select count(Id) reccount , contact__c  from Interaction__c where Contact__c in :contacts.keySet() AND Selected_Sub_type__c = 'Attend Event' AND Start_Date_Time__c != LAST_N_DAYS:365 AND (Interaction_Type__c = 'Alumni Interaction' OR Interaction_Type__c = 'Advancement Interaction') group by contact__c

 

KevinRussellKevinRussell
Thanks s_k_a. I got past that hurdle. The last piece I'm trying to get going is writing to the Contact field itself. I'm getting the error:
Compile Error: Invalid field Attended_Event_Within_Past_Ye​ar__c for SObject Contact
That's coming from this line:
contacts.get(contId).Attended_Event_Within_Past_Ye​ar__c = true;
KevinRussellKevinRussell
Thanks Rahul_sg, Your suggestion worked, my code compiled. Unfortunately my code itself is looping on itself since I'm triggering on update, but updating the same record. Not very smart logic on my part!!!!!
s_k_as_k_a

yOU CAN CHECK THE API NAME OF THE FIELD  ON CONTACT OBJECT.