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 Contact field in trigger of child record update

Hello, I need to know how to reference a Contact field within the trigger of a child object.  I need to reference the Contact field in an IF THEN statement.

 

Thanks!

 

Kevin

 

Here is more detail:

Trigger needs to reference Customer field and custom object.

I'm doing a record count on an object related to Contacts: npe5__Affiliation__c
This works great.  However, I need to use a Customer field in my IF statement.
I want to change it from:

if(recordcount >= 1)
to various versions of:
if(recordcount >= 1 && Customer.Guest_Speaker__c = TRUE) 


Integer recordcount = [select count() from npe5__Affiliation__c where npe5__contact__c = :record.npe5__contact__c AND Type__c = 'Speaker'];

if(recordcount >= 1)
{
	// Update checkbox field on the Contact record to TRUE                
	contacts.put(record.npe5__contact__c, new contact(id=record.npe5__contact__c, Guest_Speaker__c = TRUE)); 
Else {
       // Update checkbox field on the Contact record to TRUE                
	contacts.put(record.npe5__contact__c, new contact(id=record.npe5__contact__c, Guest_Speaker__c = FALSE));                
	}
	update contacts.values();   
}

 

 

 

 

 

Avidev9Avidev9

You cannot get the parent value from a trigger!

You have to do a seperate query for this !

 

Something like

 

customer__c  customer =[SELECT Guest_Speaker__c,Id FROM Cutomer__c WHERE Id=:record.npe5_contact__c];

 and then

if(recordcount >= 1 && Customer.Guest_Speaker__c = TRUE) 

 

Make sure you appropriately bulkify your code

 

Daniel Zeidler (DZ)Daniel Zeidler (DZ)

This code doesn't look like it is bulkified so you may run in to issues associated with having non-bulkified code. However, the quickest way to modify this code to meet your requirement would probably be to add your conditions to the query (instead of the IF statement) with something like:

 

Integer recordcount = [select count() from npe5__Affiliation__c where npe5__contact__c = :record.npe5__contact__c AND Type__c = 'Speaker' AND npe5_contact__r.Guest_Speaker__c = TRUE];

 

It is also worth noting that the logic in the trigger can probably be replicated declaratively using a rollup summary field and some workflow rules. Let us know if you want some help doing this declaratively or if you want help bulkifying your code.

KevinRussellKevinRussell
Thanks for the replies. I'll try to wrap my head around this and get back to you.

Kevin
KevinRussellKevinRussell

Yea... guess I do need help...

 

So, my full working trigger is below. 

How could I change this so that I could reference the Contact field: Guest_Speaker__c using the existing If statement or more bulkified with the Contact.Guest_Speaker__c in the actual query?

 

Thanks for the help!

 

Kevin

 

 

// Update Contact field: "Guest_Speaker__c" when a npe5__Affiliation__c record is inserted, updated or Deleted. 
trigger Contact_Affiliation_GuestSpeaker_checkbox_test on npe5__Affiliation__c  (after insert, after update, after delete) {

try {
// Your code here
        
       // Will store Contact record ID
       map< id, contact > contacts = new map< id, contact >();

             
if (trigger.isinsert || trigger.isupdate)
    {

       // Create trigger for new or selected npe5__Affiliation__c record record
       for(npe5__Affiliation__c record:trigger.new)        
       {
        
           Integer recordcount = [select count() from npe5__Affiliation__c where npe5__contact__c = :record.npe5__contact__c  
           AND Type__c = 'Speaker'];
                                
           if(recordcount >= 1)
                
                // Update checkbox field on the Contact record to TRUE
                contacts.put(record.npe5__contact__c, new contact(id=record.npe5__contact__c, Guest_Speaker__c = TRUE)); 
           Else {
                // Update checkbox field on the Contact record to TRUE
                contacts.put(record.npe5__contact__c, new contact(id=record.npe5__contact__c, Guest_Speaker__c = FALSE));     
           }     
           
            update contacts.values(); 

        }   
    }

if (trigger.isdelete) 
    {
            
       // Create trigger for new or selected npe5__Affiliation__c record record
       for(npe5__Affiliation__c record:trigger.old)        
       {
        
           Integer recordcount = [select count() from npe5__Affiliation__c where npe5__contact__c = :record.npe5__contact__c  
           AND Type__c = 'Speaker'];
                
           if(recordcount >= 1)
                
                // Update checkbox field on the Contact record to TRUE
                contacts.put(record.npe5__contact__c, new contact(id=record.npe5__contact__c, Guest_Speaker__c = TRUE)); 
           Else {
                // Update checkbox field on the Contact record to TRUE
                contacts.put(record.npe5__contact__c, new contact(id=record.npe5__contact__c, Guest_Speaker__c = FALSE));     
           }     
           
            update contacts.values(); 

        }   
    }

            } catch (Exception e) {
    // Generic exception handling code here

}    
}

 

 

 

Daniel Zeidler (DZ)Daniel Zeidler (DZ)

There are more efficiant solutions but hopefully this will get you going in the right direction. I did not check this for bugs so there are probably a few mistakes

 

trigger Contact_Affiliation_GuestSpeaker_checkbox_test on npe5__Affiliation__c  (after insert, after update, after delete) {

Set<Id> contactsToCheck = new Set<Id>();

if(!trigger.isInsert)
	for(npe5__Affiliation__c na : trigger.old)
		contactsToCheck.add(na.npe5__contact__c);

if(!trigger.isDelete)
	for(npe5__Affiliation__c na : trigger.new)
		contactsToCheck.add(na.npe5__contact__c);
	
List<Contact> contactList = [SELECT Guest_Speaker__c, (SELECT Id FROM npe5__Affiliations__r WHERE Type__c = 'Speaker' LIMIT 1) FROM Contact WHERE Id IN :contactsToCheck];

for(Contact c : contactList)
{
	// in this loop you can reference c.Guest_Speaker__c which will contain the value returned by the query, unless modified
	
	if(c.npe5__Affiliations__r.size() > 0)
		c.Guest_Speaker__c = true;
	else
		c.Guest_Speaker__c = false;
}

update contactList;

}