+ Start a Discussion
cml9cml9 

Something is wrong my Trigger during Update. Please help.

Hi Dev Masters,

I am writing a trigger wherein if there is a new Account created in SF it will create a Contact. The trigger was working fine during creation of new Contacts but during UPDATES its not doing anything. I am not sure why this is not working can anyone help? Thanks in advance!
 
trigger AccountSync on Account (after insert, after update) {

    if (trigger.isafter) {
        if (trigger.isInsert) {
   			 for (Account a: Trigger.new ) {
        
      			  if (a.RecordTypeId == '01228000000kgCd') { // Use to check if Record Type is equal to 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; // Conversion of Double to Integer so that when converted to it will remove .00
                            Integer newInt = myD.intValue();
                                             
                             Contact c = New Contact();
                             c.LastName = a.Contact_Name__c.Substring(a.Contact_Name__c.indexof(' '),a.Contact_Name__c.length());
                             c.FirstName = a.Contact_Name__c.substring(0,a.Contact_Name__c.indexof(' '));
                             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) {
   			 for (Account a: Trigger.new ) {
        
      			  if (a.RecordTypeId == '01228000000kgCd') { // Use to check if Record Type is equal to 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; // Conversion of Double to Integer so that when converted to it will remove .00
                            Integer newInt = myD.intValue();
                                             
                             Contact c = New Contact();
                             c.LastName = a.Contact_Name__c.Substring(a.Contact_Name__c.indexof(' '),a.Contact_Name__c.length());
                             c.FirstName = a.Contact_Name__c.substring(0,a.Contact_Name__c.indexof(' '));
                             c.Phone = a.Phone;
                             c.Email= a.Contact_Email__c;
                             c.Phone = a.Contact_Phone__c;
                             
                            
                                
       
            update C; 
                              }
            }
        } 
       }
    }
       
 
}

 
karthikeyan perumalkarthikeyan perumal
Hello, 

use below updated code for insert and update as well. 
 
trigger AccountSync on Account (after insert, after update) {

    if (trigger.isafter) {
        if (trigger.isInsert) {
   			 for (Account a: Trigger.new ) {
        
      			  if (a.RecordTypeId == '01228000000kgCd') { // Use to check if Record Type is equal to 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; // Conversion of Double to Integer so that when converted to it will remove .00
                            Integer newInt = myD.intValue();
                                             
                             Contact c = New Contact();
                             c.LastName = a.Contact_Name__c.Substring(a.Contact_Name__c.indexof(' '),a.Contact_Name__c.length());
                             c.FirstName = a.Contact_Name__c.substring(0,a.Contact_Name__c.indexof(' '));
                             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) {
   			 for (Account a: Trigger.new ) {
        
      			  if (a.RecordTypeId == '01228000000kgCd') { // Use to check if Record Type is equal to 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; // Conversion of Double to Integer so that when converted to it will remove .00
                            Integer newInt = myD.intValue();
                                             
                             contact c=[select LastName,FirstName,Phone,Email from contact where accountid =:a.id];
              c.LastName = a.Contact_Name__c.Substring(a.Contact_Name__c.indexof(' '),a.Contact_Name__c.length());
                             c.FirstName = a.Contact_Name__c.substring(0,a.Contact_Name__c.indexof(' '));
                             c.Phone = a.Phone;
                             c.Email= a.Contact_Email__c;
                             c.Phone = a.Contact_Phone__c;
                            
       
            update C; 
             }
            }
        } 
       }
    }
       
 
}
Hope this will help you. 

mark it best ANSWER if its work for you. 

Thanks
karthik

 
cml9cml9
sir is this bulkified?
karthikeyan perumalkarthikeyan perumal
Hello 

Use below code, 
 
trigger AccountSync on Account (after insert, after update) {
List<contact> lstc= new List<Contact>();
    if (trigger.isafter) {
        if (trigger.isInsert) {
   			 for (Account a: Trigger.new ) {
        
      			  if (a.RecordTypeId == '01228000000kgCd') { // Use to check if Record Type is equal to 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; // Conversion of Double to Integer so that when converted to it will remove .00
                            Integer newInt = myD.intValue();
                                             
                             Contact c = New Contact();
                             c.LastName = a.Contact_Name__c.Substring(a.Contact_Name__c.indexof(' '),a.Contact_Name__c.length());
                             c.FirstName = a.Contact_Name__c.substring(0,a.Contact_Name__c.indexof(' '));
                             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) {
   			 for (Account a: Trigger.new ) {
        
      			  if (a.RecordTypeId == '01228000000kgCd') { // Use to check if Record Type is equal to 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; // Conversion of Double to Integer so that when converted to it will remove .00
                            Integer newInt = myD.intValue();
                                             
                           for(Contact c : [select LastName,FirstName,Phone,Email from contact where accountid =:a.id])
							{
                             c.LastName = a.Contact_Name__c.Substring(a.Contact_Name__c.indexof(' '),a.Contact_Name__c.length());
                             c.FirstName = a.Contact_Name__c.substring(0,a.Contact_Name__c.indexof(' '));
                             c.Phone = a.Phone;
                             c.Email= a.Contact_Email__c;
                             c.Phone = a.Contact_Phone__c;
                             lstc.add(c);
						    }
       
           
             }
			  updatelstc; 
            }
        } 
       }
    }
       
 
}

Hope this will help you. 

Thanks
karthik