You need to sign in to do that
Don't have an account?
lainchbury
What's the correct syntax to find a parent object within a trigger?
Hi
I'm writing a trigger which fires on an object (Occupancy__c), which is a child of another object (Tenant__c).
When the trigger fires, I want to be able to make a list of the parent objects (Tenant__c) in the trigger.
I can't work out the syntax to retrieve the list of Tenant__c objects. Here is my code...
trigger OnThisTenancyReferencing on TLM__Occupancy__c (after insert, after update) { for(TLM__Occupancy__c OnTenancy :Trigger.new){ List <TLM__Tenant__c> Tenant = [Select Name from TLM__Tenant__c WHERE TLM__Tenant__c.Id IN :Trigger.new]; } }
Any help would be much appreciated.
Thanks,
Paul
OK, first of all this
TLM__Tenant__c.Id IN :Trigger.new
will not work, it is expexting a list of type ID and you are giving it a list of type SObject. Here is what you need:
I may not have all the names exactly right, but that should be pretty close.
If you just want tio know how to get parent list
Proper syntax for this trigger with optimized script statements as well would be like this
I hope it helps you , please ask if any issue
Thanks, I tried that and it didn't quite work as it created a list of ids that contained the ids of Occupancy objects. I need to collect the Ids of the parent objects so amended the code as follows. It produces no results. Any ideas why?
Thanks for your help!
Again this:
TLM__Tenant__r.id IN :Tenant]
is getting you in trouble same as the first section of code, just different object. TLM__Tenant__r.id this is type 'ID' and this 'Tenant' is a List of Type 'TLM__Tenant__c'. The Tenant list contains 0 ID's and so there are no matches.
Try this instead:
Hope it works for you!
Hi Shashikant
I tried what you suggested, as per below but I get an error saying that 'Incompatible element type SOBJECT:TLM__Tenant__c for collection of Id'. The name of my relationship field from Occupancy__c to Tenant__c is Tenant__r. Any ideas?
Thanks for your help...
Thanks Jake
Tried this but I'm getting an error saying 'Incompatible element type SOBJECT:TLM__Tenant__c for collection of id'.
:?
A couple of changes in there.
Even though it is not written for bulk handling but you can try it once
I think best syntax is this
We don't need this part as set Ids also contain the same set of id for TLM__Tenant__c records