Trigger to Automatically Submit Record for Approval

Use Case:

You want all new records for an object to be submitted to an Approval Process, but you do not want to rely on users to manually click the ‘Submit for Approval’ button after creating the record.

Solution:

After creating and testing a new Approval Process (Setup / App Setup / Create / Workflow & Approvals / Approval Processes), implement this simple Apex Trigger (the following example is for the Account object). You will also need to create a test method to ensure code coverage if your current production Apex Classes do not provide adequate coverage. Developers may also want to add try / catch statements to ensure that exceptions are caught and handled properly.

The Apex Trigger code:


trigger accountApprovalSubmit on Account (after insert) {

for (Account a : trigger.new) {

Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();
app.setObjectId(a.id);

Approval.ProcessResult result = Approval.process(app);

}

}

Tip submitted by:

Michael Topalovich
Delivered Innovation

Create VisualForce Tab to Display Static List View for Standard or Custom Object

Use Case:

You would like to create a custom tab to display a specific list view for a standard or custom object rather than use the standard tab view that only displays recently viewed records.

Solution:

  1. From the standard or custom object tab, Edit an existing view or ‘Create New View.’
  2. Ensure that you give the view a Name, that you specify filter criteria, that you select the fields to display, and that you restrict visibility as necessary.
  3. When the view has been saved and you are returned to the tab, select the view from the dropdown.
  4. Copy the 15-character view ID in the URL and paste it to Notepad, or write it down.
  5. Create a new VisualForce page (Setup / App Setup / Develop / Pages) using the sample code provided below.
  6. Create a new VisualForce tab (Setup / App Setup / Create / Tabs) and select the VisualForce Page created in Step #5, provide a Tab Label, Tab Name, and Tab Style, and then apply the desired profile visibility for the new tab.
  7. Optional – hide the tab for the standard or custom object if your new tab is a replacement for the default tab.
  8. If you want to change the View that is displayed in the tab at any time, simply navigate back to the standard tab for the object and Edit the view to meet requirements.

The VisualForce Page code:

Note: Replace “xxxxxxxxxxxxxxx” with the 15-character ID of the View that you captured in Step #4 above.  You can adjust the ‘height’, ‘customizable’, and ‘rowsPerPage’ values as desired.

<apex:page >
<apex:enhancedList listId="xxxxxxxxxxxxxxx" height="600" customizable="false" rowsPerPage="25"/>
</apex:page>

Tip submitted by:

Michael Topalovich
Delivered Innovation

Record Type Redirect to VisualForce Page Using Button Override

This tip originally appeared on SaaSkatoon, the SaaS and cloud computing blog from Delivered Innovation.

Use Case: Client has multiple call centers supporting various product lines; support team ‘A’ requires a custom Wizard to rapidly search for and collect details for the Case record that the standard Salesforce search interface cannot provide.

Solution: While you can assign a specific Page Layout to a specific Record Type and embed VisualForce pages in the object Detail view, currently salesforce.com does not support embedded VisualForce Pages in the Edit view of a Page Layout; likewise, salesforce.com does not support custom VisualForce pages for specific Record Types. The solution is to “intercept” the command to create a new record in an Object before Salesforce processes it, and this is accomplished with a 1-line VisualForce page and a StandardController extension that pulls and analyzes certain URL parameters to enhance the out-of-the-box process routing capabilities of Salesforce.

Continue reading Record Type Redirect to VisualForce Page Using Button Override