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
Sukriti SharmaSukriti Sharma 

I want to create and update contacts when an account is created. But everytime I update an account it creates a new duplicate contact. How can i solve this?

This is the code I have written so far:
trigger CreateContact on Account (after insert, after update, after undelete) {
    set<id> getaccountid =new set<id>();
    for(account acc : trigger.new){
        getaccountid.add(acc.id);
        system.debug(acc.id);
    }
    list<contact> con = [SELECT id, accountid from contact where accountid in:getaccountid];
    List<Contact> addCon = new List<Contact>();
    for(Account acc : trigger.new){
        if(acc.Create_Contact__c == true){
            Contact con = new Contact();
            con.Lastname = acc.Name;
            con.Fax = acc.Fax;
            con.Phone = acc.Phone;
            con.MailingStreet = acc.BillingStreet;
            con.MailingCity = acc.BillingCity;
            con.MailingCountry = acc.BillingCountry;
            addCon.add(con);
        }
        insert addCon;
    }
}
Best Answer chosen by Sukriti Sharma
Sukriti SharmaSukriti Sharma
trigger CreateContactOnAccount on Account (before insert,after insert, before update, after update) {
    if(Trigger.isBefore || Trigger.isInsert){
         List<Contact> addCon = new List<Contact>();
    	 for(Account acc : trigger.new){
            if(acc.Create_Contact__c == true){
                Contact cons = new Contact(Lastname = acc.Name,
                	AccountId = acc.id,
            		Fax = acc.Fax,
            		Phone = acc.Phone,
            		MailingStreet = acc.BillingStreet,
            		MailingCity = acc.BillingCity,
            		MailingCountry = acc.BillingCountry,
                    MailingPostalCode=acc.BillingPostalCode);
            	addCon.add(cons);
            }
        }
        insert addCon;
    }
    if(Trigger.isupdate && Trigger.isAfter){
        set<id> getaccountid =new set<id>(); //to get the accountid which is updating
        for(account account : trigger.new){
        	getaccountid.add(account.id);
   			list<contact> contactlist = [SELECT id, accountid FROM contact WHERE accountid =:getaccountid]; //gets the contact with same accountid
    		List<Contact> addCon = new List<Contact>();
    		for(Contact cons : contactlist){
        		Account acc = Trigger.newmap.get(cons.accountid);
            	cons.Lastname = account.Name;
        		//cons.AccountId = account.id;
            	cons.Fax = account.Fax;
            	cons.Phone = account.Phone;
            	cons.MailingStreet = account.BillingStreet;
            	cons.MailingCity = account.BillingCity;
            	cons.MailingCountry = account.BillingCountry;
            	addCon.add(cons);
       	 	}
        	update addCon;
    	}
    }
}

 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sukriti,

In the code above you are just quering the List<Contact> and you are not using that any where . You have to use two context like for insert your code works fine. 

For update you have to use other such logic and try to update the existing record. Can you try and let me know if you face any issue.

This is just sample code for update scenerio
 
If(Trigger.isupdate && Trigger.isAfter){
        set<id> getaccountid =new set<id>();
        for(account acc : trigger.new){
        getaccountid.add(acc.id);
        system.debug(acc.id);
    }
    list<contact> conlist = [SELECT id, accountid from contact where accountid in:getaccountid];
    List<Contact> addCon = new List<Contact>();
    for(Contact cons : conlist){
        Account acc = Trigger.newmap.get(cons.accountid);
            cons.Lastname = acc.Name;
            cons.Fax = acc.Fax;
            cons.Phone = acc.Phone;
            cons.MailingStreet = acc.BillingStreet;
            cons.MailingCity = acc.BillingCity;
            cons.MailingCountry = acc.BillingCountry;
            addCon.add(cons);
        }
        update addCon;
    }



If this solution helps, Please mark it as best answer.

Thanks,
 
Sukriti SharmaSukriti Sharma
Hi @Sai Praveen, 
Thank you for replying. So, what I am trying to do is i want to have create and update contacts in one trigger only. I wanted to know how can I use if-else statements for this. I am still new to triggers. I want to create a contact when account is created but if there is an exsisting contact and account Iwant to update the same contact when I edit account.Currently it is making a new duplicate contact.
Sukriti SharmaSukriti Sharma
trigger CreateContactOnAccount on Account (before insert,after insert, before update, after update) {
    if(Trigger.isBefore || Trigger.isInsert){
         List<Contact> addCon = new List<Contact>();
    	 for(Account acc : trigger.new){
            if(acc.Create_Contact__c == true){
                Contact cons = new Contact(Lastname = acc.Name,
                	AccountId = acc.id,
            		Fax = acc.Fax,
            		Phone = acc.Phone,
            		MailingStreet = acc.BillingStreet,
            		MailingCity = acc.BillingCity,
            		MailingCountry = acc.BillingCountry,
                    MailingPostalCode=acc.BillingPostalCode);
            	addCon.add(cons);
            }
        }
        insert addCon;
    }
    if(Trigger.isupdate && Trigger.isAfter){
        set<id> getaccountid =new set<id>(); //to get the accountid which is updating
        for(account account : trigger.new){
        	getaccountid.add(account.id);
   			list<contact> contactlist = [SELECT id, accountid FROM contact WHERE accountid =:getaccountid]; //gets the contact with same accountid
    		List<Contact> addCon = new List<Contact>();
    		for(Contact cons : contactlist){
        		Account acc = Trigger.newmap.get(cons.accountid);
            	cons.Lastname = account.Name;
        		//cons.AccountId = account.id;
            	cons.Fax = account.Fax;
            	cons.Phone = account.Phone;
            	cons.MailingStreet = account.BillingStreet;
            	cons.MailingCity = account.BillingCity;
            	cons.MailingCountry = account.BillingCountry;
            	addCon.add(cons);
       	 	}
        	update addCon;
    	}
    }
}

 
This was selected as the best answer