You need to sign in to do that
Don't have an account?
Aishwarya P 4
Auto Create a contact when account is created
This is the controller class:
public class MYAPEX_Acc
{
public static void createcon(List<Account> lstaccforcon)
{
List<Contact> lstcon = new List<Contact>();
for(Account acc : lstaccforcon)
{
Contact newcon = new Contact();
newcon.AccountId = acc.Id;
newcon.FirstName = acc.Name;
newcon.Description = acc.Description;
newcon.Phone = acc.Phone;
newcon.Email = acc.Email__c;
lstcon.add(newcon);
}
try{
INSERT lstcon;
}
catch(exception e)
{
}
}
}
This is the trigger :
trigger AishuAccount on Account (before insert,before update,after insert,after update)
{
List<Account> lstacc = new List<Account>();
List<Account> lstaccforcon = new List<Account>();
List<Contact> lstcon = new List<Contact>();
If(Trigger.isAfter)
{
If(Trigger.isInsert)
{
for(Account acc : Trigger.new)
{
If(acc.Phone != NULL && acc.Name != NULL)
lstaccforcon.add(acc);
}
}
If(lstaccforcon.size() > 0)
{
MYAPEX_Acc.createcon(lstaccforcon);
}
}
}
I'm unable to create a contact with this code. please help
public class MYAPEX_Acc
{
public static void createcon(List<Account> lstaccforcon)
{
List<Contact> lstcon = new List<Contact>();
for(Account acc : lstaccforcon)
{
Contact newcon = new Contact();
newcon.AccountId = acc.Id;
newcon.FirstName = acc.Name;
newcon.Description = acc.Description;
newcon.Phone = acc.Phone;
newcon.Email = acc.Email__c;
lstcon.add(newcon);
}
try{
INSERT lstcon;
}
catch(exception e)
{
}
}
}
This is the trigger :
trigger AishuAccount on Account (before insert,before update,after insert,after update)
{
List<Account> lstacc = new List<Account>();
List<Account> lstaccforcon = new List<Account>();
List<Contact> lstcon = new List<Contact>();
If(Trigger.isAfter)
{
If(Trigger.isInsert)
{
for(Account acc : Trigger.new)
{
If(acc.Phone != NULL && acc.Name != NULL)
lstaccforcon.add(acc);
}
}
If(lstaccforcon.size() > 0)
{
MYAPEX_Acc.createcon(lstaccforcon);
}
}
}
I'm unable to create a contact with this code. please help
All Answers
In your code you missed adding Lastname in contact and its a required field. So in your class MYAPEX_Acc add below line
newcon.LastName = acc.Name;
Thanks
Shweta
Below is the working code:
public class MYAPEX_Acc {
public static void createcon(List<Account> lstaccforcon)
{
List<Contact> lstcon = new List<Contact>();
for(Account acc : lstaccforcon)
{
Contact newcon = new Contact();
newcon.AccountId = acc.Id;
newcon.FirstName = acc.Name;
newcon.LastName = acc.Name;
newcon.Description = acc.Description;
newcon.Phone = acc.Phone;
newcon.Email = acc.Email__c;
lstcon.add(newcon);
}
try{
INSERT lstcon;
}
catch(exception e)
{
}
}
}
trigger AishuAccount on Account (before insert,before update,after insert,after update) {
List<Account> lstacc = new List<Account>();
List<Account> lstaccforcon = new List<Account>();
List<Contact> lstcon = new List<Contact>();
If(Trigger.isAfter)
{
If(Trigger.isInsert)
{
for(Account acc : Trigger.new)
{
If(acc.Phone != NULL && acc.Name != NULL)
lstaccforcon.add(acc);
}
}
If(lstaccforcon.size() > 0)
{
MYAPEX_Acc.createcon(lstaccforcon);
}
}
}
TRIGGER:
trigger accounttgr on account(after insert){
list<contact> cons = new list<contact>();
for(account a :trigger.new)
{
contact c = new contact();
c.lastname = a.name;
c.phone = a.phone;
c.email = a.description
slly we can match or assign the fileds which u want
finally
c.accountid = a.id;
cons.add(c);
}
insert cons;
}
Its working.