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
nksfnksf 

Trigger to Auto Create Child Record

Hi guys can you please help me to write a Trigger to Auto create child Record when new parent record is created 
I have custom Object as Order__c and I have it's child Object asTax Details__c. I have a lookup field of Account and Country__c Picklist Custom Field on Tax Details Object . So I want whenever a new Order is created then a new Tax Details record should be created with Order Number, Account Name and Country. Order Number will be Parent Account (Order_c) number, Account Name will be same as Account Name on Order__c (Lookup of Account on Order__c Object) and Country should be same as we have Country on Order__c Object.
Thanks in advance 
Best Answer chosen by nksf
Balaji Chowdary GarapatiBalaji Chowdary Garapati
So, It would be something like this:

Assuming Account__c is the API name of look to account on both the objects and order__c is the api name for order lookup on tax detail object.
 
Trigger CreateTaxDetails on Order__c (after insert){
		
		List<Tax_Details__c> TaxDetailsToBeInserted=new List<Tax_Details__c>();
		
	for(Order__c order_Record: trigger.new){
		TaxDetailsToBeInserted.add(new Tax_Details__c(
									Order__c=order_Record.id,
									Account__c=order_Record.Account__c,
									Country__c=order_Record.Country__c
							));
	}
	
	try{
	if(TaxDetailsToBeInserted != Null){
		insert TaxDetailsToBeInserted;
	}
	}Catch(Exception e){
		System.debug('Exception ****'+e.getMessage());
	}
}

Hope it helps.,

Thanks,
Balaji

All Answers

Balaji Chowdary GarapatiBalaji Chowdary Garapati
So, It would be something like this:

Assuming Account__c is the API name of look to account on both the objects and order__c is the api name for order lookup on tax detail object.
 
Trigger CreateTaxDetails on Order__c (after insert){
		
		List<Tax_Details__c> TaxDetailsToBeInserted=new List<Tax_Details__c>();
		
	for(Order__c order_Record: trigger.new){
		TaxDetailsToBeInserted.add(new Tax_Details__c(
									Order__c=order_Record.id,
									Account__c=order_Record.Account__c,
									Country__c=order_Record.Country__c
							));
	}
	
	try{
	if(TaxDetailsToBeInserted != Null){
		insert TaxDetailsToBeInserted;
	}
	}Catch(Exception e){
		System.debug('Exception ****'+e.getMessage());
	}
}

Hope it helps.,

Thanks,
Balaji
This was selected as the best answer
nksfnksf
Amazing. Thanks for quick response. It is working great
Thomas Panni 6Thomas Panni 6
Hello Balaji,
I am farely new to writing triggers. What I want to do is to auto create a child case. If you create a case you have the option to enter a parent ID for your new case. My intention is as follows: first step create a new case, save it, then upon insert of the case the trigger (or whatever) should fire and get a list of all cases, then check if the field parent ID is filled. Only for those where this field is empty it should now automatically create one or more child cases, depending on some custom fields on the case object (check box fields like lets say: Interest_In_Tobacco__c, Interest_In_Red_wine__c etc.) If one or more of these check boxes is ticked the corresponding child case should be created. Same in case of updating the parent child with a new tick in one of the tickboxes (only those which were not ticked before).
I read a lot around the internet but it seems this work is not as easy as it should be. I already know: my trigger needs after insert and after update options. It needs a list of cases (should use Set instead of List to avoid duplicates). And: I want to use the original saleforce relationship with the parent case ID (lookup field) to combine parent and child. I tried a lot but I am not able to get any step further. Here is a first approach:


trigger AutoChild on Case (after insert) {

    Set<Id> parentCaseIds = new Set<Id>();

    //Gather up a set of the parent cases. We use Set instead of List because Set will prevent duplicates.

    for (Case ParentCase:Trigger.new) {
        if (ParentCase.ParentId !=null) {
            parentCaseIds.add(ParentCase.ParentId);
    }
    
    }
    List<Case> NewCases = new List<Case>();
    if (parentCaseIds.size()!=0) {
        
        

    
    for (Case newPosition: Trigger.New) {

        if (newPosition.ParentId != null) {

            NewCases.add(new Case(

                        Subject= 'Test'
                        
                        

                        ));
        
}
}
}
    insert NewCases;
}

Can you advise me how to get out of this loop and write a working trigger? Thanks so much in advance....

best Thomas