2

I am writing a trigger to auto create a contact once the account is created. Also I would like to update all the existing accounts so that it will create contacts only once(Further updates of account should not create one more contact). I am stuck on how to write after update trigger. Here is the code that I have written so far. Any help in the code could be helpful.

trigger BillingContacts on Account (after insert , after update){

if(trigger.isinsert && trigger.isafter)    {

    List<Contact> ct = new List <Contact>();

    for(Account acc : trigger.new){

        Contact c = new Contact(LastName = acc.name + '-' +'Billing Contact',
                    AccountId=acc.id,
                    Fax=acc.Fax,
                    Phone=acc.Phone,
                    Contact_Type__c = true,
                    MailingStreet=acc.BillingStreet,
                    MailingCity=acc.BillingCity,
                    MailingState=acc.BillingState,
                    MailingPostalCode=acc.BillingPostalCode,
                    MailingCountry=acc.BillingCountry,
                    );

        ct.add(c);
    }
    insert ct;
    } 

    if(trigger.isupdate && trigger.isafter){

    }

}
  • I guess I don't understand. You only want it to happen on the insert and not on the update, then why do you need to add anything for an update trigger. What you have now works only on insert and not on update. – Chris Duncombe Jul 8 '15 at 15:40
  • I need that to happen also for update. But that should happen only once. I mean for the first time update of the existing account it should create a contact. If you can see in the above code, the name of the contact ends with "Billing contact". So if the contacts for that specific account already has a contact with the name ending with "Billing Contact", it should not create any other contact. If that account doesn't has any contact ending with "billing contact", it should create one. I hope this is clear. – SFDC_BigDog Jul 8 '15 at 15:44
  • I assume you are creating this trigger to ensure that all Accounts have at least one Contact, correct? For existing Accounts, you would need to incorporate some code that would check whether a Contact already exist for that Account. If there isn't, you would run the same code that you already have. If there is, it would do nothing. Am I correct in this assumption? – Aaron Jul 8 '15 at 16:10
  • @Aaron You are right. But it should check for a contact whose name ends with "billing contact". If there is none, it should create one. Otherwise it should not. Hope this is clear. – SFDC_BigDog Jul 8 '15 at 16:13
3

You can try the below code.

if(trigger.isupdate && trigger.isafter){
    List<Contact> ct = new List <Contact>();
    Map<Id,Boolean> mapCheck = new Map<Id,Boolean>();
    for(Contact c : [SELECT LastName,AccountId FROM Contact WHERE AccountId IN : trigger.new]) {
        mapCheck.put(c.AccountId,true);
        if(c.LastName.contains('Billing Contact'))
            mapCheck.put(c.AccountId,false);
    }
    for(Account acc : trigger.new){
        if(mapCheck.get(acc.Id)) {
            Contact c = new Contact(LastName = acc.name + '-' +'Billing Contact',
                        AccountId=acc.id,
                        Fax=acc.Fax,
                        Phone=acc.Phone,
                        Contact_Type__c = true,
                        MailingStreet=acc.BillingStreet,
                        MailingCity=acc.BillingCity,
                        MailingState=acc.BillingState,
                        MailingPostalCode=acc.BillingPostalCode,
                        MailingCountry=acc.BillingCountry,
                        );
            ct.add(c);
        }
    }
    insert ct;
}

My only concern is for each Contact the mapCheck will be updated in the first for loop with its accountid as key and true as value.

I can't think of any better way to avoid this.

1
trigger BillingContacts on Account (after insert , after update){

if(trigger.isinsert && trigger.isafter)    {

    List<Contact> ct = new List <Contact>();

    for(Account acc : trigger.new){

        Contact c = createcontact(acc);

        ct.add(c);
    }
    insert ct;
    } 

    if(trigger.isupdate && trigger.isafter){

    Map<id,contact> Mapcon = new Map<Id,contact>();

    List<contact> Lst=[select id,name,accountId from Contact where AccountId IN:trigger.newmap.keyset()];
     List<Contact> ctinsert = new List <Contact>();
     List<Contact> ctupdate = new List <Contact>();
    if(Lst.size()>0){
     for(contact c:Lst){
    mapcon.put(c.AccountId,c);
     }

     if(mapcon.size()>0){

     for(Account a:trigger.new){

     if(Mapcon.containsKey(a.Id)){

     if(Mapcon.get(a.id).Name.contains('Billing Contact')){
     Contact c = updatecontact(a,Mapcon.get(a.id).Id);
     ctupdate.add(c);

     }else{
     Contact c = createcontact(a);
     ctinsert.add(c);
     }

     }else{
     Contact c = createcontact(a);
     ctinsert.add(c);
     }
     }



     }

    }else{

      for(Account acc : trigger.new){

        Contact c = createcontact(acc);

        ctinsert.add(c);
        }
     }

    if(ctinsert.size()>0){

     Insert ctinsert;
     }

    if(ctupdate.size()>0){

     update ctupdate;
     }
    }

    public static contact createcontact(Account acc){

       Contact c = new Contact(LastName = acc.name + '-' +'Billing Contact',
                    AccountId=acc.id,
                    Fax=acc.Fax,
                    Phone=acc.Phone,
                    Contact_Type__c = true,
                    MailingStreet=acc.BillingStreet,
                    MailingCity=acc.BillingCity,
                    MailingState=acc.BillingState,
                    MailingPostalCode=acc.BillingPostalCode,
                    MailingCountry=acc.BillingCountry
                    );

                    return c;


    }

    public static contact updatecontact(Account acc,string cid){

       Contact c = new Contact(LastName = acc.name + '-' +'Billing Contact',
                    AccountId=acc.id,
                    Fax=acc.Fax,
                    Phone=acc.Phone,
                    Contact_Type__c = true,
                    MailingStreet=acc.BillingStreet,
                    MailingCity=acc.BillingCity,
                    MailingState=acc.BillingState,
                    MailingPostalCode=acc.BillingPostalCode,
                    MailingCountry=acc.BillingCountry,
                    id=cid
                    );

                    return c;


    }

}
  • This code may insert a contact if you update a account having two contacts in which one already having last name as 'billing contact' because mapcon.put(c.AccountId,c) will get overwritten. – Vigneshwaran G Jul 9 '15 at 1:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.