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
ruchika Nayyarruchika Nayyar 

Once an account insert a contact will create with the name of account and that contact will be the client contact on Account

how i write this code?
explain me???
GauravGargGauravGarg
Hi Ruchika,

Please try below code:
Trigger Account_Trigger on Account(After insert){
	List<Contact> contactList = new List<contact>();
	for(Account acc: trigger.new){
		contact con = new Conact();
		con.Lastname = acc.name;
		con.accountId = acc.id;
		contactList.add(con);
	}
	if(contactList != null && contactList.size() >)
		insert contactList;
}


Hope this will help you, let me know if you still need assistance on this.

Thanks,

Gaurav
Email: gauravgarg.nmims@gmail.com

ruchika Nayyarruchika Nayyar
i have done code for this--- Once an account insert a contact will create with the name of account
i have  Create a field on Account Named (Client Contact lookup to Contact)
how i will do this that contact will be the client contact on Account???


trigger accountcontact on Account (after insert) {
    if(trigger.isinsert){
        
        list<contact> ct= new list<contact>();
        for(account acc:Trigger.new){
            contact c= new contact();
            c.LastName=acc.name;
            c.AccountID=acc.ID;
             c.MailingStreet=acc.BillingStreet;
             c.MailingCity=acc.BillingCity;
              c.MailingCountry=acc.BillingCountry;
             c.Phone=acc.Phone;
             c.Email=acc.myruchika__email1__c;
            c.ID=acc.myruchika__client_contact__c;
            ct.add(c);
        }
    insert ct; 
    }

}
GauravGargGauravGarg
Hi Ruchika,

Firstly, Account - Contact relationship is master (Account) - Detail (Contact) i.e. for single Account we could have multiple contacts. Which contact we need to put as client. 

Thanks,
Gaurav
ruchika Nayyarruchika Nayyar
while we make a new account its contact name will be the name of account and that contact will be client contact on account
GauravGargGauravGarg
Hi Ruchika,

Please add new trigger on Contact object, below is the code:
trigger contact_trigger on Contact (after insert){
	List<Id> AccountId = new List<id>();
	for(Contact con: trigger.new){
		accountId.add(con.accountId);
	}
	Map<Id, Account> AccountMap = new Map<Id, Account>([select id, name, client_contact__c from Account where Id IN: accountId]);
	List<Account> updateAccountList = new List<Account>();
	for(contact con: trigger.new){
		if(AccountMap != null && AccountMap.size() > 0){
			for(Account acc: AccountMap){
				acc.client_contact__c = con.id;
				updateAccountList.add(acc);
			}
		}
	}
	if(updateAccountList != null && updateAccountList.size() >0)
		update updateAccountList;
	
}
Also, please remove "c.ID=acc.myruchika__client_contact__c;". We cannot set "ID" field.

Hope this will help you. 

Thanks,
Gaurav
Email: gauravgarg.nmims@gmail.com
Varalakshmi Niharika JaguVaralakshmi Niharika Jagu
Please add this trigger,
 
trigger CreateClientContactOnAccountInsert on Account (after insert) {
    List<Contact> conList = new List<Contact>();
    List<Account> accList = new List<Account>();
    for(Account acc : Trigger.new) {
        conList.add(new Contact(LastName = acc.Name,
                               	AccountId = acc.Id));
    }
    if(conList.size() > 0) {
        insert conList;
    }
    for(Contact con : conList) {
        Account ac = [SELECT Id, Name, Client_Contact_lookup_to_Contact__c FROM Account WHERE Id =: con.AccountId];
        ac.Client_Contact_lookup_to_Contact__c = con.Id;
        accList.add(ac);
    }
    update accList;
}