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
mahi rafimahi rafi 

how to insert new record using trigger

Below is a sample code which trigger to create a contact after an account insert. You can try something like this based on your functionality.
trigger test_trigger on Account (after insert) {
    List<contact> conlst = new List<contact>();
    for(account acc :{
    if(acc.Primary_Contact__c == null){
        Contact con = new contact();
        con.lastname = 'test name';
        con.accountid =;
    if (conlst.size()>0){
    insert conlst;

Akanksha JadhavAkanksha Jadhav
First note that, trigger is invoked when some insert/update/delete command is run for some object.
Steps to insert record using trigger.

1. Create a trigger on some object.
trigger testTrigger on Account (after insert) { //For now, we will define it after insert of account
    if(Trigger.isAfter and Trigger.isInsert){
        List<Account> accList = new List<Account>();
        for(Account tempAcc :  Trigger.New){
        //Use helper class

2. Create a helper class
public class ContactClass{
    public ContactClass(){
    public void insertRecord(List<Account> lstAccount){
        List<Contact> contactList = new List<Contact>();
        if(lstAccount.size() > 0){
            for(Account acc : lstAccount){
                Contact c = new Contact();
                c.lastname = 'Contact ' + acc.Name;
                c.accountid =;
        if(contactList.size() > 0 ){
            insert contactList;

Mark as best answer, if it works.!
Thank and Regards,