You need to sign in to do that
Don't have an account?
AbhishekJ
Need help with a trigger, not updating the value when a corresponding record is deleted.
Trigger 'CommunicationContentTrigger' is written on a custom object 'Communication_Content__C' to populate value from First Name field on Contact object to Salutation field on Communication Content object. But the value on Salution is not getting updated when a contact is deleted. I am new to apex. Would anyone be able to help me with modifying the trigger.
Here is the Trigger:
Here is apex class handler
Here is the Trigger:
trigger CommunicationContentTrigger on Communications_Content__c ( before insert, before update) { CommunicationContentTriggerHandler handler = new CommunicationContentTriggerHandler(true,200,false); if (Trigger.isBefore) { //call your handler.before method if(Trigger.isInsert) { handler.OnBeforeInsert(Trigger.new); } else if(Trigger.isUpdate) { handler.OnBeforeUpdate(Trigger.oldMap,Trigger.new); } } }
Here is apex class handler
public with sharing class CommunicationContentTriggerHandler { private boolean m_isExecuting = false; private boolean force_update = false; private integer BatchSize = 0; public CommunicationContentTriggerHandler(boolean isExecuting, integer size, boolean forceUpdate){ m_isExecuting = isExecuting; BatchSize = size; force_update = forceUpdate ; } public void OnBeforeInsert(Communications_Content__c[] newRecords){ updateSalutationField(null,newRecords); } public void OnBeforeUpdate(Map<Id,Communications_Content__c> oldRecords, Communications_Content__c[] updatedRecords){ } public void updateSalutationField(Map<Id,Communications_Content__c> oldRecords, Communications_Content__c[] newRecords) { Set<Id> companyID = new Set<Id>(); Set<String> correspPref = new Set<String>(); List<Communications_Content__c> ccToUpdate = new List<Communications_Content__c>(); for(Communications_Content__c cc :newRecords) { System.debug('---- Company: ' + cc.Company__c + ' Correspondence_Preference__c: ' + cc.Correspondence_Preference__c); if(oldRecords == null && cc.Company__c != null && cc.Correspondence_Preference__c != null) { companyID.add(cc.Company__c); correspPref.add(cc.Correspondence_Preference__c); } if(oldRecords != null && (cc.Company__c != oldRecords.get(cc.Id).Company__c || cc.Correspondence_Preference__c != oldRecords.get(cc.Id).Correspondence_Preference__c )) { if(cc.Company__c == null || cc.Correspondence_Preference__c == null) { cc.Salutation__c = '' ; cc.Correspondence_Preference__c=''; ccToUpdate.add(cc); } else { System.debug('Not null'); companyID.add(cc.Company__c); correspPref.add(cc.Correspondence_Preference__c); } } if(oldRecords != null && cc.Company__c != null && cc.Correspondence_Preference__c != null && (cc.Company__c == oldRecords.get(cc.Id).Company__c || cc.Correspondence_Preference__c == oldRecords.get(cc.Id).Correspondence_Preference__c )){ System.debug('Blank update'); companyID.add(cc.Company__c); correspPref.add(cc.Correspondence_Preference__c); } } System.debug('MKlich Companies: '+ CompanyID); System.debug('MKlich Coresponding Pref: : '+ correspPref); if(companyID.size() > 0 && correspPref.size() > 0) { Map<string,List<String>> accCPandContactMap = new Map<string,List<String>>(); for(Correspondence_Preference__c cp : [Select Id,Contact__c,Contact__r.FirstName,Contact__r.Email,RecordType.Name,Company__c,Seniority_Rank_On_Contact__c, Contact__r.International_Salutation_Type__c, Contact__r.LastName from Correspondence_Preference__c where Company__c IN :companyID AND RecordType.Name IN :correspPref AND Recipient_Type__c = 'To' AND Contact__r.Email != null AND ( NOT Contact__r.Email LIKE '%@generalatlantic.com' ) order by Seniority_Rank_On_Contact__c, Contact__r.FirstName]){ string key = cp.Company__c+cp.RecordType.Name.toLowerCase(); System.debug('Key is: '+key); if(!accCPandContactMap.containsKey(key)) { accCPandContactMap.put(key,new List<String>{}); System.debug('Inside the if accCPandContactMap: '+accCPandContactMap.keySet()); System.debug('Inside the if accCPandContactMap: '+accCPandContactMap.Values()); } String name = ''; if(cp.Contact__r.International_Salutation_Type__c != null){ if(cp.Contact__r.International_Salutation_Type__c == 'Japanese'){ name = cp.Contact__r.LastName + '-san'; } else if(cp.Contact__r.International_Salutation_Type__c == 'Thai' && cp.Contact__r.FirstName != null){ name = 'Khun ' + cp.Contact__r.FirstName; } } else if(cp.Contact__r.FirstName != null){ name = cp.Contact__r.FirstName; } if(name != ''){ accCPandContactMap.get(key).add(name); } } for(Communications_Content__c cc :newRecords) { if(cc.Company__c != null && cc.Correspondence_Preference__c != null) { string key = cc.Company__c+cc.Correspondence_Preference__c.toLowerCase(); string valToUpdate = '' ; if(accCPandContactMap.containsKey(key)) { List<String> contacts = accCPandContactMap.get(key); valToUpdate = replaceLast(String.join(contacts,', '),', ',' and '); valToUpdate += ','; } cc.Salutation__c = valToUpdate ; if(String.isEmpty(cc.Salutation__c)) { cc.Correspondence_Preference__c = ''; } ccToUpdate.add(cc); } } } if(force_update && ccToUpdate.size() > 0 ){ update ccToUpdate; } } private string replaceLast(string mainStr, string substrToReplace, string replacedBy) { integer index = mainStr.lastIndexOf(substrToReplace); if (index == -1) return mainStr; return mainStr.substring(0, index) + replacedBy + mainStr.substring(index+substrToReplace.length()); } }
This issue is not limited to delete, I need the field to update when a new contact record is added as well.
One more thing to note here is that there is another cusotm object involed Correspondence Preference with master detail relationship, contact being the master. So until a corresponding record on Correspondence Preference object is created, I do not expect anything to change on communication content object. Would be glad to walk you through the whole functionality if that helps.
Here is the trigger:
Here is the handler apex class:
Sorry I am very new to this.
Communication Content has look up relationship with Company.
Company has master-detail relationship with Contact object.