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
Anshuman ParhiAnshuman Parhi 

Write a trigger to create related contact when an Account is created and associate that contact with that account

Hello guys

I am having above scenario but  i am not able to findout perfect solution and i was trying but it is not working.

could someone help me out..
Best Answer chosen by Anshuman Parhi
CharuDuttCharuDutt
Hii Anshuman
Try Below Code
trigger test on Account (after  insert) {
    list<Contact> lstCon = new list<Contact>();
    for(Account Acc: trigger.new){
        Contact con = new Contact ();
        Con.FirstName = 'test';
        Con.AccountId = Acc.Id;
        Con.LastName = Acc.Name;
        lstCon.Add(Con);
        
    }
insert lstCon;
}
Please Mark It As Best Answer If It Helps
Thank You! 

 

All Answers

CharuDuttCharuDutt
Hii Anshuman
Try Below Code
trigger test on Account (after  insert) {
    list<Contact> lstCon = new list<Contact>();
    for(Account Acc: trigger.new){
        Contact con = new Contact ();
        Con.FirstName = 'test';
        Con.AccountId = Acc.Id;
        Con.LastName = Acc.Name;
        lstCon.Add(Con);
        
    }
insert lstCon;
}
Please Mark It As Best Answer If It Helps
Thank You! 

 
This was selected as the best answer
Prateek Joshi 33Prateek Joshi 33
i have written the same code but just for extra caution i came here and copy paste it in my dev console but it still showing the same error : "Variable does not exist: FirstName". I have never faced this error, Can you bother to tell what is the problem cause i have write this code many times in past but i never faced this problem.
Prateek Joshi 33Prateek Joshi 33
@charuDutt 

Prateek Joshi 33
i have written the same code but just for extra caution i came here and copy paste it in my dev console but it still showing the same error : "Variable does not exist: FirstName". I have never faced this error, Can you bother to tell what is the problem cause i have write this code many times in past but i never faced this problem.
Saurabh PriyadarshanSaurabh Priyadarshan
@Prateek Joshi 33
Please check if the field 'FirstName' exist in the contact object. Also make sure the field api name is 'FirstName'.
Govind Kumar 11Govind Kumar 11
Follow My Code. It is very Usefull:- Trigger on after insert, after update, after delete, after undelete on Contact
public class CountContactOnAccount {
    Public Static void countContact(List<Contact> conList){
        List<id> accId = new List<id>();
        for(Contact con : conList){
            if(con.AccountId != Null){
                accId.add(con.accountId);
            }           
        }
        List<Account> accList = new List<Account>();
        for(Account acc : [Select id,(Select id from Contacts) from account Where id IN: accId]){
            acc.NumberofLocations__c = acc.Contacts.size();
            accList.add(acc);
        }
        if(!accList.isEmpty()){
            update accList;
        }
    }
}

@Govind Kumar