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
Prallavi DuaPrallavi Dua 

Create a record in one object and insert the same record in other.

Hi,

Can anyone help me to write the VF page controller to, automatically create a record in one object when a new record is created in
another object.

Thanks,
Varalaxmi Jangay.
SandhyaSandhya (Salesforce Developers) 
Hi,
You can use a trigger for this.Below is the sample code for an account and opportunity 
// Automatically create an Opp when an Account is created
trigger CreateNewAccountOpprtunity on Account (after insert) {
List<Opportunity> opplst=new List<Opportunity>();
for(account a:Trigger.new ){
 Opportunity opp=new Opportunity ();
  opp.AccountId = a.Id;
  system.debug(opp);
  

 opp.Name=a.name;
  opp.Stagename ='Proposal/price quote';
 opp.closeDate=system.today()+30;
 opplst.add(opp);
 system.debug(opplst);
}
insert opplst;

  system.debug(opplst);

}
If you want to use Visualforce then, please refer below link which has sample code.

https://developer.salesforce.com/forums/?id=906F0000000904MIAQ
 
http://salesforce.stackexchange.com/questions/10104/how-to-create-related-record-upon-saving-new-custom-object-record

 Hope this helps you!

If this helps you, please mark it as solved.

Thanks and Regards
Sandhya