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
lainchburylainchbury 

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

 

 

Jake GmerekJake Gmerek

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:

trigger OnThisTenancyReferencing on TLM__Occupancy__c (after insert, after update) {
List<ID> ids = new List<ID>();
for(TLM__Occupancy__c OnTenancy :Trigger.new){
	ids.add(OnTenancy.id);
}

List <TLM__Tenant__c> Tenant = new List<TLM__Tenant__c>([Select Name from TLM__Tenant__c WHERE TLM__Occupancy__c IN :ids]);
	

}

 I may not have all the names exactly right, but that should be pretty close.

Shashikant SharmaShashikant Sharma

If you just want tio know how to get parent list

trigger OnThisTenancyReferencing on TLM__Occupancy__c (after insert, after update) {


Set<ID> setTenantID = new Set<ID>();

for(TLM__Occupancy__c OnTenancy :Trigger.new){
	
	//TLM__Tenant__c should be you relationship field
	setTenantID.add(OnTenancy.TLM__Tenant__c);
}

List<TLM__Tenant__c> Tenant =  [Select Name from TLM__Tenant__c WHERE Id IN :setTenantID];
}

 

 

 

Proper syntax for this trigger with optimized script statements as well would be like this

 

trigger OnThisTenancyReferencing on TLM__Occupancy__c (after insert, after update) {


Set<ID> setTenantID = new Set<ID>();
MAP<ID , ID> mapOccupancyID_TenantID = new MAP<ID , ID>();
for(TLM__Occupancy__c OnTenancy :Trigger.new){
	
	//TLM__Tenant__c should be you relationship field
	setTenantID.add(OnTenancy.TLM__Tenant__c);
        mapOccupancyID_TenantID.put(TLM__Tenant__c , TLM__Tenant__c);
	
}

MAP<ID , TLM__Tenant__c> mapTenant =  [Select Name from TLM__Tenant__c WHERE Id IN :setTenantID];
for(TLM__Occupancy__c OnTenancy :Trigger.new){
	    
TLM__Tenant__c tenantObj = mapTenant.get(mapOccupancyID_TenantID.get(OnTenancy.id));

//Add your logic here
	
}

//any DML insert or update should be after for loop
}

 

I hope it helps you , please ask if any issue

 

lainchburylainchbury

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!

 

 

trigger OnThisTenancyReferencing on TLM__Occupancy__c (after insert, after update) {

List<ID> ids = new List<ID>();

for(TLM__Occupancy__c OnTenancy :Trigger.new){
    ids.add(OnTenancy.TLM__Tenant__r.id);
}

List <TLM__Tenant__c> Tenant = [Select Name from TLM__Tenant__c WHERE id IN :ids];

for(TLM__Tenant__c li :Tenant){
    
    List<TLM__Reference__c> References = [SELECT Name from TLM__Reference__c where TLM__Tenant__r.id IN :Tenant];
    
    for(TLM__Reference__c rli :References){
        
        rli.TLM__HasUpdated__c=TRUE;
        
    }
    
    update References;  
}
    

}

 

 

Jake GmerekJake Gmerek

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:

trigger OnThisTenancyReferencing on TLM__Occupancy__c (after insert, after update) {

List<ID> ids = new List<ID>();

for(TLM__Occupancy__c OnTenancy :Trigger.new){
    ids.add(OnTenancy.TLM__Tenant__r.id);
}

List <ID> TenantIDs = new List <ID> TenantIDs();

for (TLM__Tenant__c t: [Select Name from TLM__Tenant__c WHERE id IN :ids]){
    TenantIDs.add(t.id)
}

List<TLM__Reference__c> References = [SELECT Name from TLM__Reference__c where TLM__Tenant__r.id IN :TenantIDs];
    
    for(TLM__Reference__c rli :References){
        
        rli.TLM__HasUpdated__c=TRUE;
        
    
    
    update References;  
}
    

}

 Hope it works for you!

lainchburylainchbury

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...

 

trigger OnThisTenancyReferencing on TLM__Occupancy__c (after insert, after update) {


Set<ID> setTenantID = new Set<ID>();

for(TLM__Occupancy__c OnTenancy :Trigger.new){
	
	//TLM__Tenant__c should be you relationship field
	setTenantID.add(OnTenancy.TLM__Tenant__r);
}

List<TLM__Tenant__c> Tenant =  [Select Name from TLM__Tenant__c WHERE Id IN :setTenantID];

for(TLM__Tenant__c li :Tenant){
	li.TLM__HasUpdated__c=TRUE;
	
}


}

 

lainchburylainchbury

Thanks Jake

 

Tried this but I'm getting an error saying 'Incompatible element type SOBJECT:TLM__Tenant__c for collection of id'.

 

:?

Jake GmerekJake Gmerek
trigger OnThisTenancyReferencing on TLM__Occupancy__c (after insert, after update) {

List<ID> ids = new List<ID>();

for(TLM__Occupancy__c OnTenancy :Trigger.new){
    ids.add(OnTenancy.TLM__Tenant__r.id);
}

List <ID> TenantIDs = new List <ID> TenantIDs();

for (TLM__Tenant__c t: [Select id from TLM__Tenant__c WHERE id IN :ids]){
    TenantIDs.add(t.id)
}

List<TLM__Reference__c> References = [SELECT Name from TLM__Reference__c, TLM_HasUpdated__c where TLM__Tenant__r.id IN :TenantIDs];
    
    for(TLM__Reference__c rli :References){
        
        rli.TLM__HasUpdated__c=TRUE;
        
    
    }
    update References;  

    

}

 A couple of changes in there.

Shashikant SharmaShashikant Sharma
trigger OnThisTenancyReferencing on TLM__Occupancy__c (after insert, after update) {

List<ID> ids = new List<ID>();

for(TLM__Occupancy__c OnTenancy :Trigger.new){
    ids.add(OnTenancy.TLM__Tenant__r.id);
}

List <TLM__Tenant__c> Tenant = [Select Name from TLM__Tenant__c WHERE id IN :ids];

for(TLM__Tenant__c li :Tenant){
    
    List<TLM__Reference__c> References = [SELECT Name from TLM__Reference__c where TLM__Tenant__r.id =: li.id];
    
    for(TLM__Reference__c rli :References){
        
        rli.TLM__HasUpdated__c=TRUE;
        
    }
    
    update References;  
}
    

}

 Even though it is not written for bulk handling but you can try it once

Shashikant SharmaShashikant Sharma

I think best syntax is this

trigger OnThisTenancyReferencing on TLM__Occupancy__c (after insert, after update) {

List<ID> ids = new List<ID>();

for(TLM__Occupancy__c OnTenancy :Trigger.new){
    ids.add(OnTenancy.TLM__Tenant__r.id);
}


List<TLM__Reference__c> References = [SELECT Name from TLM__Reference__c, TLM_HasUpdated__c where TLM__Tenant__r.id IN :ids];
    
    for(TLM__Reference__c rli :References){
        
        rli.TLM__HasUpdated__c=TRUE;
        
    
    }
    update References;  

    

}

 

 

We don't need this part as set Ids also contain the same set of id  for TLM__Tenant__c records

 



List <ID> TenantIDs = new List <ID> TenantIDs();

for (TLM__Tenant__c t: [Select id from TLM__Tenant__c WHERE id IN :ids]){
    TenantIDs.add(t.id)
}