We can create and link child object record from related lists on parent object record but what if we have to create and link parent record from child record?
Here in this post I'm creating and associating an Account (parent) with already created Case (child) record and same can be done for other objects.
Following are the two flows to create and associate Account with a case.
Flow A: Standard Flow
Step 1: Case is raised via email to case.
Step 2: Create an Account (and populate email field with the supplied email from case so that future cases are automatically associated with the account).
Step 3: Open the case in edit mode and link the case to the account.
Step 4: Save the record.
Cons:
1. Consumes too much time while switching between Account and Case tabs
2. There might be multiple accounts with same name which might result in relating the case with wrong account
Pros:
1. Standard functionality
Clicks involved (least possible clicks)
1. Go to cases
2. Open the case
3. Open accounts in new tab
4. Click on new and fill the details
5. Save
6. Go back to case tab
7. Edit
8. Type the account name and click on lookup
9. Save.
Flow B: Custom Solution
Step 1: Click on the Create Account custom button on Case detail page
Step 2: Fill the details of the Account
Step 3: Save the record and on save you’ll be redirected back to the case details page and the case is also associated with the new account record
Cons:
1. Create a custom Case detail page
Pros:
1. Less time consuming flow.
2. Negligible chances of associating the case with wrong Account.
Clicks involved (least possible clicks)
1. Go to cases
2. Open the case
3. Click on Create Account Button
4. Fill in the details
5. Save
Result: 44.44% less clicks and saved 44.44% of the time the user spent on clicks every time he created an Account.
How to implement flow B:
When I created a custom button to create an account, I used saveURL and retURL attribute and due to this I created a new account using that button on save/cancel I was redirected back to the case detail page and the Id of the new account after saving the new account record was added the URL as newid by Salesforce.
So in my solution I used the auto generated newid URL attribute to associate the case with the newly created account. Also a DML is performed when the page is redirected back to the Case Detail page after the creation of new Account.
Step 1: Create a custom button
Formula:
/001/e?&saveURL={!Case.Id}&retURL={!Case.Id}
Step 2: Create a Visualforce page and override case View page with the custom visualforce page.
Page:
Page:
<apex:page standardController="Case" extensions="Case_View_custom_Controller" sidebar="false" action="{!associateToNewAccount}"> <apex:pageMessages /><br/> <apex:form > <apex:pageBlock title="Case Information" mode="inlineEdit"> <apex:pageBlockButtons > <apex:commandButton action="{!Edit}" id="editButton" value="Edit"/> <apex:commandButton action="{!Delete}" id="delete" value="Delete"/> <apex:commandButton action="{!URLFOR($Action.Case.Create_Account, Id)}" id="createAccountButton" value="Create Account"/> </apex:pageblockButtons> <apex:pageBlockSection columns="2"> <apex:repeat value="{!infoFields}" var="f"> <apex:outputField value="{!Case[f]}"/> </apex:repeat> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
Controller:
public class Case_View_custom_Controller { private ApexPages.StandardController controller; public Case_View_custom_Controller(ApexPages.StandardController controller) { this.controller=controller; controller.addFields(infoFields); } public list<string> infoFields{ get{ if(infoFields==null){ infoFields=new List<string>(); infoFields.add('caseNumber'); infoFields.add('Status'); infoFields.add('Priority'); infoFields.add('OwnerId'); infoFields.add('AccountId'); infoFields.add('Origin'); infoFields.add('Description'); } return infoFields; } private set; } public void associateToNewAccount(){ case caseDetails; String newAccountId= ApexPages.currentPage().getParameters().get('newid'); String caseId=ApexPages.currentPage().getParameters().get('id'); if(caseId!=null) caseDetails=[select accountId from case where id=:caseId]; if(newAccountId!=null && newAccountId.startsWith('001') && caseDetails.accountId!=newAccountId){ caseDetails.accountId=newAccountId; update caseDetails; } } }
No comments:
Post a Comment