Salesforce Documentation indicates that when the account field is edited on an Opportunity, related Partner records are deleted. You can find this information in the notes section here.
I have tried capturing these values in a trigger on the Opportunity and re-inserting them, As well as writing a trigger on the Partner object. Unfortunately neither of these workarounds have been successful. I don't want these Partner records automatically deleted. Is there anyway around this default behavior?
public without sharing class OpportunityTriggerHandler
extends GenericTriggerHandler
{
public override void BeforeUpdate(Map<Id, SObject> newItems, Map<Id, SObject> oldItems){
OpportunityTriggers.cachePartnersOnAccountChange((Map<Id, Opportunity>) oldItems, (Map<Id, Opportunity>) newItems);
}
public override void AfterUpdate(Map<Id, SObject> newItems, Map<Id, SObject> oldItems)
{
PartnerHelper.reInsertExistingPartners();
}
}
public static void cachePartnersOnAccountChange(Map<Id, Opportunity> oldOpps, Map<Id, Opportunity> newOpps){
List<Id> processOpportunities = new List<Id>();
for(Opportunity opp : newOpps.values()){
if(oldOpps.get(opp.Id) != null && oldOpps.get(opp.Id).AccountId != opp.AccountId){
processOpportunities.add(opp.Id);
}
}
PartnerHelper ph = new PartnerHelper(processOpportunities);
}
public with sharing class PartnerHelper {
public static List<Partner> existingPartners;
public partnerHelper(List<Id> opportunityIds){
existingPartners = new List<Partner>();
for(Partner p : [Select AccountFromId, AccountToId, IsPrimary, OpportunityId, ReversePartnerId, Role
From Partner Where OpportunityId = :opportunityIds]){
existingPartners.add(new Partner(AccountToId = p.AccountToId, IsPrimary = p.IsPrimary, OpportunityId = p.OpportunityId, Role = p.Role));
}
}
public static void reInsertExistingPartners(){
insert existingPartners;
}
}