Alert with Three Options – jQueryUI in a Salesforce Custom Button

Recently, I was asked to create a custom button that would allow a user to choose between two SuperClone Configuration options. jQueryUI provides a nice modal dialog that solved the problem. The dialog showed two buttons for the configurations and a cancel button. Below is the sample code for what I put together. The actions for each button could be swapped out to load other any other pages or run javascript methods.

A few stackoverflow posts helped me put this together. Credit to:
http://stackoverflow.com/questions/9091001/how-to-show-confirmation-alert-with-three-buttons-yes-no-and-cancel-as-it.
http://salesforce.stackexchange.com/questions/63291/open-jquery-dialog-from-custom-button
http://stackoverflow.com/questions/16170157/dialog-was-hidden-behind-overlay-when-modal-true-on-jquery-dialog-in-asp-net
jQueryUI Modal Dialog

Eclipse Force.com IDE Editor Configuration Preferences

Eclipse can be setup to display code in an infinite number of ways. The following settings are how I like to configure the Eclipse Force.com IDE.

  • Spaces instead of tabs
  • Show line numbers
  • Show whitespace characters
  • Show a print margin

These settings can be changed at: Window -> Preferences -> General -> Editors -> Text Editors

Eclipse Editor Configuration

Click the “configure visibility” link next to the show white space setting to add fine tuning. I always uncheck the the trailing carriage return and line feed boxes and increase the opaqueness.

Eclipse White Space Characters

Apex File Naming Standards

Consistent file naming helps keep Visualforce and Apex classes easy to recognize and find. I’ve seen many orgs with both the good and the bad. Below is a table with naming suggestions. Some of them are mine, but most came from a presentation that I found referenced in a Trailhead module. The presentation is by Vivek Chawla. I recommend looking it over because it covers a lot of good stuff. Thank you Vivek.

Apex and Visualforce Naming

  • UpperCamelCase
  • Avoid using underscores.
  • Use full names where possible. Be consistent.
  • Test class should be named for the class they cover, and the word “Test” should be appended to the end.
Functional Type Name Suffix Examples
Trigger Trigger AccountTrigger
Trigger Handler TriggerHandler AccountTriggerHandler
VF Controller Controller AccountCloneController
VF Controller Extension ControllerExt AccountCloneControllerExt
Service Class Services AccountServices
Utility Class Util AccountDupeCatcherUtil
Selector Class Selector AccountSelector
Model / Wrapper Class Varies… Accounts / AccountWrapper
Web Service (SOAP) Ws AccountToolsWs
Web Service (REST) Rest AccountToolsRest
Email Service EmlSvc AccountCreateEmlSvc
Asynchronous (Future) Async AccountIntegrationsAsync
Asynchronous (Batch) Batch AccountMigrationBatch
Scheduled Apex Job AccountCleanupJob
Queueable Apex Que AccountSyncingQue
Visualforce Page -none- AccountClone
Visualforce Component Cmp AccountCloneCmp
Lightning Components -TBD-
Lightning Pages -TBD-

Workflow Rule, Field Update, and Email Alert Naming

Workflow Rules and their actions can also get unruly without consistent naming. Force365 published some good suggestions. Below are the ones that I refer to most.

Workflow Rule
[Object]: [Criteria Description i.e. Condition]

Workflow Actions
Field Update –
Set [Field] to [Value]
Email Alert –
Send [Template short description]
Task –
[Task Subject]

References

SlideShare: Salesforce Development Best Practices by Vivek Chawla
Blog: Force365 – Salesforce Naming Conventions – Declarative

 

Using the Enter Keypress Event in Visualforce to Search or Change Checkbox Properties

Perform common actions like run a search or check a checkbox when the enter key is pressed, instead of form submission. The example below shows how to do this with help from jQuery in the Visualforce page.

Things to notice:

  • jQuery.noConflict() prevents conflicts with other javascript that Salesforce may have loaded.
  • j$(document).ready will run the contained JavaScript after the components are loaded on the page. Binding event handling to elements could be missed without this.
  • event.keyCode == 13 is the key code for the Enter button.
  • CSS classes are used to attach the keypress event handling, but you could also specify element types, names, or any other valid selector logic.
  • searchFilter CSS class could be used on text, drop down/picklists, checkboxes, etc.
  • .trigger(‘click’) is used below. It is similar to .click(), and they are interchangeable for this example. The difference that I’m aware of is .trigger(‘click’) can accept additional parameters.
  • Probably only one or two of the methods below would be used in a real project. Multiple options are documented to illustrate different options.

Continue reading

Javascript Popup Window Tips for Salesforce

Check if a Popup Blocker Prevented the Popup Window

Most browsers will block popup windows the first time a popup window tries to appear. Here is how to check if a popup window was blocked.

Modal Window

Making a popup window modal helps prevent it from being lost behind the main browser window. The following technique makes the popup window display above the window that created it. However, it doesn’t force the window to stay on top when other tabs or applications have focus.

Refresh or Change the Original Page’s Location

A popup window may be used to alter information related to what is displayed on the page that initiated the window. This may require the original page to be refreshed or redirected. Assign the URL to the window.opener.location.href property to change opener’s page.

Custom Button to Refresh the Original Page

Custom Buttons that use Javascript can do a lot. However, security in modern browsers can make it tricky to refresh or change the URL of the originating page. One way to do this is to have the originating page monitor the popup window that was created. The page can then refresh itself when the popup window no longer exists in memory.

Custom button code:

Change the Window Size

Changing the window height to adapt to content within the page can be frustrating because of variations in different browsers. Below is an example of one way to do it. I would not call it pretty, but it works well enough. First, it sets an initial size for the page. This is to specify the width, and the content will flow down the page based on the width. Second, we set the page size again because we can now tell the height based on the new width. Please comment if you’ve found a better light weight way to do this.

Javascript in the Popup Window:

Close Popup after a Successful Action

A popup window may ask for user input and automatically close when processing has completed successfully. The controller can set a Javascript Boolean variable on success, and the portion of the form that is rerendered can have a script assign a true/false value. Then a method that evaluates if the window should be closed can check the value of that variable.

References

Mozilla – Window open
Mozilla – setInterval
Bob Buzzard – VisualForce Lookup