+ Start a Discussion
cml9cml9 

Can anyone help in creating a trigger for this apex trigger?

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

   List<RecordType> rtypes = [Select Name, Id From RecordType 
                 					 where sObjectType='Account' and isActive=true];
    Map<String,String> accountRecordTypes = new Map<String,String>{};
     
        for(RecordType rt: rtypes)
       		 accountRecordTypes.put(rt.Name,rt.Id);
    
    if (trigger.isInsert) {

         for (Account a: Trigger.new ) {
           String LastName, FirstName;
             
           if (a.Contact_Name__c.indexof(' ') == -1) {
                 LastName =  '-';
               	 FirstName = a.Contact_Name__c;
           } else {
                 LastName = a.Contact_Name__c.Substring(a.Contact_Name__c.indexof(' '),a.Contact_Name__c.length());
                 FirstName = a.Contact_Name__c.substring(0,a.Contact_Name__c.indexof(' '));
           }   
        
            
           if (a.RecordTypeId==accountRecordTypes.get('Sales Process')){ 
               		if (a.CreatedById == '005p0000000r5oI') { //Use to determine if Account was created through API || Needs to be changed to '00528000004nP9f' before deploying to Prod
                       Decimal myD = a.Business_ID__c; 
                       Integer newInt = myD.intValue();
                       
                       Contact c = New Contact();
                     
                             c.FirstName = FirstName;
                   			 c.LastName = LastName;
                             c.AccountId = a.id;
                             c.Phone = a.Phone;
                             c.Email= a.Contact_Email__c;
                             c.Phone = a.Contact_Phone__c;
                             c.Business_Id__c = newInt.format().remove(',');
                             c.User_ID__c = a.User_Id__c;
                                
       
              			insert C;
                 	}
         }
    	}
    }
    
    if (trigger.isUpdate) {
      System.debug(Trigger.newMap.keySet());
      List<Account> accountsWithContacts = [select id, User_Id__c, Contact_Name__c, Business_ID__c,Contact_Email__c,Phone,Contact_Phone__c,  (select id, 
                                                                firstname, lastname, email, User_ID__c,Phone,Business_Id__c,MobilePhone from Contacts) 
                                                                from Account where id IN :Trigger.newMap.keySet()];
	  
  List<Contact> contactsToUpdate = new List<Contact>{};
  
 	 for(Account a: accountsWithContacts){
           String LastName, FirstName;
             
           if (a.Contact_Name__c.indexof(' ') == -1) {
                 LastName =  '-';
               	 FirstName = a.Contact_Name__c;
           } else {
                 LastName = a.Contact_Name__c.Substring(a.Contact_Name__c.indexof(' '),a.Contact_Name__c.length());
                 FirstName = a.Contact_Name__c.substring(0,a.Contact_Name__c.indexof(' '));
           }   
         
    
     	if (a.Contacts.isEmpty()) {
        	             Decimal myD = a.Business_ID__c; 
             Integer newInt = myD.intValue();
                       
                       Contact c = New Contact();
                     
                             c.FirstName = FirstName;
                   			 c.LastName = LastName;
                             c.AccountId = a.id;
                             c.Phone = a.Phone;
                             c.Email= a.Contact_Email__c;
                             c.MobilePhone = a.Contact_Phone__c;
                             c.Business_Id__c = newInt.format().remove(',');
                             c.User_ID__c = a.User_Id__c;
            
              contactsToUpdate.add(c);  
                 insert contactsToUpdate;
        }
        for(Contact c: a.Contacts) {
         
              if (a.User_Id__c == c.User_Id__c || true) {
               				 c.Description= c.firstName + ' ' + c.lastname; 
               				 c.FirstName = FirstName;
                   			 c.LastName = LastName;
                  			 c.MobilePhone = a.Contact_Phone__c;
                       		 c.Phone = a.Phone;
                             c.Email= a.Contact_Email__c;
               
   	  			contactsToUpdate.add(c);  
                   update contactsToUpdate;
              }
   	  		
        } 	  
      }
    }   
}

Thanks!
ManojSankaranManojSankaran
Hi,

Can you please elobrate the requirement that you have to achieve.

Thanks
Manoj S
cml9cml9
I started to create the trigger but I only have 59% coverage and I dont know where to get the rest or just the lacking 16%.
 
@isTest
public class AccountSyncTest {
    
    static testMethod void SSAdminSync() {
        
       String strRecordTypeId = [Select Id From RecordType Where SobjectType = 'Account' and Name = 'Sales Process'].Id;
        
      
        User u = new User();
       
      
       u.FirstName = 'Name';
       u.LastName  = 'Last';
       u.Email     = 'tester+user@gmail.com';
       u.Username  = 'tester+user@gmail.com';
       u.Alias     = 'nlast';
       u.ProfileId = '00e280000016UKf';
        u.TimeZoneSidKey    = 'America/Denver';
       u.LocaleSidKey      = 'en_US';
       u.EmailEncodingKey  = 'UTF-8';
       u.LanguageLocaleKey = 'en_US';
        
        insert u;
        	
       String Owner_Id = u.id;
        
        List<Account> a = new List<Account>();
         a.add(new Account(
             
                Name = 'Account',
                Contact_Name__c = 'My Name',
                Phone = '123456789',
                Contact_Email__c = 'myemail@mail.com',
                Contact_Phone__c = '123456789',
                Business_ID__c = 123456,
                Business_Name__c = 'MyBusiness',
                Business_Phone__c = '123456789',
                Mobile_Phone__c = '123456789',
             	RecordTypeId = strRecordTypeId,
             	OwnerId = Owner_Id, 
                User_Id__c = 123456 ));
                               
       
            insert a;
        
                a[0].Name = 'Account';
                a[0].Contact_Name__c = 'My Name';
                a[0].Phone = '123456789';
                a[0].Contact_Email__c = 'myemail@mail.com';
                a[0].Contact_Phone__c = '123456789';
                a[0].Business_Name__c = 'MyBusiness';
                a[0].Business_Phone__c = '123456789';
                a[0].Mobile_Phone__c = '123456789';
               
        
            update a;
            
        
            List<Contact> c = new List<Contact>();
         	 c.add(new Contact(

                LastName = 'LastName',
                FirstName = 'FirstName',
                AccountId = a[0].Id,
                Email = a[0].Contact_Email__c,
                Phone = a[0].Business_Phone__c,
                MobilePhone = a[0].Mobile_Phone__c,
                User_ID__c = a[0].User_Id__c,
                Business_Id__c = string.valueOf(a[0].Business_ID__c)));
            
            
            insert c;
             
                c[0].LastName = 'LastName';
                c[0].FirstName = 'FirstName';
                c[0].Email = a[0].Contact_Email__c;
                c[0].Phone = a[0].Business_Phone__c;
                c[0].MobilePhone = a[0].Mobile_Phone__c;
        	
        	update c;
           
    }

}

I have already removed the description since it was just used for testing.