You are here:
Useful S-Controls
Use these samples to get started using s-controls.
Required Editions
| Available in: Salesforce Classic |
| Available in: Contact Manager, Group, Professional, Enterprise, Performance, Unlimited, and Developer Editions |
| Available in: Salesforce Classic |
Custom buttons and links are available in: All Editions S-controls are available in: Contact Manager, Group, Professional, Enterprise, Performance, Unlimited, and Developer Editions Overriding standard buttons and tab home pages is available in: Enterprise, Performance, Unlimited, and Developer Editions |
S-Controls for Detail Pages
Use the Yahoo Map API and the billing address merge fields to display a map for an account.
Yahoo Map
Use the Yahoo Map API and the billing address merge fields to display a map for an account. Use the following code in an HTML s-control and add it to your account detail page layout:
<html>
<head>
<script type="text/javascript"
src="http://api.maps.yahoo.com/ajaxymap?v=3.0&appid=YahooDemo">
</script>
<style type="text/css">
#mapContainer {
height: 200px;
width: 100%;
}
</style>
</head>
<body>
<div id="mapContainer"></div>
<script type="text/javascript">
// Create a map object
var map = new YMap(document.getElementById('mapContainer'));
// Display the map centered on given address
map.drawZoomAndCenter("{!Account.BillingStreet}, \
{!Account.BillingCity},\
{!Account.BillingState},\
{!Account.BillingPostalCode}", 3);
// Set marker at that address
map.addMarker("{!Account.BillingStreet}, \
{!Account.BillingCity},\
{!Account.BillingState},\
{!Account.BillingPostalCode}", 3);
</script>
</body>
</html>S-Controls that Override Standard Buttons and Tab Home Pages
You can have your own code that you prefer to use for adding products to opportunities instead of the standard page.
Add Product Override
Use the following s-control sample to pass data values using merge fields from a record detail page into a custom s-control that overrides the Add Product button on the Products related list of an opportunity. This type of override illustrates how related list buttons can contain merge fields from the master object as well as the detail. For example, this code contains opportunity merge fields, which is on the master side of a master-detail relationship with opportunity products.
<html>
<head>
<script type="text/javascript"
src="/soap/ajax/13.0/connection.js">
</script>
</head>
<body>
<b>Opportunity Info:</b>
<br>
Opportunity ID: {!Opportunity.Id}
<br>
Opportunity Name: {!Opportunity.Name}
<br>
Opportunity Record Type: {!Opportunity.RecordType}
<br>
</body>
</html>To implement this functionality, create an HTML s-control with the previous content and inserting your code in the space provided. Then, override the add product action from the opportunity products object using the s-control. This example assumes you have record types on opportunities.
Conditional Override for Editing Leads
You can override a standard action conditionally, redirecting to a standard action or custom s-control depending on certain conditions. For example, you want to use a separate s-control to edit leads when they have been open longer than 30 days. Using the following example, create an s-control to evaluate if a lead has been open longer than 30 days and, if so, run your custom s-control to edit leads. Otherwise, use the standard lead edit action.
<script type="text/javascript">
//determine if the lead has been open longer than 30 days
if ({!IF(ISPICKVAL( Lead.Status , "Open"), ROUND(NOW()- Lead.CreatedDate , 0), 0)} > 30)
{
//more than 30 days - display a custom scontrol page
window.location.href="{!URLFOR($SControl.EditLeadsOpenLongerThan30)}";
}
else
{
//30 days or less - display the standard edit page
window.parent.location.href="{!URLFOR($Action.Lead.Edit, Lead.Id, [retURL=URLFOR($Action.Lead.View, Lead.Id)], true)}";
}
</script>To implement this in your organization, create the s-control that you want to use to edit leads that have been open longer than 30 days. Name this s-control EditLeadsOpenLongerThan30. Next, create an s-control using the previous example code to determine if a lead has been open longer than 30 days, and, if so, override the edit action on leads using the EditLeadsOpenLongerThan30 s-control.
Note the differences between the first and second if statements in the
previous example code. The first one is a JavaScript if statement that
evaluates on the browser. The second is the Salesforce IF function that evaluates on the
server and returns a single value—the number of days the lead has been open, or zero if
the lead is not open.
To display a standard Salesforce page without invoking the override, set the no override argument in the URLFOR function to “true.”
Also,
use the retURL parameter in your URLFOR function to return the user to
the detail page after saving.
Edit Contact Override
You can have your own code that you prefer to use for editing contacts. Use this s-control sample to pass data values using merge fields from a record detail page into a custom s-control that overrides a standard detail page button.
<html>
<head>
<script type="text/javascript" src="/soap/ajax/13.0/connection.js">
</script>
</head>
<body>
<b>Contact Info:</b>
<br>
Contact ID: {!Contact.Id}
<br>
Contact Name: {!Contact.FirstName} {!Contact.LastName}
<br>
</body>
</html>
To implement this functionality, create an HTML s-control with the previous content inserting your code in the body section. Then, override the edit contact action using the s-control. This overrides the edit contact action everywhere it is available: the Edit button on a contact detail page, the Edit link on list views, and the Edit link on any related lists.
Interrupt Override for New Accounts
Overriding standard buttons makes them unavailable in your entire Salesforce organization. However, you can override a standard action and redirect to that action from your s-control without getting into an infinite loop. For example, you can override the New button on accounts, perform your own custom process, and resume with the standard new account action without getting into an infinite loop. To do this, use the no override argument in the URLFOR function.
<script type="text/javascript">
alert("Hi, I am demonstrating how to interrupt New Account with an override. Click OK to continue.");
window.parent.location.href="{! URLFOR($Action.Account.New, null, null, true)}";
</script>
To implement this s-control, create an HTML s-control with the previous content. Then, override the new account action using the s-control.
null. This example does not require any inputs, which
is why the third argument in the URLFOR function is set to null. The
fourth argument in the URLFOR function is set to true to ignore the
override, avoiding an infinite loop.Conditional Accounts Tab Home Page Override
You can override a tab home page conditionally, redirecting the original tab home page to an s-control depending on certain conditions. For example, you want to display an s-control, instead of the standard Accounts tab home page, to users with a specific profile. Using the following sample code, create an s-control to display job applicant information to users with the Recruiter profile when they click the Accounts tab; for all other users, display the standard Accounts tab home page.
To implement this, first create an s-control called “ApplicantHomePage” that contains the content to display to recruiters. Next create an s-control of type HTML using the following code to implement the conditional override logic:
<script type="text/javascript">
//determine the user profile name
var recruiter = {!IF($Profile.Name = "Recruiter", true, false)};
//when the profile is recruiter - display a custom s-control page
if (recruiter) {
window.parent.location.href="{! urlFor($SControl.ApplicantHomePage)}";
} else {
//when the profile is not recruiter - display the standard Accounts tab page
window.parent.location.href="{! urlFor( $Action.Account.Tab , $ObjectType.Account,null,true)}";
}
</script>Finally, override the Accounts tab to use the HTML s-control shown here. This example assumes that a profile named “Recruiter” exists in your organization.
S-Controls that Include Snippets
Include snippets in your custom s-controls to reuse common code.
Including Snippets
The following example references a snippet that provides a header for a page that displays in a web tab. The page has the title “My Title.” Use the $Control global variable to reference a snippet. To implement this, create two snippets called “Resize_Iframe_head” and “Resize_Iframe_onload” and create an HTML s-control called “Resize_Iframe_sample” that includes this code.
<html>
<body>
{!INCLUDE(
$SControl.Header_Snippet,
[title = "My Title", theme = "modern"]
)}
</body>
</html>
