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
Girbson BijouGirbson Bijou 

Update child in Self relationship when parent is update

I want to update some field child with the same value of the parent anytime the parent is updated. With this below code, it fire only when a record is create. But it does not update the field on child when the parent is update. I remind that , the contition to just update it i
trigger AutoOutput on 	Output__c (after insert, after update) {
    if(Trigger.isInsert){
    List<Output__c> Output = new List<Output__c>();
 
    for (Output__c NewOut : Trigger.New) {
        if ( NewOut.Project_Type__c =='House'&& NewOut.Housing_If_Sanitation__c==NULL) {
            Output__c OutputChild = new Output__c();
            OutputChild.Housing_If_Sanitation__c= NewOut.Id;
            OutputChild.Project_Type__c = 'Sanitition';
            OutputChild.RecordTypeId=NewOut.RecordTypeId;
            OutputChild.Description__c=NewOut.Description__c;
            OutputChild.Locality__c=NewOut.Locality__c;
            OutputChild.Milestone__c= NewOut.Milestone__c;
            OutputChild.Address__c = NewOut.Address__c;
            OutputChild.claust__c=NewOut.claust__c;
            OutputChild.A__c=NewOut.A__c;
            OutputChild.Besem__c = NewOut.Besem__c;
            Output.add(OutputChild);
            }     
       
        }
        upsert Output;   
    }  
      
}

s when  NewOut.Housing_If_Sanitation__c !=NULL
NagendraNagendra (Salesforce Developers) 
Hi Bijou,

If your formula evaluates to House for the child, then you've created an infinite loop. This error is indicative of a typical infinite loop. However, without knowing how the formula operates, no way to tell your intent. You might want to check if the parent field is already populated or check some other condition, or you might need to adjust your formula's behavior, or some combination thereof.

Hope this helps.

Thanks,
Nagendra
Girbson BijouGirbson Bijou
Hi Nagrend,  I did solve my problem with the infinite loop.  What i need to do is to update the child if child already exist and create the child if it does not exist.  Below is my code the error i receive is  : System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.AutoOutput: line 28, column 1
trigger AutoOutput on 	Output__c (after insert, after update) {
    
    List<Output__c> Output = new List<Output__c>();
 
    for (Output__c NewOut : Trigger.New) {
        if ( NewOut.Project_Type__c =='House') {
            Output__c OutputChild = new Output__c();
            OutputChild.Housing_If_Sanitation__c= NewOut.Id;
            OutputChild.Project_Type__c = 'Sanitition';
            OutputChild.RecordTypeId=NewOut.RecordTypeId;
            OutputChild.Description__c=NewOut.Description__c;
            OutputChild.Locality__c=NewOut.Locality__c;
            OutputChild.Milestone__c= NewOut.Milestone__c;
            OutputChild.Address__c = NewOut.Address__c;
            OutputChild.claust__c=NewOut.claust__c;
            OutputChild.A__c=NewOut.A__c;
            OutputChild.Besem__c = NewOut.Besem__c;
            Output.add(OutputChild);
            }     
       
        }
    for(Output__c out : [SELECT (SELECT Id  from Housing_If_Sanitation__r) FROM Output__c]){
        
        if(out==NULL){
            insert Output;
        }
        else {
            update output;
        }
        }
}