You need to sign in to do that
Don't have an account?
cml9
Thanks!
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!
Can you please elobrate the requirement that you have to achieve.
Thanks
Manoj S
I have already removed the description since it was just used for testing.