You need to sign in to do that
Don't have an account?
Daniel Gonzalez 209
Trigger to (count number of contacts associated with an account)
Hi guys, I need help for a Trigger, I new in Salforce and develop but I need solve this:
"However, field in Membership Sales (Number_Of_Members__c) will require automation, because roll up summary fields cannot be created between a Contact (Member) and Account (Membership). For this field, create an Apex trigger on after insert, update (only run code for update when the Account field on the Contact is updated), and delete Contact that will set the number of Contact records on the parent Account. Create a custom number field on Account named Number_Of_Members__c, which will not appear on the Membership's page layouts. This field will be updated to the count of Contacts on the Membership"
I have this code and I have to add code for the request:
I hope you can help me.
Thanks.
"However, field in Membership Sales (Number_Of_Members__c) will require automation, because roll up summary fields cannot be created between a Contact (Member) and Account (Membership). For this field, create an Apex trigger on after insert, update (only run code for update when the Account field on the Contact is updated), and delete Contact that will set the number of Contact records on the parent Account. Create a custom number field on Account named Number_Of_Members__c, which will not appear on the Membership's page layouts. This field will be updated to the count of Contacts on the Membership"
I have this code and I have to add code for the request:
trigger MemberTrigger on Contact (before insert, after insert, after update, after delete) { if (Trigger.isInsert) { if (Trigger.isBefore) { Map<String, Communication_Preferences__c> preferenceMap = new Map<String, Communication_Preferences__c>(); for (Contact member : Trigger.New) { if (member.Communication_Preferences__c == null) { Communication_Preferences__c newPreference = new Communication_Preferences__c(); newPreference.Name = member.lastName; preferenceMap.put(member.Id, newPreference); } } insert preferenceMap.values(); for (Contact member : Trigger.new) { member.Communication_Preferences__c = preferenceMap.get(member.Id).Id; } } if (Trigger.isAfter) { List<String> accountIds = new List<String>(); for (Contact member : Trigger.New) { if (!accountIds.contains(member.AccountId)) { accountIds.add(member.AccountId); } } List<Account> accountList = [SELECT Id, First_Membership_Members__c FROM Account WHERE Id IN : accountIds]; List<Account> updatedAccounts = new List<Account>(); for (Contact member : Trigger.New) { for (Account accountElement : accountList) { if (member.AccountId == accountElement.Id) { if (accountElement.First_Membership_Members__c == null) { String nameString; if (member.FirstName != null) { nameString = member.FirstName + ' ' + member.LastName; }else { nameString = member.LastName; } accountElement.First_Membership_Members__c += member.Id + ':' + nameString; } else { String nameString; if (member.FirstName != null) { nameString = member.FirstName + ' ' + member.LastName; }else { nameString = member.LastName; } accountElement.First_Membership_Members__c += ',' + member.Id + ':' + nameString; } updatedAccounts.add(accountElement); } } } update updatedAccounts; } }else if (Trigger.isUpdate) { if (Trigger.isAfter) { }else if (Trigger.isDelete) { if (Trigger.isAfter) { } } }
I hope you can help me.
Thanks.
Try Below Trigger Please Mark It As Best Answer If It Helps
Thank You!
All Answers
Try Below Trigger Please Mark It As Best Answer If It Helps
Thank You!
Thank You!
if you liked @CharuDutt answer then don't forget to mark it as best answer so that it can help to others.
Thank you