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
Che SFDCChe SFDC 

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;

       
    }

 
Best Answer chosen by Che SFDC
Shyama B SShyama B S
Hi,
Please try the below code and let me know if it works.
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 != false ) {
            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 ) {
                Dev_che_123__Strategic_Contacts__c SC = new Dev_che_123__Strategic_Contacts__c ();
  				SC.Dev_che_123__Account__c = c.AccountID;
				SC.Dev_che_123__First_Name__c = c.FirstName;
				SC.Dev_che_123__Last_Name__c = c.LastName;
				STToInsert.add(SC);  		
    }
        insert STToInsert; 
    }

 

All Answers

Shyama B SShyama B S
Hi,
Please try the below code and let me know if it works.
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 != false ) {
            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 ) {
                Dev_che_123__Strategic_Contacts__c SC = new Dev_che_123__Strategic_Contacts__c ();
  				SC.Dev_che_123__Account__c = c.AccountID;
				SC.Dev_che_123__First_Name__c = c.FirstName;
				SC.Dev_che_123__Last_Name__c = c.LastName;
				STToInsert.add(SC);  		
    }
        insert STToInsert; 
    }

 
This was selected as the best answer
Pramodh KumarPramodh Kumar
Hi Che,

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.
Che SFDCChe SFDC
Thank you Shyama. It works.