You need to sign in to do that
Don't have an account?
KevinRussell
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(); }
You cannot get the parent value from a trigger!
You have to do a seperate query for this !
Something like
and then
Make sure you appropriately bulkify your code
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:
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.
Kevin
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
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