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
Kartik Dangre 15Kartik Dangre 15 

how to write code when Account updated Contacts created and AS Contact created Opportunity also get created

Hi,

How to write code when Account updated Contacts will created and as Contact created Opportunity also get created
Mahesh DMahesh D
Hi Karthik,

If we go step by step,

Step 1: Create an 'After Trigger' on Account object to create the corresponding Contacts.
           --> You have to add proper condition as it is in update, make sure that the Contact is creating only the required update is happening on the Account.
Step 2: Create an 'After Trigger' On Contact object to create the corresponding Opportunity record.

Regards,
Mahesh
Kartik Dangre 15Kartik Dangre 15
Hi Mahesh,

I have written code below:

//Automatically create an Contact when an Account is created
trigger AccCon on Account (after insert) 
{
 List<Contact> NewConn = new List<Contact> ();
 for(Account Acc: trigger.new)
 {
  
  Contact C = new Contact();
  //Con.FirstName= 'Surbhi';
  C .LastName= 'Hanu';
  C .Title='IAS';
  C .Email='Surbh.h@gmail.com';
  C .Phone='8149';
  C .AccountId= Acc.Id;
  NewConn .add(C );
 }
 insert NewConn ;
}


but contact record is not created.
 
Kartik Dangre 15Kartik Dangre 15
please advice if any error in above code
Parteek Kumar 5Parteek Kumar 5
Hi Kartik,

Please try below code..
 
trigger CreateContactOnAccountUpdate on Account (after Update) {
    List<Account> lstAccount = new List<Account>();
    List<Contact> contactInsert = new List<Contact>(); 
    for(Account a : trigger.new){
        lstAccount.add(a);
    }
    for(Account b : lstAccount){
        Contact c = new Contact(LastName= b.Name,FirstName= b.Name,AccountID=b.ID);
        contactInsert.add(c);
    }
    insert contactInsert;
}



trigger CreateOpportunity on Contact (after Insert) {
    List<Contact> lstContact = new List<Contact>();
    List<Opportunity> lstOpp = new List<Opportunity>();
    for(Contact c : trigger.new){
        lstContact.add(c);
    }
    for(Contact d : lstContact){
        System.debug('AccountValue>>>>'+d.Accountid);
        Opportunity o = new Opportunity(Name = d.FirstName,CloseDate=date.today(),StageName='Prospecting',Accountid=d.Accountid);
        lstOpp.add(o);
    }
    insert lstOpp;
}

Thanks,
Parteek
Mahesh DMahesh D
Hi Karthik,

Below is the code for your Account's Trigger:
 
//Automatically create an Contact when an Account is created
trigger AccCon on Account (after insert) {
	List<Contact> conList = new List<Contact> ();
	for(Account acc: trigger.new) {
		Contact con = new Contact();
		//con.FirstName= 'Surbhi';
		con.LastName= 'Hanu';
		con.Title='IAS';
		con.Email='Surbh.h@gmail.com';
		con.Phone='8149';
		con.AccountId= acc.Id;
		NewConn.add(con);
	}
	insert conList;
}

Similar way you can write for Contact object as well. But the above code works only for Account Creation. Means whenever you create an Account, it will create the Contact record.

Regards,
Mahesh