You need to sign in to do that
Don't have an account?
Inna Chigareva
Auto create a contact when adding new account, but not Personal account
Hi,
I am trying to create a contact associated with a new business account. See below:
trigger CreateAccountContact on Account (after insert, after update){
if(Trigger.isInsert){
List<Contact> ct = new List <Contact>();
for(Account acc : trigger.new){
Contact c = new Contact(LastName = acc.name,
AccountId=acc.id,
Fax=acc.Fax,
MailingStreet=acc.BillingStreet,
MailingCity=acc.BillingCity,
MailingState=acc.BillingState,
MailingPostalCode=acc.BillingPostalCode,
MailingCountry=acc.BillingCountry,
Address__c=acc.Address__c,
Phone=acc.Phone,
Email=acc.Email_address__c);
ct.add(c);
}
insert ct;
}
}
The only problem is that it also triggers when Personal account is created. I need to add a line in the code to only assosiate it to business accounts. Is there a way?
Thanks,
Inna
I am trying to create a contact associated with a new business account. See below:
trigger CreateAccountContact on Account (after insert, after update){
if(Trigger.isInsert){
List<Contact> ct = new List <Contact>();
for(Account acc : trigger.new){
Contact c = new Contact(LastName = acc.name,
AccountId=acc.id,
Fax=acc.Fax,
MailingStreet=acc.BillingStreet,
MailingCity=acc.BillingCity,
MailingState=acc.BillingState,
MailingPostalCode=acc.BillingPostalCode,
MailingCountry=acc.BillingCountry,
Address__c=acc.Address__c,
Phone=acc.Phone,
Email=acc.Email_address__c);
ct.add(c);
}
insert ct;
}
}
The only problem is that it also triggers when Personal account is created. I need to add a line in the code to only assosiate it to business accounts. Is there a way?
Thanks,
Inna
I guess there would be a Record type for the Account types. Write criterial after for loop like:
Hope this helps !!We have 2 business record types, and that makes it even more complicated. Your proposed solution didnt realy help, code doesnt recognise record type ID.
Opps !!
I forget to mention the variable and recordtypeID in the if condition.
Try this, it might work !
I still have some troubles. Please see below:
trigger CreateAccountContact on Account (after insert, after update){
if(Trigger.isInsert){
List<Contact> ct = new List <Contact>();
for(Account acc : trigger.new)
{
if(acc.recordTypeID='01224000000Da23')
{
Contact c = new Contact(LastName = acc.name,
AccountId=acc.id,
Fax=acc.Fax,
MailingStreet=acc.BillingStreet,
MailingCity=acc.BillingCity,
MailingState=acc.BillingState,
MailingPostalCode=acc.BillingPostalCode,
MailingCountry=acc.BillingCountry,
Address__c=acc.Address__c,
Phone=acc.Phone,
Email=acc.Email_address__c);
}
if(acc.recordTypeID='01224000000CrTk')
{
Contact c = new Contact(LastName = acc.name,
AccountId=acc.id,
Fax=acc.Fax,
MailingStreet=acc.BillingStreet,
MailingCity=acc.BillingCity,
MailingState=acc.BillingState,
MailingPostalCode=acc.BillingPostalCode,
MailingCountry=acc.BillingCountry,
Address__c=acc.Address__c,
Phone=acc.Phone,
Email=acc.Email_address__c);
}
ct.add(c);
}
insert ct;
}
}
It dowsnt allow me to save it, because variable "c" in ct.add(c) doesn't exist. And condition expression must be of type Boolean at line 9 (if function).