Your code is correct but one key point is missing ..
You create an an Account from the contact .. But forget to link the account and contact relation .. In Salesforce isolcated contacts have some limitations ,,
srikanth cheera
Bulkfiy this code
trigger InsertAccountIfContactCreated on Contact (after insert) {
List<Contact> needAccounts = new List<Contact>();
for (Contact c : trigger.new) {
needAccounts.add(c);
}
if (needAccounts.size() > 0) {
//List<Account> newAccounts = new List<Account>();
for (Contact c : needAccounts) {
String accountName = c.lastname;
Account a = new Account(name=accountName);
insert a ;
//newAccounts.add(a);
c.AccountId = a.Id ;
}
// INSERT newAccounts;
}
}
trigger conAccount on Contact (before insert) {
Map<String,Contact> conMap = new Map<String,Contact>();
List<Account> acc = new List<Account>();
for(Contact con:trigger.new){
//Only create the account record when a contact is not linked to account
if(String.isBlank(con.accountId)){
conMap.put(con.LastName, con);
acc.add(new Account(name=con.LastName));
}
}
if(acc.size()>0){
insert acc;
for(Account a : acc){
if(conMap.containsKey(a.Name)){
conMap.get(a.Name).accountId = a.Id;
}
}
}
}
Greetings to you.
Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks and Regards,
Khan Anas
Please add this below Code in your Contact trigger after insert:
List<Account> newAccounts = new List<Account>();
for (Contact c : trigger.new) {
newAccounts.add(new Account(name=c.lastname));
}
if(!newAccounts.isEmpty()){
insert newAccounts;
}
Please mark it as best answer if you find it positive.
Thanks,
Jolly Birdi
You need after trigger to update on another object. Hope this will help you.Please mark it as best answer if you find it positive.
Regards,
Gulafsha
Below code can fulfill your requirements. Hope this will work for you.
Trigger :
trigger CreateDefaultContact on Contact (after insert) {
ContactDefault.createContact(trigger.new);
}
Class:
public class ContactDefault {
public static void createContact(List<Contact> contactList)
{
List<Account> accountList = new List<Account>();
for(Contact conObj : contactList)
{
Account accObj = new Account();
accObj.Name = conObj.LastName;
accountList.add(accObj);
}
if(accountList.size() > 0)
{
insert accountList;
}
}
}
Please mark this as best answer if this solves your problem.
Thank you
Ajay Dubedi
Your code is correct but one key point is missing ..
You create an an Account from the contact .. But forget to link the account and contact relation .. In Salesforce isolcated contacts have some limitations ,,
srikanth cheera
Bulkfiy this code
Here is the answer for your ask