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
brozinickrbrozinickr 

Simple Contact Creation from Account Trigger

I'm having issues writing a trigger that will insert a Contact record from information based on the Account.

 

Basically, if it's an account is inserted, I want it to create a Contact.  That part is easy.  The next part that I am having trouble writing is if the account is updated part.   If it's an update, I want it to look to see if there are any Contacts with the Contact_Type__c = General.  If not, then it needs to create a Contact like in the insert portion.  Thought it was simple enough but I can't get it work correctly.  I also need to make sure that this bulk safe since we load accounts nightly.

 

 

I marked the line it's failing on in red.

 

 

 

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,
                        Phone=acc.Phone);

        ct.add(c);
        
        }
        
        if(!ct.isEmpty())
            insert ct; 
    }
    
    else{
        
        List<contact> cntsload = new List <Contact>();
        
        for(Account acc : trigger.new){
                List<Contact> cnt = new List<Contact>([select id, Contact_Type__c from Contact where AccountId = acc.id and Contact_Type__c = 'General']);
        
        if(cnt.isEmpty()){

        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,
                        Phone=acc.Phone);

        cntsload.add(c);
        
        }
        }
        
        if(!cntsload.isEmpty())
            insert cntsload; 
    
    
    }
    
}

 

h20riderh20rider

What is the error you are getting?

 

To bulkify you just need to do the following.

 

        Set<Id> acctIds = new Set<Id>{};

        Map<Id, Contact> contactmap = new Map<Id, Contact>{};

        for(Account acc : trigger.new){

              acctIds.add(  acc.id);

              contactmap.put( acc.id, acc);

        }

       for ( Contact cnt : [select id, Contact_Type__c, AccountId from Contact where AccountId in  :acctIds and Contact_Type__c = 'General']){


         Account acc = contactmap.get(cnt);
        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,
                        Phone=acc.Phone);

        cntsload.add(c);

 }

 

brozinickrbrozinickr

this is the error that I am getting:

 

 Error: Compile Error: Variable does not exist: cntsload at line 61 column 20

 

 

 

 

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,
                        Phone=acc.Phone);

        ct.add(c);
        
        }
        
        if(!ct.isEmpty())
            insert ct; 
    }
    
    else{
    
    Set<Id> acctIds = new Set<Id>{};

        Map<Id, Contact> contactmap = new Map<Id, Contact>{};

        for(Account acc : trigger.new){

              acctIds.add(  acc.id);

              contactmap.put( acc.id, acc);

        }

       for ( Contact cnt : [select id, Contact_Type__c, AccountId from Contact where AccountId in  :acctIds and Contact_Type__c = 'General']){


        Account acc = contactmap.get(cnt);
        
        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,
                        Phone=acc.Phone);

        cnt.add(c);

    }
    
            if(!cnt.isEmpty())
            insert cnt; 
    
    }
    
}

 

anil87anil87

Add this line to your code

 

List<contact> cntsload=mew  List<contact>();

---

---

 

change   cnt.add(c); to cntsload.add(c);

                insert cnt; to insert cnt;sload

brozinickrbrozinickr

Thanks, that fixed that error!  Now I am getting another one though.  Here is the error:

 

Compile Error: Incompatible value type SOBJECT:Account for MAP<Id,Contact> at line 37 column 15 (marked line in red below)

 

I also had another question, if the account is being updated, how would work if I wanted to insert the contact if it doesn't exist or if the contact does exist, update the information from the account?  Could I just change the insert at the bottom to an upsert to get to work?

 

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,
                        Phone=acc.Phone);

        ct.add(c);
        
        }
        
        if(!ct.isEmpty())
            insert ct; 
    }
    
    else{
    
    Set<Id> acctIds = new Set<Id>{};
    List<Contact> cntsload = new List<Contact>();
    Map<Id, Contact> contactmap = new Map<Id, Contact>{};

       for(Account acc : trigger.new){

              acctIds.add(acc.id);

              

contactmap.put(acc.id, acc);

       }

       for (Contact cnt : [select id, Contact_Type__c, AccountId
                           from Contact
                           where AccountId in :acctIds
                           and Contact_Type__c = 'General']){

        Account acc = contactmap.get(cnt);
        
        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,
                        Phone=acc.Phone);

        cntsload.add(c);

     }
    
            if(!cnt.isEmpty())
            insert cntsload;  
            
    }
    
}

 

 

h20riderh20rider

change the map from <Id, Contact> to <Id, Account>.  My bad

brozinickrbrozinickr

Thanks!  I also made some other changes too (it didn't like the key type on this line: Account acc = contactmap.get(cnt.AccountId); ), so I changed it.  I got it to save and it works.  I am having issues with it creating duplicates.

 

So if there is a contact with the type of General, don't create a contact. If there isn't a contact with the type of General, create the contact.

 

 

 

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,
                        Phone=acc.Phone);

        ct.add(c);
        
        }
        
        if(!ct.isEmpty())
            insert ct; 
    }
    
    else{
    
    Set<Id> acctIds = new Set<Id>{};
    List<Contact> cntsload = new List<Contact>();
    Map<Id, Account> contactmap = new Map<Id, Account>{};

       for(Account acc : trigger.new){

              acctIds.add(acc.id);

              contactmap.put(acc.id, acc);

       }

       for (Contact cnt : [select AccountId, Contact_Type__c
                           from Contact
                           where AccountId in :acctIds
                           and Contact_Type__c = 'General']){

        Account acc = contactmap.get(cnt.AccountId);
        
        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,
                        Phone=acc.Phone);

        cntsload.add(c);

     }
    
            if(!cntsload.isEmpty())
            insert cntsload;  
            
    }
    
}
brozinickrbrozinickr

I made some changes to this since it wasn't working correctly.  Would anyone be able to take a look at the changes I made?  I was able to change the code to insert and to update when it already existed.  I'm having issues now with inserting the contact record on already existing accounts.

 

Basically in the bottom part, in the first half of the update, I am looking to see if a Contact with Contact_Type__c = General exists.  If so, then don't create and add the id the contacts to update portion.  if a Contact with Contact_Type__c = General doesn't exist, then insert.  I thought i could use the isEmpty(), but then it says the variable c doesn't exist on the red line below.

 

trigger InsertUpdateGeneralContact on Account (after insert, after update)
{
    if(Trigger.isInsert)
    {
        List<Contact> ct = new List <Contact>();
        for (Account acc: Trigger.New)
        {
            //  Insert a contact on account insert with mapping

                 ct.add (new Contact(
                         FirstName = 'Account',
                         LastName = 'Email',
                         Email = acc.Company_Email__c,
                         AccountId = acc.id,
                         Fax=acc.Fax,
                         MailingStreet=acc.BillingStreet,
                         MailingCity=acc.BillingCity,
                         MailingState=acc.BillingState,
                         MailingPostalCode=acc.BillingPostalCode,
                         MailingCountry=acc.BillingCountry,
                         Phone=acc.Phone,
                         Contact_Source__c = 'Contact'
                         )
                     );   
            
        }
        if(!ct.isEmpty())
            insert ct; 
    }
    else{
    
        List<Contact> lstContacttoInsert = new List<Contact>();
        Set<Id> setAccountIdwithEmail = new Set<Id>();
        Set<Id> setAccountToInsert = new Set<Id>();
        Map<Id, Account> contactmap = new Map<Id, Account>{};
        
        for(Account objAccount: Trigger.new){
        
              setAccountToInsert.add(objAccount.id);

              contactmap.put(objAccount.id, objAccount);
             
             for (Contact cnt : [select AccountId, Contact_Type__c
                           from Contact
                           where AccountId in :setAccountToInsert
                           and Contact_Type__c = 'General']){
                           
             Account acc = contactmap.get(cnt.AccountId);
             
             //if(cnt.isEmpty())
             
             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,
                        Phone=acc.Phone);

                        lstContacttoInsert.add(c);
            }
        
            /*if(Trigger.oldMap.get(objAccount.Id).Company_Email__c == null && objAccount.Company_Email__c != null){
            
                //  Old value of company email was null and it was assigned a value, so create a contact
                
                lstContacttoInsert.add (new Contact(
                         FirstName = 'Account',
                         LastName = 'Email',
                         Email = objAccount.Company_Email__c,
                         AccountId = objAccount.id)
                     );
            }*/
            
            if(Trigger.oldMap.get(objAccount.Id).Phone != Trigger.newMap.get(objAccount.Id).Phone){
            
                //  Old value is updated, so collect the account ids whos contact is tp be updated.
                
                setAccountIdwithEmail.add(objAccount.Id);
            }
        }
        
        //  query contacts based on the set of account whose company email field was updated.
        List<Contact> lstContact = new List<Contact>([Select FirstName, LastName, Email, AccountId,
                                                            OwnerId, Fax, MailingStreet, MailingCity, MailingState, MailingPostalCode, Phone, Contact_Type__c
                                                            from contact where AccountId in: setAccountIdwithEmail
                                                            AND Contact_Source__c = 'Contact']);
        for(Contact objContact: lstContact)
        {
            //  Modify lstContact with Accounts company email field 
            objContact.Email = Trigger.newMap.get(objContact.AccountId).Company_Email__c;
            objContact.Fax = Trigger.newMap.get(objContact.AccountId).Fax;
            objContact.MailingStreet = Trigger.newMap.get(objContact.AccountId).BillingStreet;
            objContact.MailingCity = Trigger.newMap.get(objContact.AccountId).BillingCity;
            objContact.MailingState = Trigger.newMap.get(objContact.AccountId).BillingState;
            objContact.MailingPostalCode = Trigger.newMap.get(objContact.AccountId).BillingPostalCode;
            objContact.MailingCountry = Trigger.newMap.get(objContact.AccountId).BillingCountry;
            objContact.Phone = Trigger.newMap.get(objContact.AccountId).Phone;

        }
        if(!lstContact.isEmpty())
            update lstContact;
        if(!lstContacttoInsert.isEmpty())
            insert lstContacttoInsert;
    }
    
}

 

swati_sehswati_seh

I tried using this code.... it worked perfectly fine but contact created for that particular account is not visible in account related list.

it is getting saved in contact but not visible on related list of account.

 

 

please help.