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
Inna ChigarevaInna 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 
Praveen Venkata BPraveen Venkata B

I guess there would be a Record type for the Account types. Write criterial after for loop like:

for(Account acc:trigger.new)
{
    if(recordType='XXXXXXRecordID')
    {
        //add your add contact login
    }
}
Hope this helps !!
Inna ChigarevaInna Chigareva
Praveen,
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.
Praveen Venkata BPraveen Venkata B

Opps !!

I forget to mention the variable and recordtypeID in the if condition.

Try this, it might work !

for(Account acc:trigger.new)
{
    if(acc.recordTypeID='XXXXXXRecordIDOfBusiness1')
    {
        //add your add contact logic
    } 
     if(acc.recordTypeID='XXXXXXRecordIDOfBusiness2')
    {
        //add your add contact logic
    } 
}
Inna ChigarevaInna Chigareva
Dear Praveen,
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).