Does anyone know of an elegant solution to auto assign cases to different related accounts in a before trigger? All internally created cases are associated with 1 account by code. This creates a problem when multiple cases are being created at the exact same time as one of the cases will lock the account record and the other case will be lost. I originally was going to create 10 case specific accounts (CaseAccount1, CaseAccount2, etc.), and route cases to each account based on the last digit of the case (Case Number 12345670 -> CaseAccount0, Case Number 12345671 -> CaseAccount1, etc.) Unfortunately since Case Number is an Auto Number, it is not available on a before insert trigger. I think it's possible to do a random number generator but I don't think that would be the most ideal situation. Any ideas?
[SalesForce] Round Robin Auto-assignment of Related Account on Case object with before insert trigger
Related Solutions
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>
The problem you're having here doesn't have anything to do with triggers. As the error message clearly states (I haven't validated your other code, just fixed the problem you're facing)
Error: Compile Error: Didn't understand relationship 'Case' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 13 column 51
The problem you're having is that in your inner query in the FROM clause you're using Case, which isn't a known child relationship for Accounts. It is Cases (multiple), when you would do this in custom object relationships you would have your child relationshipname appended with __r
Map accounts = new Map( [ Select a.Id, (Select CaseStatusCalcGRY__c From Case Where CaseStatusCalcGRY__c != null) from Account a where a.ID in :accountIds] );
You can easily find the child relationship names (you'd have to insert in the from clause for an inner query) via these ways:
1. Eclipse / Force.com IDE Open your project and select your salesforce.schema file (which is in the root of your project), make sure you have the lastest version by Refreshing your Schema.
Then go to to object you wish to find the child relationships from (in your case Account), Open it, Go to Child Relationships and find the object you wish to use in your inner query (Case here), open that, and there you'll find the Relationship Name
this you'll have to use in your FROM clause
2. Force.com Explorer Very similar to the last, but in the Force.com Explorer tool now.
I'm sure there are plenty more ways, as this information can be read from the API, but these 2 are the onces I use the most.
Best Answer
If you know the 10 accounts at all times, then you can create 2 extra fields on the account - a checkbox field (Last_Round_Robin_Assigned__c or something) and a number field (Order_Number__c 1..10).
In your trigger then you can query your accounts, order by Order_Number__c, then loop through and find which one has Last_Round_Robin_Assigned__c field set to true. Assign the case to the next account and mark it
Last_Round_Robin_Assigned__c = true
and also mark the previous withLast_Round_Robin_Assigned__c = false
. Repeat the same for each case