You need to sign in to do that
Don't have an account?

Help with Insert Trigger
All, need help with a trigger. This trigger copies record from Contacts standard object to Strategic Contacts custom object. I want this code to copy all Contacts in account and create Strategic Contacts. For example, if account has 4 contacts, then 4 strategic contacts should be created. Code is currently inserting only 1 record. Can someone pls help?
trigger StrategicContactAutoCr on Account (after update) { List<Dev_che_123__Strategic_Contacts__c> STToInsert = new List<Dev_che_123__Strategic_Contacts__c>(); Map <Id,Contact> ContactMaps = New Map <Id,Contact> (); Set <Id> accids = New Set <ID> (); for (Account acc: Trigger.New ) { if (acc.Strategic_Account__c != null ) { accids.add(acc.id); system.debug('total strt accs' + accids); } } List <Contact> Con = [SELECT AccountID, FirstName, LastName FROM Contact WHERE AccountID IN :accids ]; system.debug('total Con List' + Con); For (Contact C : Con ) { If (C.AccountID != null) { ContactMaps.put(C.AccountID, c); system.debug('total Con List after Put Id and Contact in Map' + Con); } } for (Account acct: Trigger.New ) { if (acct.Strategic_Account__c != False ) { Dev_che_123__Strategic_Contacts__c SC = new Dev_che_123__Strategic_Contacts__c (); SC.Dev_che_123__Account__c = acct.id; SC.Dev_che_123__First_Name__c = ContactMaps.get(acct.Id).FirstName; SC.Dev_che_123__Last_Name__c = ContactMaps.get(acct.Id).LastName; STToInsert.add(sc); } } insert STToInsert; }
Please try the below code and let me know if it works.
All Answers
Please try the below code and let me know if it works.
The Trigger you wrote will always insert a contact record whenever you update the Account record. I think you should a trigger on the Contact object instead of account object
Thanks,
Pramodh.