+ Start a Discussion
MalakondaiahMalakondaiah 

Automatically created the 2 child records when parent(Account) record is created?

Hi All,

Automatically created the 2 child records when parent(Account) record is created? Best approach (Process builder or Apex trigger)?


Thanks,
Malakondaiah
Mukesh_SfdcDevMukesh_SfdcDev
Hi Malakondaiah,

You should use a Trigger when you cannot do what you want to do with the declarative features of Salesforce.
So the answer is process builder.

Thanks
Mukesh Kumar
mukesh guptamukesh gupta
Hi Malakondaiah,

I have test below code my side , Please use :-
 
trigger Addreletedrecord on Account (after insert) {
List<Opportunity> oppList = new List<Opportunity>();
	for(Account a:[SELECT Id,Name from Account where Id IN:Trigger.new]){
		oppList.add(New Opportunity(Name= a.Name+'opportunity',StageName='Prospecting',AccountId=a.Id));
    }
    
    try {
       insert oppList;
    } catch (system.Dmlexception e) {
        system.debug (e);
    }

}

Please MARK AS BEST ANSWER!!!!!!

Regards
Mueksh
Ajay K DubediAjay K Dubedi
Hi Malakondaiah,
A Process Builder will also do the work and will be a better option.
You just have to create an action that would create a new record and reference the account id in the accountId field.

Thanks
Ajay
MalakondaiahMalakondaiah
Hi Ajay,
 I am doubt on automatically create 2 child records ,when parent record is created using process builder(more than 2 objects possible using process builder).
Thanks,
Malakondaiah
Ajay K DubediAjay K Dubedi
Hi Malakondaiah,

Yes we can do it via Process Builder.

We need to just create two actions as in the pic below

User-added image

a1 and a2 are two actions which will create contacts

Set action as:-
User-added image

Please mark it as best if it helps.

Thanks 
Ajay
MalakondaiahMalakondaiah
Hi Ajay,
Thanks for Your Replay. I am not talking about the actions. 
My question : is it possible to do 2 or more child objects when parent/other record created using process builder.
Thanks,
Malakondaiah
Ajay K DubediAjay K Dubedi
Hi,
So do you want two different object records to be created whwn a account record is created?
 
MalakondaiahMalakondaiah
Yes.
Ajay K DubediAjay K Dubedi
Two Object Records not Objects.
Yes, you can do it, just change the object in the action.