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
Nagarjuna Reddy NandireddyNagarjuna Reddy Nandireddy 

HOW TO WRITE A TRIGGER Account Name will comes to related contact last name

HI,
here i was attached two pics 
my quetion is how to write a trigger as 
when creating a record in account, Account Name will comes to related contact last name
User-added image



User-added image

please help me,


thanks,
NAGARJUNA REDDY NANDIREDDY
Kapil KaushikKapil Kaushik
Hi Nagarjuna,

I got your question, But before to reach at a solution, We have certain condition which you must have to follow to get a workaround for this case. Please read conditions mentioned below - 

1) You need to override the "New Contact" button shown in the Contact related list of Account.
2) If you are agree with the 1st point then this solution will be org dependent, means If you are working on multiple orgs and moving your code from one org to another then this solution will not work for you.

Please give your thoughts and let me know your queries.

Thanks & Regards,
Kapil Kaushik
Dilip_VDilip_V
Hi NAGARJUNA REDDY NANDIREDDY,

Please use the below trigger to create contact when creating account.
 
Trigger ContactCreate On Account(After Insert)
{
List<Contact> Contacts=new List<Contact>();
for(Account Account:Trigger.new)
{
Contact Contact=new Contact();
Contact.lastname=Account.name;
Contact.AccountId=Account.id;
Contacts.add(Contact);
}
if(Contacts.size()>0)
 insert Contacts;
}

Please mark it as best answer if it solves your problem.

Thanks,
Dilip.

 
Swaraj Behera 7Swaraj Behera 7
Hi Nagarjuna,
Please use below code.
trigger CreateAccountContact on Account (after insert, after update){

if(Trigger.isInsert){

    List<Contact> ct = new List <Contact>();

    for(Account acc : trigger.new){

        Contact c = new Contact(LastName = acc.name
                    );

        ct.add(c);
    }
    insert ct; 
}

Thanks,
Swaraj Behera
Kapil KaushikKapil Kaushik
Hi Nagarjuna, Please clear our below queries - 

1) Do you really wants to create multiple contacts when any new Account is created or
2) You want to show the Account Name as the Last Name of contact on the standard Create page of a Contact ?