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&id={!Account.Id}" target="_BLANK">Go to list(<apex:outputText value="{!accPartner.size}" />) ยป</a>
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
You must wait between several days up to a week in order to delete the parent record. Even if you empty the recycle bin, the records are not "permanently" deleted until some reaper job on the Salesforce side runs to truly delete them.
If you're in a rush, you can open a case with Salesforce asking to "run a physical delete" on your org. They will require that you agree to empty your recycle bin first.
The deleted records don't count toward your data storage limit.
We have worked around this problem by setting a 'status' field on the parent object to indicate that we want it deleted. After we delete all of the child records, we set the status to 'Deleting'. Then, create a Scheduled Apex job that runs around nightly trying to delete the parent records that are in that status. You must rescue the possible exception and carry on. Sooner or later, the records can be deleted.
Last caveat: sometimes the org gets corrupted and even the 1 week rule doesn't hold true. In that case, you will need to open a case and have them escalate it to R&D for physical deletion. This is rare, but it does happen.
The bit of Scheduled Apex we use looks something like:
List<SupremeQueryResult__c> deletedSnapshots = [SELECT Id FROM SupremeQueryResult__c
WHERE Status__c IN('Deleted','Deleting')];
for(SupremeQueryResult__c result : deletedSnapshots){
try{
delete result;
}
catch(System.DmlException dmle){
if(dmle.getDmlType(0) != StatusCode.DELETE_OPERATION_TOO_LARGE){
throw dmle; //reraise
}
}
}
Best Answer
I am not writing complete code for you suppose you have
As it is not optimize code because it has DML inside for loop but it will work for you. If you have more then millions record then you will hit the limit.