Create a custom field at Contract object "Opportunity Team Mail Address (API name: OpportTeam_Mail_Address__c )".
Then with the help of below trigger on update of any contract, we will be able to tag the email address of the Account Team Member. Please find the Trigger below:
trigger TagOppTeamMemEmailAddress on Contract (Before Update) {
Map<Id,Id> mapOfAccIdVsContrtId = new Map<Id,Id>();
Set<Id> uniqAccIDSet = new Set<Id>();
Set<Id> oppIdSet = new Set<Id>();
Map<Id,Id> mapOfOppIdVsAccId = new Map<Id,Id>();
Map<Id,String> mapOfOppIdvsEmailAdd = new Map<Id,String>();
Map<Id,String> mapContactvsEmailToUdt = new Map<Id,String>();
Set<ID> contrSet = new Set<ID>();
if(Trigger.isBefore && Trigger.isUpdate){
for(Contract cntr:Trigger.New){
System.debug('@@@ cntr:'+cntr);
if(cntr.AccountId != null){
mapOfAccIdVsContrtId.put(cntr.AccountId,cntr.Id);
uniqAccIDSet.add(cntr.AccountId);
}
}
System.debug('@@@ mapOfAccIdVsContrtId:'+mapOfAccIdVsContrtId);
if(!mapOfAccIdVsContrtId.isEmpty()){
for(Opportunity opp:[SELECT Id,AccountId FROM Opportunity WHERE AccountId In:uniqAccIDSet]){
oppIdSet.add(opp.Id);
mapOfOppIdVsAccId .put(opp.Id,opp.AccountId);
}
if(oppIdSet.size() > 0){
for(Opportunity_Team__c oppTeamMem :[SELECT Id,Email__c,Opportunity__c FROM Opportunity_Team__c WHERE Opportunity__c In:oppIdSet]){
if(oppTeamMem.Email__c != null && oppTeamMem.Email__c != ''){
mapOfOppIdvsEmailAdd.put(oppTeamMem.Opportunity__c,oppTeamMem.Email__c);
}
}
}
System.debug('@@@ mapOfOppIdvsEmailAdd:'+mapOfOppIdvsEmailAdd);
if(!mapOfOppIdvsEmailAdd.isEmpty()){
for(Id oppId : mapOfOppIdvsEmailAdd.Keyset()){
if(mapOfAccIdVsContrtId.get(mapOfOppIdVsAccId.get(oppId)) != null){
mapContactvsEmailToUdt.put(mapOfAccIdVsContrtId.get(mapOfOppIdVsAccId.get(oppId)),mapOfOppIdvsEmailAdd.get(oppId));
contrSet.add(mapOfAccIdVsContrtId.get(mapOfOppIdVsAccId.get(oppId)));
}
}
}
//Section where we are updating the email from Opp Team Mem
if(!mapContactvsEmailToUdt.isEmpty()){
for(Contract cntr:Trigger.New){
cntr.OpportTeam_Mail_Address__c = mapContactvsEmailToUdt.get(cntr.Id);
}
}
}
}
}