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
SUYASH KUMAR BHARTISUYASH KUMAR BHARTI 

Create a field on Account called “Only_Default_Contact”, checkbox, default off Assignment: When a new Account is created, create a new Contact that has the following data points: First Name = “Info” Last Name = “Default” Email = “info@websitedomain.tld”

Foram Rana RForam Rana R
Hi Suyash,

Hope you are doing well .....!!
You need to write trigger on Account Object.
Please go through below code:
trigger insertContact on Account (after insert) {
Contact cont = new Contact();
cont.LastName = Trigger.new[0].name;
cont.AccountId = Trigger.new[0].ID;
insert cont;
}


Hope this solution will help you.
Please select as best answer if you got the solution so other can use it.

Thanks & Regards,
Foram Rana
Ajay K DubediAjay K Dubedi
Hi Suyash,

I have understood your requirements and wrote a trigger to find the solution.
If you also need to make the checkbox true after the contact created, then you can use the below snippet of code.

Trigger-
trigger defaultContact on account(after insert)
{
  Account acc = [select id,Only_Default_Contact from account where id in : trigger.new];
  contact con = new contact();
  con.firstName = 'Info';
  con.LastName = 'Default';
  con.email = 'info@websitedomain.tld';
  con.accountId = acc.id;

  insert con;

  //If you want to make checkbox true then use this also-

   acc.Only_Default_Contact = true;
   update acc;



}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
SUYASH KUMAR BHARTISUYASH KUMAR BHARTI
i have to do another scenario using triggers during update if an account has 2 contacts & then the 2nd contact is shifted to another account then the checkbox should be checked
also if the shifted contact to another account is the first contact of that account then the checkbox must be true
 
Deepali KulshresthaDeepali Kulshrestha
Hi Suyash,

I've gone through your requirement and you can refer the code below to create automatically contact whenever the account created:--->
Apex trigger--->>

Trigger accountinsertautomaticallycontactalsoinsert on Account(After Insert)
{
if(trigger.isInsert && trigger.IsAfter)
CreatedContactAutomatic.createContact(Trigger.New);
}

Apex Handler--->

public class CreatedContactAutomatic
{

public static void createContact(List<Account> allaccounts)
{
List<Contact> allcontacts = new List<Contact>();
For(Account acc:Trigger.New)
{
if(acc.Only_Default_Contact==true)
{
Contact con = New Contact();
   con.lastname = acc.name;
   con.accountid=acc.id;
   allcontacts.add(con);
}
}

if(allcontacts.size()>0)
   insert allcontacts;
}
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com