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
brielea1984brielea1984 

Trigger for updating field in child records

I have three objects: General_Intakes__c, Contact, and Households__c

 

The Households__c is the Parent of Contact. the Contact has a lookup field called Household_Id_del__c to the Household.

 

The General_Intakes__c is the child of both Households and Contacts,  with lookup fields called Primary_Contact__c and Primary_Household_Id__c. 

 

I was trying to write a trigger, which I am fairly new at, that when the Household_Id_del field is changed in the Contact object, the Primary_Household_Id__c field on the General_Intakes__c is updated to reflect the same.

 

Essentially, Primary_Household_Id__c must be the same as Primary_Contact__r.Household_Id_del__c.

 

I tried copying some code I found in these boards, but I keep getting this error: 

 

Error: Compile Error: Didn't understand relationship 'Households__r' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 9 column 42

 

Here is the code: 

trigger GeneralIntakeHHId on Contact (after update) {
    
    for( Contact parent: Trigger.new)
    {
    
        List<General_Intakes__c> children = [ SELECT Primary_Household_ID__c from 
                                               General_Intakes__c where Primary_Contact__r.id = :parent.Id];

        list <Households__c> household = [Select Household.Id
                                            from Households__c
                                            where Household.id = :parent.Household_ID_del__c];                                                       
        
        List<General_Intakes__c> childrenToUpdate = new List<General_Intakes__c>();
        
        for(General_Intakes__c thischild: children )
        {
           if( thischild.Primary_Household_Id__c !=  household[0].Id)
           {
               thischild.Primary_Household_Id__c =  household[0].Id;
               childrenToUpdate.add(thischild);
           }
        }
        
        if( !childrenToUpdate.isEmpty())
        {
           update childrenToUpdate;
        }
    
    }                       
}

 The error is for list <Households__c> household = [Select Household.Id

 

Any help would be much appreciated.

 

Best Answer chosen by Admin (Salesforce Developers) 
brielea1984brielea1984

I found coding that worked:

 

trigger updateGeneralIntake on Contact (after update) {
    List < Id > ConIds = new List < Id >();
    List < Id > GIIds = new List < Id >();
    for(Contact con: Trigger.New) {    
        if(con.Household_ID_del__c != null){    
           List<General_Intakes__c> proObj = [select p.Id,p.Primary_Household_ID__c from General_Intakes__c p where p.Primary_Contact__c =:con.Id];
           if(proObj.size() > 0){
               for(General_Intakes__c pc: proObj ){
                   pc.Primary_Household_ID__c = rem.Household_ID_del__c;
                   pc.Household_Update__c = 'Completed' ;
                   update pc;
               }
           }          
        }      
    }
}

 

All Answers

arizonaarizona

The problem is that you named your object as Households__c so now you have to use Householdss__c

brielea1984brielea1984

I found coding that worked:

 

trigger updateGeneralIntake on Contact (after update) {
    List < Id > ConIds = new List < Id >();
    List < Id > GIIds = new List < Id >();
    for(Contact con: Trigger.New) {    
        if(con.Household_ID_del__c != null){    
           List<General_Intakes__c> proObj = [select p.Id,p.Primary_Household_ID__c from General_Intakes__c p where p.Primary_Contact__c =:con.Id];
           if(proObj.size() > 0){
               for(General_Intakes__c pc: proObj ){
                   pc.Primary_Household_ID__c = rem.Household_ID_del__c;
                   pc.Household_Update__c = 'Completed' ;
                   update pc;
               }
           }          
        }      
    }
}

 

This was selected as the best answer