How to update Account object when related AccountPartner is updated/inserted/deleted/undeleted?

I am working on an org with Person Accounts enabled, but I think that my problem applies to regular non-person Accounts as well.

I am tasked with writing a trigger to cross-populate spouse information for Accounts that have an AccountPartner object where role=spouse.

This works fine for when the Account object is update/inserted/deleted/undeleted. The problem is that creating or deleting a partner relationship does not cause the trigger on the Account object to fire. So you can create John and Jane Doe, and when you create each of them the trigger will fire on insert, but they are not partners yet and no information will be cross-populated. Now you go to the record you just created and add a new partner relationship and set the type to spouse. This creates the AccountPartner object but does not update the Account record and therefore the trigger is not fired.

My solution was to create a trigger on the AccountPartner object, except I get the error:

sObject type does not allow triggers: AccountPartner

The same is true for the Partner object

sObject type does not allow triggers: Partner

So I’m stuck here. How do I cause my trigger to fire when two accounts are linked via a partner relationship? Or how do I populate a custom field on each Account with a value from the partner account?

Answer

Much like AccountContactRole and OpportunityContactRole and other junction objects, it would appear that AccountPartners don’t support workflow or triggers either.

I was faced with the same problem on ACRs and OCRs and the couple of solutions possible are :

Override the Partner Related List with a custom VisualForce section, which means you can control all of the users Interaction with the object and trap the event to perform a phantom update on the Account to invoke your trigger, which can process the event as usual. (you can’t explicitly insert an AccountPartner, you will need to operate on the Partner object which creates an AccountPartner behind the scenes)
Reference : http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_partner.htm

Use a custom object to model the AccountPartner relationship, which lets you write triggers and workflows and work as usual. This is the lesser preferable option in my head coz it would mean you miss out on the standard Partner functionality that comes out of the box with Salesforce.

Here’s a first cut of what you could use

Controller Class:

public with sharing class PartnerExtension{

 public PartnerExtension(ApexPages.StandardController stdController) {
    this.acct = (Account)stdController.getRecord();

    this.pageMode = 'Detail';

    accPartner = [Select AccountToId, AccountFromId, Role FROM Partner WHERE AccountFromId = :acct.Id];
}

public Account acct;
public Partner[] accPartner { get; set; }
public String pageMode { get; set; }

public PageReference setEditMode(){
    this.pageMode = 'Edit';
    accPartner =  this.accPartner = new Partner[]{ new Partner(),new Partner(),new Partner(),new Partner(),new Partner()};

    return null;

}



public PageReference savePartner(){
    List<Partner> partnersToIns = new List<Partner>{};
    List<Account> acctsToUpdt = new List<Account>{};

    for(Partner accP : accPartner){
    if(accP.AccountToId  != null && 
             accP.Role != null){
        accP.AccountFromId = acct.Id;
        partnersToIns.add(accP);

        if( accP.Role == 'Spouse')
            acctsToUpdt.add(new Account(Id=accp.AccountFromId));
    }
        insert accPartner;

        update acctsToUpdt;

        pageMode = 'Detail';
    }

    accPartner = [Select AccountToId, AccountFromId, Role FROM Partner WHERE AccountFromId = :acct.Id];
    return null;
}

}

Visualforce Page :

<apex:page standardController="Account" extensions="PartnerExtension" >

<apex:form>
<apex:pageBlock mode="{!pageMode}">

<apex:pageBlockButtons>
<apex:commandButton value="Save" action="{!savePartner}" rendered="{!pageMode=='Edit'}"/>
<apex:commandButton value="New" action="{!setEditMode}"/>
<apex:commandButton value="Cancel" action="{!Cancel}" rendered="{!pageMode=='Edit'}" />
</apex:pageBlockButtons>

<apex:pageBlockSection>

<apex:pageBlockTable value="{!accPartner}" var="accP">

<apex:column  headerValue="Action" rendered="{!pageMode=='Detail'}">
<a href="/setup/own/deleteredirect.jsp?delID={!accP.Id}&retURL=%2F{!Account.Id}">Del</a>
</apex:column>

<apex:column value="{!accP.AccountToId}" rendered="{!pageMode=='Detail'}"/>
<apex:column value="{!accP.Role}" rendered="{!pageMode=='Detail'}"/>

<apex:column headerValue="Partner" rendered="{!pageMode=='Edit'}">
<apex:inputField value="{!accP.AccountToId}" />
</apex:column>
<apex:column headerValue="Role" rendered="{!pageMode=='Edit'}">
<apex:inputField value="{!accP.Role}" />
</apex:column>

</apex:pageBlockTable>


<apex:outputPanel rendered="{!pageMode=='Detail'}">
<a href="/001?rlid=RelatedPartnerList&amp;id={!Account.Id}" target="_BLANK">Go to list(<apex:outputText value="{!accPartner.size}" />)&nbsp;»</a>
</apex:outputPanel>

</apex:pageBlockSection>

</apex:pageBlock>
</apex:form>

</apex:page>

Attribution
Source : Link , Question Author : Aaron P. , Answer Author : techtrekker

Leave a Comment