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
Lukesh KarmoreLukesh Karmore 

simple trigger but stuck in relating custom field

create custom field on account (client contact lookup to contact)
when account is insert  ,contact is also inserted with same name
and  that contact will be client contact on account
CharuDuttCharuDutt
Hii Lokesh Kamore
Try Below Trigger
trigger test2 on Account (After insert) {
    list<Contact> lstCon = new list<Contact>();
	list<id> AccId =new list<id>();
    String AccName;
    if(Trigger.IsAfter && Trigger.IsInsert){
        For(Account Acc :Trigger.new){
            AccId.add(Acc.Id);
            AccName = Acc.Name;
        }
    }
    
    Contact Con = new Contact();
    Con.FirstName = 'test';
    Con.LastName = AccName;
    Con.AccountId = AccId[0];
    /*fill All The Necessary Fields*/
    lstCon.add(Con);
    insert lstCon;
}
Please Mark It As Best Answer If It Helps
Thank You!

 
Lukesh KarmoreLukesh Karmore
my code is not working:
trigger AccountsClientContact on Account (after insert) {
    list<contact> conlist=new list<contact>();
    list<id> myid=new list<id>();
    string accname;
    string phone;
    string email;
    id  conid;
    
    for(Account acc:Trigger.new){
    myid.add(acc.id);
    accname=acc.name;
    phone=acc.phone;
    email=acc.email__c;
    conid=acc.clientContact__c;
    }
    contact con=new contact();
    con.firstname='test';
    con.lastname=accname;
    con.accountId=myid[0];
    con.phone=phone;
    con.email=email;
    con.Id=conid;
    conlist.add(con);
    insert conlist;
    }
 
CharuDuttCharuDutt
Hii Lukesh Kamore 
The Error is con.Id field Is not Writeable As It Is Generated By System It Self
trigger AccountsClientContact on Account (after insert) {
    list<contact> conlist=new list<contact>();
    list<id> myid=new list<id>();
    string accname;
    string phone;
    string email;
    id  conid;
    
    for(Account acc:Trigger.new){
    myid.add(acc.id);
    accname=acc.name;
    phone=acc.phone;
    email=acc.email__c;
    conid=acc.clientContact__c;
    }
    contact con=new contact();
    con.firstname='test';
    con.lastname=accname;
    con.accountId=myid[0];
    con.phone=phone;
    con.email=email;
    con.Id=conid;
    conlist.add(con);
    insert conlist;
    }
Please Mark It As Best Answer If It Helps
Thank You!
Lukesh KarmoreLukesh Karmore
Whai is the correct ans
CharuDuttCharuDutt
Hii Lukesh Kamore 
I've Made Some  Changes In Trigger It Will Work Now
trigger AccountsClientContact on Account (After insert) {
    list<Account> lstAcc = new list<Account>();
    list<Contact> lstCon = new list<Contact>();
	list<id> AccId =new list<id>();
    String AccName;
    if(Trigger.IsAfter && Trigger.IsInsert){
        For(Account Acc :Trigger.new){
            AccId.add(Acc.Id);
            AccName = Acc.Name;
        }
    }
  
    
    Contact Con = new Contact();
    Con.FirstName = 'test';
    Con.LastName = AccName;
    
    Con.AccountId = AccId[0];
   
    
    lstCon.add(Con);
    insert lstCon;
    
    For(Account Acc2 : [Select id,Name,Client_Contact__c From Account Where Id In:AccId]){
        Acc2.Client_Contact__c = Con.Id;
        lstAcc.add(Acc2);
    }
    Update lstAcc;
   
}
Please Let Me Know If It Work And Mark It As Best Answer If It Helps
Thank You!