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 

Create Child report automatically

Where do you thank the problem is. the trigger is save but does not fire.  The idee is to create a child record on a self relationship when outputType is House. Help me please
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.OutputType__c =='House') {
            Output__c OutputChild = new Output__c();
            OutputChild.Housing_If_Sanitation__c= NewOut.Id;
            OutputChild.Project_Type__c = 'Sanitition';
            OutputChild.RecordTypeId='Infrastructure';
            OutputChild.Description__c='Discrption';
            OutputChild.Locality__c='ghjggh';
            Output.add(OutputChild);
        }
    }
    upsert Output;
}
Raj VakatiRaj Vakati
Try this code .. update the Parent__c API name with corerect Output__c ​​​​​​​ Parent Name 


If name field is not Auto number please set the name also 
​​​​​​​
trigger AutoOutput on 	Output__c (after insert, after update) {
    List<Output__c> Output = new List<Output__c>();

     String recTypeId = Schema.SObjectType.Output__c.getRecordTypeInfosByName().get('Infrastructure').getRecordTypeId() ; 
	 
    for (Output__c NewOut : Trigger.New) {
        if ( NewOut.OutputType__c =='House') {
            Output__c OutputChild = new Output__c();
            OutputChild.Housing_If_Sanitation__c= true;
		  OutputChild.Parent__c= NewOut.Id;
            OutputChild.Project_Type__c = 'Sanitition';
            OutputChild.RecordTypeId=recTypeId;
            OutputChild.Description__c='Discrption';
            OutputChild.Locality__c='ghjggh';
            Output.add(OutputChild);
        }
    }
    upsert Output;
}

 
Girbson BijouGirbson Bijou
Hi @Raj the code is not work. I realized i made a mistake and i correct it .  I have the code below. It fire and display the message in the attached Screenshot. I recall this is a self relationship(on the same object).User-added image
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.OutputType__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='Discrption';
            OutputChild.Locality__c='ghjggh';
            OutputChild.Milestone__c= NewOut.Milestone__c;
            Output.add(OutputChild);
            upsert OutputChild;
        }
        
    }
    
}
Raj VakatiRaj Vakati

Many developers face this issue because of a recursive trigger. For example, in an 'after update' trigger, a developer could be performing an update operation and this would lead to a recursive call, and the error:
"maximum trigger depth exceeded"

Refer this link


https://help.salesforce.com/articleView?id=000133752&type=1


Create Apex Class as shown below  

 
public class checkRecursive {
 public static Set<Id> SetOfIDs = new Set<Id>();
}

Change apex trigger as shown below 
 
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.OutputType__c =='House') {
		 If(!checkRecursive.SetOfIDs.contains(NewOut.Id)){
		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='Discrption';
		OutputChild.Locality__c='ghjggh';
		OutputChild.Milestone__c= NewOut.Milestone__c;
		Output.add(OutputChild);
					checkRecursive.SetOfIDs.add(NewOut.Id);

		 }
	}
	
}
upsert Output ;




}

 
Girbson Bijou 8Girbson Bijou 8
i get the same message. and receive an email with subject: maximum trigger depth exceeded Output trigger event AfterUpdate Output trigger event AfterInsert Output trigger event AfterInsert Output trigger event AfterIn...
Goku ZeusGoku Zeus
For those who enjoy cooking, SUVI has a great package deal. The Suvi Sous Vide Bundle comes with everything you need to prepare your Sous Vide dishes perfectly.
https://suvi.com.au/blogs/news/easy-to-prepare-sous-vide-chicken-breast-recipe-suvi-cooking